Skip to content

Commit

Permalink
Require Grape 0.16.1 and fix route_xxx deprecations.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Apr 4, 2016
1 parent a3cf046 commit 61a0fdd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 60 deletions.
25 changes: 15 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-02-07 23:26:32 +0900 using RuboCop version 0.33.0.
# on 2016-04-04 12:53:30 -0400 using RuboCop version 0.33.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 14
Metrics/AbcSize:
Max: 214
Max: 220

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 150
Max: 149

# Offense count: 7
Metrics/CyclomaticComplexity:
Max: 39
Max: 42

# Offense count: 354
# Offense count: 360
# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 242
Max: 206

# Offense count: 24
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 169
Max: 175

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 472
Max: 478

# Offense count: 5
Metrics/PerceivedComplexity:
Max: 40
Max: 44

# Offense count: 8
Style/ClassVars:
Exclude:
- 'example/api.rb'
- 'lib/grape-swagger/doc_methods.rb'

# Offense count: 95
# Offense count: 96
Style/Documentation:
Enabled: false

Expand All @@ -61,6 +61,11 @@ Style/FileName:
- 'spec/grape-swagger_helper_spec.rb'
- 'spec/grape-swagger_spec.rb'

# Offense count: 1
Style/MultilineBlockChain:
Exclude:
- 'lib/grape-swagger/doc_methods.rb'

# Offense count: 1
# Configuration parameters: NamePrefix, NamePrefixBlacklist.
Style/PredicateName:
Expand Down
9 changes: 1 addition & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@ rvm:
- rbx-2

env:
- GRAPE_VERSION=0.8.0
- GRAPE_VERSION=0.9.0
- GRAPE_VERSION=0.10.0
- GRAPE_VERSION=0.10.1
- GRAPE_VERSION=0.11.0
- GRAPE_VERSION=0.12.0
- GRAPE_VERSION=0.13.0
- GRAPE_VERSION=0.14.0
- GRAPE_VERSION=0.16.1
- GRAPE_VERSION=HEAD

matrix:
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source 'http://rubygems.org'

gemspec

case version = ENV['GRAPE_VERSION'] || '~> 0.9.0'
case version = ENV['GRAPE_VERSION'] || '~> 0.16.1'
when 'HEAD'
gem 'grape', github: 'ruby-grape/grape'
else
Expand Down
2 changes: 1 addition & 1 deletion grape-swagger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Gem::Specification.new do |s|
s.summary = 'A simple way to add auto generated documentation to your Grape API that can be displayed with Swagger.'
s.license = 'MIT'

s.add_runtime_dependency 'grape', '>= 0.8.0'
s.add_runtime_dependency 'grape', '>= 0.16.1'
s.add_runtime_dependency 'grape-entity', '< 0.5.0'

s.add_development_dependency 'rake'
Expand Down
8 changes: 4 additions & 4 deletions lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def add_swagger_documentation(options = {})

