aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2015-04-19 21:25:54 +0900
committerKazuki Yamaguchi <k@rhe.jp>2015-04-19 21:25:54 +0900
commita90c98bbeea0592a04c9459f98a674c686978c5e (patch)
treed8b07ee2c3f3b53469e256bdf6d9a437dc1d85cb
parent9fcbf0855f97d253475674618ac33065cb152663 (diff)
downloadaclog-a90c98bbeea0592a04c9459f98a674c686978c5e.tar.gz
web: upgrade grape from 0.9.0 to 0.11.0
-rw-r--r--Gemfile2
-rw-r--r--Gemfile.lock4
-rw-r--r--app/api/api.rb3
-rw-r--r--app/controllers/apidocs_controller.rb42
-rw-r--r--app/helpers/apidocs_helper.rb2
-rw-r--r--app/helpers/application_helper.rb4
-rw-r--r--app/views/apidocs/endpoint.html.haml15
-rw-r--r--app/views/apidocs/index.html.haml14
-rw-r--r--app/views/shared/sidebar/_apidocs.html.haml7
9 files changed, 38 insertions, 55 deletions
diff --git a/Gemfile b/Gemfile
index f1a2f0c..3807468 100644
--- a/Gemfile
+++ b/Gemfile
@@ -6,7 +6,7 @@ gem "tzinfo-data"
gem "mysql2"
gem "settingslogic"
gem "yajl-ruby", require: "yajl"
-gem "grape", "~> 0.9.0"
+gem "grape"
gem "grape-rabl"
gem "haml-rails"
gem "sass-rails"
diff --git a/Gemfile.lock b/Gemfile.lock
index f09920c..39e85f9 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -101,7 +101,7 @@ GEM
formatador (0.2.5)
globalid (0.3.5)
activesupport (>= 4.1.0)
- grape (0.9.0)
+ grape (0.11.0)
activesupport
builder
hashie (>= 2.1.0)
@@ -364,7 +364,7 @@ DEPENDENCIES
dalli
eventmachine
factory_girl_rails
- grape (~> 0.9.0)
+ grape
grape-rabl
guard-rspec
haml-rails
diff --git a/app/api/api.rb b/app/api/api.rb
index 8dcb7be..588e755 100644
--- a/app/api/api.rb
+++ b/app/api/api.rb
@@ -1,5 +1,6 @@
class Api < Grape::API
- format :json
+ content_type :json, "application/json"
+ default_format :json
formatter :json, Grape::Formatter::Rabl
error_formatter :json, ->(message, backtrace, options, env) do
{ error: { message: message } }.to_json
diff --git a/app/controllers/apidocs_controller.rb b/app/controllers/apidocs_controller.rb
index c9a8da8..b22ecf3 100644
--- a/app/controllers/apidocs_controller.rb
+++ b/app/controllers/apidocs_controller.rb
@@ -1,47 +1,31 @@
class ApidocsController < ApplicationController
- before_action :reload_docs
+ before_action :set_apidocs, :set_sidebar
def index
- @routes = @@routes
end
def endpoint
- @routes = @@routes
-
- method = @@routes[params[:method]]
- unless method
- raise Aclog::Exceptions::DocumentNotFound
- end
-
- @resource = method[params[:namespace]]
- unless @resource
- raise Aclog::Exceptions::DocumentNotFound
- end
-
- @endpoint = @resource[params[:path]]
- unless @endpoint
- raise Aclog::Exceptions::DocumentNotFound
- end
-
- if @endpoint.route_example_params
- @example_request_uri = root_url + "api" + @endpoint.route_path.sub(/\(\.:format\)$/, ".json")
- @example_request_uri += "?" + @endpoint.route_example_params.to_param
- end
+ method = @apidocs[params[:method].to_s.upcase] || raise(Aclog::Exceptions::DocumentNotFound)
+ @resource = method[params[:namespace]] || raise(Aclog::Exceptions::DocumentNotFound)
+ @endpoint = @resource[params[:path]] || raise(Aclog::Exceptions::DocumentNotFound)
end
private
- def reload_docs
- @@routes ||= begin
+ def set_apidocs
+ @apidocs = Rails.cache.fetch("apidocs", expired_in: 1.days) do
h = {}
Api.routes.reject {|r| r.route_ignore }.each {|route|
next if route.route_method == "HEAD"
- # /tweets/show(.:format) -> tweets, show
- method = route.route_method.downcase
- namespace = route.route_namespace[1..-1]
- path = route.route_path.sub(route.route_namespace, "")[1..-11] # 10: "(.:format)".size
+ method = route.route_method
+ namespace = route.route_namespace.sub(/^\//, "")
+ path = route.route_path.split("/", 3).last.sub(/\(\.:format\)$/, "")
((h[method] ||= {})[namespace] ||= {})[path] = route
}
h
end
end
+
+ def set_sidebar
+ @sidebars = [:apidocs]
+ end
end
diff --git a/app/helpers/apidocs_helper.rb b/app/helpers/apidocs_helper.rb
index dc3892b..2f47da5 100644
--- a/app/helpers/apidocs_helper.rb
+++ b/app/helpers/apidocs_helper.rb
@@ -1,5 +1,5 @@
module ApidocsHelper
def format_endpoint_name(endpoint)
- endpoint.route_method + " " + endpoint.route_path[1..-11]
+ endpoint.route_method + " " + endpoint.route_path.sub(/^\//, "").sub(/\(\.:format\)$/, "")
end
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 4e9157b..2d4d38a 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -47,6 +47,8 @@ module ApplicationHelper
parts = [:nav_user] + @sidebars
elsif all = @sidebars.delete(:all)
parts = [:nav_all] + @sidebars
+ elsif apidocs = @sidebars.delete(:apidocs)
+ parts = @sidebars
end
capture_haml do
@@ -54,6 +56,8 @@ module ApplicationHelper
haml_concat render("shared/sidebar/user")
elsif all
haml_concat render("shared/sidebar/all")
+ elsif apidocs
+ haml_concat render("shared/sidebar/apidocs")
end
haml_tag(".sidebar-flex") do
diff --git a/app/views/apidocs/endpoint.html.haml b/app/views/apidocs/endpoint.html.haml
index 8770b57..6fdf53a 100644
--- a/app/views/apidocs/endpoint.html.haml
+++ b/app/views/apidocs/endpoint.html.haml
@@ -2,13 +2,7 @@
.container
.row
.col-sm-3.sidebar
- %h1 aclog API
- .sidebar-list
- .list-group.list-group-scroll
- - @routes.values.inject(:merge).each do |namespace, endpoints|
- %span.list-group-head= namespace.titleize
- - endpoints.reject {|_, e| e.route_nodoc || e.route_deprecated }.each do |path, endpoint|
- = link_to_with_active format_endpoint_name(endpoint), about_api_endpoint_path(endpoint.route_method.downcase, namespace, path), class: "list-group-item"
+ = render_sidebar_content
.col-sm-9
%h1= format_endpoint_name(@endpoint)
%p= @endpoint.route_description
@@ -29,13 +23,12 @@
%p= options[:desc]
%p
%b> Type
- \:
- = options[:type]
- - if @example_request_uri
+ \: #{options[:type]}
+ - if @endpoint.route_example_params
%h2 Example Request
%p
%span= @endpoint.route_method
- %code#example_request_uri= @example_request_uri
+ %code#example_request_uri= root_url + "api" + @endpoint.route_path.sub(/\(\.:format\)$/, ".json") + "?" + @endpoint.route_example_params.to_param
%pre
%code
#example_request_loading= image_tag "loading.gif", alt: "loading..."
diff --git a/app/views/apidocs/index.html.haml b/app/views/apidocs/index.html.haml
index b011faf..978a72c 100644
--- a/app/views/apidocs/index.html.haml
+++ b/app/views/apidocs/index.html.haml
@@ -2,13 +2,7 @@
.container
.row
.col-sm-3.sidebar
- %h1 aclog API
- .sidebar-list
- .list-group.list-group-scroll
- - @routes.values.inject(:merge).each do |namespace, endpoints|
- %span.list-group-head= namespace.titleize
- - endpoints.reject {|_, e| e.route_nodoc || e.route_deprecated }.each do |path, endpoint|
- = link_to_with_active format_endpoint_name(endpoint), about_api_endpoint_path(endpoint.route_method.downcase, namespace, path), class: "list-group-item"
+ = render_sidebar_content
.col-sm-9
%h2 OAuth
%p ツイートを非公開にしているアカウントの情報にアクセスするには本人であるか、対象のアカウントをフォローしている必要があります。Aclog API では、その認証に OAuth Echo を使用します。
@@ -22,11 +16,11 @@
%a{href: "#"} Twitter のドキュメント
を参照してください。
%h2 Endpoints
- - @routes.values.inject(:merge).each do |namespace, endpoints|
+ - @apidocs.values.inject(:merge).each do |namespace, endpoints|
%h3= namespace.titleize
%table.table
%tbody
- - endpoints.reject {|_, e| e.route_nodoc }.each do |path, endpoint|
+ - endpoints.reject {|_, e| e.route_nodoc }.each do |action, endpoint|
%tr
- %td= link_to format_endpoint_name(endpoint), about_api_endpoint_path(endpoint.route_method.downcase, namespace, path), class: (endpoint.route_deprecated ? "deprecated" : nil)
+ %td= link_to format_endpoint_name(endpoint), about_api_endpoint_path(endpoint.route_method.downcase, namespace, action), class: (endpoint.route_deprecated ? "deprecated" : nil)
%td= endpoint.route_description
diff --git a/app/views/shared/sidebar/_apidocs.html.haml b/app/views/shared/sidebar/_apidocs.html.haml
new file mode 100644
index 0000000..bf7ff5b
--- /dev/null
+++ b/app/views/shared/sidebar/_apidocs.html.haml
@@ -0,0 +1,7 @@
+%h1 aclog API
+.sidebar-list
+ .list-group.list-group-scroll
+ - @apidocs.values.inject(:merge).each do |namespace, endpoints|
+ %span.list-group-head= namespace.titleize
+ - endpoints.reject {|_, e| e.route_nodoc || e.route_deprecated }.each do |action, endpoint|
+ = link_to_with_active format_endpoint_name(endpoint), about_api_endpoint_path(endpoint.route_method.downcase, namespace, action), class: "list-group-item"