@target_class.combined_routes = {}
@target_class.routes.each do |route|
route_path = route.route_path
route_match = route_path.split(/^.*?#{route.route_prefix.to_s}/).last
route_path = route.path
route_match = route_path.split(/^.*?#{route.prefix.to_s}/).last
next unless route_match
route_match = route_match.match('\/([\w|-]*?)[\.\/\(]') || route_match.match('\/([\w|-]*)$')
next unless route_match
resource = route_match.captures.first
next if resource.empty?
@target_class.combined_routes[resource] ||= []
next if documentation_class.hide_documentation_path && route.route_path.match(/#{documentation_class.mount_path}($|\/|\(\.)/)
next if documentation_class.hide_documentation_path && route.path.match(/#{documentation_class.mount_path}($|\/|\(\.)/)
@target_class.combined_routes[resource] << route
end

Expand Down Expand Up @@ -77,7 +77,7 @@ def combine_namespace_routes(namespaces)
parent_route = @target_class.combined_routes[parent_route_name]
# fetch all routes that are within the current namespace
namespace_routes = parent_route.collect do |route|
route if (route.route_path.start_with?(route.route_prefix ? "/#{route.route_prefix}/#{name}" : "/#{name}") || route.route_path.start_with?((route.route_prefix ? "/#{route.route_prefix}/:version/#{name}" : "/:version/#{name}"))) &&
route if (route.path.start_with?(route.prefix ? "/#{route.prefix}/#{name}" : "/#{name}") || route.path.start_with?((route.prefix ? "/#{route.prefix}/:version/#{name}" : "/:version/#{name}"))) &&
(route.instance_variable_get(:@options)[:namespace] == "/#{name}" || route.instance_variable_get(:@options)[:namespace] == "/:version/#{name}")
end.compact

Expand Down
45 changes: 27 additions & 18 deletions lib/grape-swagger/doc_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,9 @@ def setup(options)
end

namespace_routes_array = namespace_routes.keys.map do |local_route|
next if namespace_routes[local_route].map(&:route_hidden).all? { |value| value.respond_to?(:call) ? value.call : value }
next if namespace_routes[local_route].map do |route|
route.settings[:description] && route.settings[:description][:hidden]
end.all? { |value| value.respond_to?(:call) ? value.call : value }

url_format = '.{format}' unless @@hide_format
url_locale = "?locale=#{params[:locale]}" unless params[:locale].blank?
Expand Down Expand Up @@ -488,11 +490,12 @@ def setup(options)
error!('Not Found', 404) unless routes

visible_ops = routes.reject do |route|
route.route_hidden.respond_to?(:call) ? route.route_hidden.call : route.route_hidden
hidden = route.route_hidden
hidden && hidden.respond_to?(:call) ? hidden.call : hidden
end

ops = visible_ops.group_by do |route|
@@documentation_class.parse_path(route.route_path, api_version)
@@documentation_class.parse_path(route.path, api_version)
end

error!('Not Found', 404) unless ops.any?
Expand All @@ -501,51 +504,57 @@ def setup(options)

ops.each do |path, op_routes|
operations = op_routes.map do |route|
route_settings_description = route.settings[:description] || {}
endpoint = target_class.endpoint_mapping[route.to_s.sub('(.:format)', '')]
endpoint_path = endpoint.options[:path] unless endpoint.nil?
i18n_key = [route.route_namespace, endpoint_path, route.route_method.downcase].flatten.join('/')
i18n_key = [route.namespace, endpoint_path, route.request_method.downcase].flatten.join('/')
i18n_key = i18n_key.split('/').reject(&:empty?).join('.')

summary = @@documentation_class.translate(
route.route_description, i18n_scope,
route.description, i18n_scope,
[:"#{i18n_key}.desc", :"#{i18n_key}.description"]
)
notes = @@documentation_class.translate(
route.route_detail || route.route_notes, i18n_scope,
route_settings_description[:detail] || route_settings_description[:notes], i18n_scope,
[:"#{i18n_key}.detail", :"#{i18n_key}.notes"]
)
notes = @@documentation_class.as_markdown(notes)

http_codes = @@documentation_class.parse_http_codes(route.route_http_codes, models)
http_codes = @@documentation_class.parse_http_codes(route.http_codes, models)

models.merge(Array(route.route_entity)) if route.route_entity.present?
models.merge(Array(route.entity)) if route.entity.present?

route_settings_description = route.settings[:description] || {}

operation = {
notes: notes.to_s,
summary: summary,
nickname: route.route_nickname || (route.route_method + route.route_path.gsub(/[\/:\(\)\.]/, '-')),
method: route.route_method,
parameters: @@documentation_class.parse_header_params(route.route_headers, scope: i18n_scope, key: i18n_key) +
@@documentation_class.parse_params(route.route_params, route.route_path, route.route_method,
nickname: route_settings_description[:nickname] || (route.request_method + route.path.gsub(/[\/:\(\)\.]/, '-')),
method: route.request_method,
parameters: @@documentation_class.parse_header_params(route.headers, scope: i18n_scope, key: i18n_key) +
@@documentation_class.parse_params(route.route_params, route.path, route.request_method,
scope: i18n_scope, key: i18n_key),
type: route.route_is_array ? 'array' : 'void'
type: route_settings_description[:is_array] ? 'array' : 'void'
}
operation[:authorizations] = route.route_authorizations unless route.route_authorizations.nil? || route.route_authorizations.empty?

authorizations = route_settings_description[:authorizations]
operation[:authorizations] = authorizations if authorizations && authorizations.any?

if operation[:parameters].any? { |param| param[:type] == 'File' }
operation.merge!(consumes: ['multipart/form-data'])
end
operation.merge!(responseMessages: http_codes) unless http_codes.empty?

if route.route_entity
type = @@documentation_class.parse_entity_name(Array(route.route_entity).first)
if route.route_is_array
if route.entity
type = @@documentation_class.parse_entity_name(Array(route.entity).first)
if route_settings_description[:is_array]
operation.merge!(items: { '$ref' => type })
else
operation.merge!(type: type)
end
end

operation[:nickname] = route.route_nickname if route.route_nickname
operation[:nickname] = route_settings_description[:nickname] if route_settings_description.key?(:nickname)
operation
end.compact
apis << {
Expand Down
22 changes: 11 additions & 11 deletions spec/api_description_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
it 'describes the API with defaults' do
routes = subject.endpoints.first.routes
expect(routes.count).to eq 2
expect(routes.first.route_description).to eq 'Swagger compatible API description'
expect(routes.first.route_params).to eq('locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
expect(routes.last.route_description).to eq 'Swagger compatible API description for specific API'
expect(routes.last.route_params).to eq('name' => { desc: 'Resource name of mounted API', type: 'String', required: true },
'locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
expect(routes.first.description).to eq 'Swagger compatible API description'
expect(routes.first.params).to eq('locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
expect(routes.last.description).to eq 'Swagger compatible API description for specific API'
expect(routes.last.params).to eq('name' => { desc: 'Resource name of mounted API', type: 'String', required: true },
'locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
end
end

Expand All @@ -31,12 +31,12 @@
it 'describes the API with defaults' do
routes = subject.endpoints.first.routes
expect(routes.count).to eq 2
expect(routes.first.route_description).to eq 'First'
expect(routes.first.route_params).to eq(x: 1, 'locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
expect(routes.first.route_xx).to eq(11)
expect(routes.last.route_description).to eq 'Second'
expect(routes.last.route_params).to eq('name' => { desc: 'Resource name of mounted API', type: 'String', required: true }, y: 42,
'locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
expect(routes.first.description).to eq 'First'
expect(routes.first.params).to eq(x: 1, 'locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
expect(routes.first.settings[:description][:xx]).to eq(11)
expect(routes.last.description).to eq 'Second'
expect(routes.last.params).to eq('name' => { desc: 'Resource name of mounted API', type: 'String', required: true }, y: 42,
'locale' => { desc: 'Locale of API documentation', type: 'Symbol', required: false })
expect(routes.last.route_yy).to eq(4242)
end
end
Expand Down
14 changes: 7 additions & 7 deletions spec/i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,17 @@ def api_translations
summary: 'Finds user by id', notes: ''
)
expect(result[:apis][api_index][:operations][0][:parameters]).to eq [
{ paramType: 'path', name: 'id', description: 'User id', type: 'string', required: true, allowMultiple: false },
{ paramType: 'query', name: 'locale', description: "Used to change locale of endpoint's responding message", type: 'string', required: false, allowMultiple: false }
{ paramType: 'query', name: 'locale', description: "Used to change locale of endpoint's responding message", type: 'string', required: false, allowMultiple: false },
{ paramType: 'path', name: 'id', description: 'User id', type: 'string', required: true, allowMultiple: false }
]

api_index += 1
expect(result[:apis][api_index][:operations][0]).to include(
summary: "Changes a user's email", notes: ''
)
expect(result[:apis][api_index][:operations][0][:parameters]).to eq [
{ paramType: 'path', name: 'id', description: 'User id', type: 'string', required: true, allowMultiple: false },
{ paramType: 'form', name: 'locale', description: "Used to change locale of endpoint's responding message", type: 'string', required: false, allowMultiple: false },
{ paramType: 'path', name: 'id', description: 'User id', type: 'string', required: true, allowMultiple: false },
{ paramType: 'form', name: 'email', description: 'A new email', type: 'string', required: true, allowMultiple: false }
]

Expand All @@ -183,8 +183,8 @@ def api_translations
summary: "Gets the strength estimation of a user's password", notes: 'The estimation is done by a well-known algorithm when he changed his password'
)
expect(result[:apis][api_index][:operations][0][:parameters]).to eq [
{ paramType: 'path', name: 'id', description: 'User id', type: 'string', required: true, allowMultiple: false },
{ paramType: 'query', name: 'locale', description: "Used to change locale of endpoint's responding message", type: 'string', required: false, allowMultiple: false }
{ paramType: 'query', name: 'locale', description: "Used to change locale of endpoint's responding message", type: 'string', required: false, allowMultiple: false },
{ paramType: 'path', name: 'id', description: 'User id', type: 'string', required: true, allowMultiple: false }
]
end

Expand All @@ -207,8 +207,8 @@ def api_translations
summary: 'Gets specific resource API document'
)
expect(result[:apis][api_index][:operations][0][:parameters]).to eq [
{ paramType: 'path', name: 'name', description: 'Resource name', type: 'string', required: true, allowMultiple: false },
{ paramType: 'query', name: 'locale', description: "Used to change locale of endpoint's responding message", type: 'string', required: false, allowMultiple: false }
{ paramType: 'query', name: 'locale', description: "Used to change locale of endpoint's responding message", type: 'string', required: false, allowMultiple: false },
{ paramType: 'path', name: 'name', description: 'Resource name', type: 'string', required: true, allowMultiple: false }
]
end
end
Expand Down

0 comments on commit 61a0fdd

Please sign in to comment.