lib/grape/api.rb in grape-0.6.0 vs lib/grape/api.rb in grape-0.6.1
- old
+ new
@@ -24,11 +24,11 @@
@routes = nil
reset_validations!
end
def compile
- @instance = self.new
+ @instance = new
end
def change!
@instance = nil
end
@@ -79,25 +79,25 @@
# @example API with legacy support.
# class MyAPI < Grape::API
# version 'v2'
#
# get '/main' do
- # {:some => 'data'}
+ # {some: 'data'}
# end
#
# version 'v1' do
# get '/main' do
- # {:legacy => 'data'}
+ # {legacy: 'data'}
# end
# end
# end
#
def version(*args, &block)
if args.any?
options = args.pop if args.last.is_a? Hash
options ||= {}
- options = {:using => :path}.merge!(options)
+ options = { using: :path }.merge(options)
raise Grape::Exceptions::MissingVendorOption.new if options[:using] == :header && !options.has_key?(:vendor)
@versions = versions | args
nest(block) do
@@ -109,11 +109,11 @@
@versions.last unless @versions.nil?
end
# Add a description to the next namespace or function.
def desc(description, options = {})
- @last_description = options.merge(:description => description)
+ @last_description = options.merge(description: description)
end
# Specify the default format for the API's serializers.
# May be `:json` or `:txt` (default).
def default_format(new_format = nil)
@@ -145,12 +145,17 @@
def parser(content_type, new_parser)
settings.imbue(:parsers, content_type.to_sym => new_parser)
end
# Specify a default error formatter.
- def default_error_formatter(new_formatter = nil)
- new_formatter ? set(:default_error_formatter, new_formatter) : settings[:default_error_formatter]
+ def default_error_formatter(new_formatter_name = nil)
+ if new_formatter_name
+ new_formatter = Grape::ErrorFormatter::Base.formatter_for(new_formatter_name, {})
+ set(:default_error_formatter, new_formatter)
+ else
+ settings[:default_error_formatter]
+ end
end
def error_formatter(format, options)
if options.is_a?(Hash) && options.has_key?(:with)
formatter = options[:with]
@@ -202,35 +207,37 @@
elsif block_given?
handler = block
end
options = args.last.is_a?(Hash) ? args.pop : {}
- if options.has_key?(:with)
- handler ||= proc { options[:with] }
- end
+ handler ||= proc { options[:with] } if options.has_key?(:with)
if handler
args.each do |arg|
imbue(:rescue_handlers, { arg => handler })
end
end
imbue(:rescue_options, options)
- set(:rescue_all, true) and return if args.include?(:all)
- imbue(:rescued_errors, args)
+
+ if args.include?(:all)
+ set(:rescue_all, true)
+ else
+ imbue(:rescued_errors, args)
+ end
end
# Allows you to specify a default representation entity for a
# class. This allows you to map your models to their respective
# entities once and then simply call `present` with the model.
#
# @example
# class ExampleAPI < Grape::API
- # represent User, :with => Entity::User
+ # represent User, with: Entity::User
#
# get '/me' do
- # present current_user # :with => Entity::User is assumed
+ # present current_user # with: Entity::User is assumed
# end
# end
#
# Note that Grape will automatically go up the class ancestry to
# try to find a representing entity, so if you, for example, define
@@ -269,11 +276,11 @@
if new_mod
mod.class_eval do
include new_mod
end
end
- mod.class_eval &block if block_given?
+ mod.class_eval(&block) if block_given?
set(:helpers, mod)
else
mod = Module.new
settings.stack.each do |s|
mod.send :include, s[:helpers] if s[:helpers]
@@ -285,11 +292,11 @@
# Add an authentication type to the API. Currently
# only `:http_basic`, `:http_digest` and `:oauth2` are supported.
def auth(type = nil, options = {}, &block)
if type
- set(:auth, {:type => type.to_sym, :proc => block}.merge(options))
+ set(:auth, { type: type.to_sym, proc: block }.merge(options))
else
settings[:auth]
end
end
@@ -311,18 +318,18 @@
def mount(mounts)
mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
mounts.each_pair do |app, path|
if app.respond_to?(:inherit_settings, true)
app_settings = settings.clone
- mount_path = Rack::Mount::Utils.normalize_path([ settings[:mount_path], path ].compact.join("/"))
+ mount_path = Rack::Mount::Utils.normalize_path([settings[:mount_path], path].compact.join("/"))
app_settings.set :mount_path, mount_path
app.inherit_settings(app_settings)
end
endpoints << Grape::Endpoint.new(settings.clone, {
- :method => :any,
- :path => path,
- :app => app
+ method: :any,
+ path: path,
+ app: app
})
end
end
# Defines a route that will be recognized
@@ -332,18 +339,18 @@
# @param paths [String] One or more strings representing the URL segment(s) for this route.
#
# @example Defining a basic route.
# class MyAPI < Grape::API
# route(:any, '/hello') do
- # {:hello => 'world'}
+ # {hello: 'world'}
# end
# end
def route(methods, paths = ['/'], route_options = {}, &block)
endpoint_options = {
- :method => methods,
- :path => paths,
- :route_options => (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
+ method: methods,
+ path: paths,
+ route_options: (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge(route_options || {})
}
endpoints << Grape::Endpoint.new(settings.clone, endpoint_options, &block)
@last_description = nil
reset_validations!
@@ -359,18 +366,38 @@
def after(&block)
imbue(:afters, [block])
end
- def get(paths = ['/'], options = {}, &block); route('GET', paths, options, &block) end
- def post(paths = ['/'], options = {}, &block); route('POST', paths, options, &block) end
- def put(paths = ['/'], options = {}, &block); route('PUT', paths, options, &block) end
- def head(paths = ['/'], options = {}, &block); route('HEAD', paths, options, &block) end
- def delete(paths = ['/'], options = {}, &block); route('DELETE', paths, options, &block) end
- def options(paths = ['/'], options = {}, &block); route('OPTIONS', paths, options, &block) end
- def patch(paths = ['/'], options = {}, &block); route('PATCH', paths, options, &block) end
+ def get(paths = ['/'], options = {}, &block)
+ route('GET', paths, options, &block)
+ end
+ def post(paths = ['/'], options = {}, &block)
+ route('POST', paths, options, &block)
+ end
+
+ def put(paths = ['/'], options = {}, &block)
+ route('PUT', paths, options, &block)
+ end
+
+ def head(paths = ['/'], options = {}, &block)
+ route('HEAD', paths, options, &block)
+ end
+
+ def delete(paths = ['/'], options = {}, &block)
+ route('DELETE', paths, options, &block)
+ end
+
+ def options(paths = ['/'], options = {}, &block)
+ route('OPTIONS', paths, options, &block)
+ end
+
+ def patch(paths = ['/'], options = {}, &block)
+ route('PATCH', paths, options, &block)
+ end
+
def namespace(space = nil, options = {}, &block)
if space || block_given?
previous_namespace_description = @namespace_description
@namespace_description = (@namespace_description || {}).deep_merge(@last_description || {})
@last_description = nil
@@ -420,11 +447,14 @@
# Retrieve an array of the middleware classes
# and arguments that are currently applied to the
# application.
def middleware
- settings.stack.inject([]){|a,s| a += s[:middleware] if s[:middleware]; a}
+ settings.stack.inject([]) do |a, s|
+ a += s[:middleware] if s[:middleware]
+ a
+ end
end
# An array of API routes.
def routes
@routes ||= prepare_routes
@@ -433,13 +463,15 @@
def versions
@versions ||= []
end
def cascade(value = nil)
- value.nil? ?
- (settings.has_key?(:cascade) ? !! settings[:cascade] : true) :
+ if value.nil?
+ settings.has_key?(:cascade) ? !!settings[:cascade] : true
+ else
set(:cascade, value)
+ end
end
protected
def prepare_routes
@@ -452,19 +484,19 @@
# Execute first the provided block, then each of the
# block passed in. Allows for simple 'before' setups
# of settings stack pushes.
def nest(*blocks, &block)
- blocks.reject!{|b| b.nil?}
+ blocks.reject! { |b| b.nil? }
if blocks.any?
settings.push # create a new context to eval the follow
- instance_eval &block if block_given?
- blocks.each{|b| instance_eval &b}
- settings.pop # when finished, we pop the context
+ instance_eval(&block) if block_given?
+ blocks.each { |b| instance_eval(&b) }
+ settings.pop # when finished, we pop the context
reset_validations!
else
- instance_eval &block
+ instance_eval(&block)
end
end
def inherited(subclass)
subclass.reset!
@@ -490,11 +522,11 @@
end
def call(env)
status, headers, body = @route_set.call(env)
headers.delete('X-Cascade') unless cascade?
- [ status, headers, body ]
+ [status, headers, body]
end
# Some requests may return a HTTP 404 error if grape cannot find a matching
# route. In this case, Rack::Mount adds a X-Cascade header to the response
# and sets it to 'pass', indicating to grape's parents they should keep
@@ -502,12 +534,12 @@
#
# In some applications (e.g. mounting grape on rails), one might need to trap
# errors from reaching upstream. This is effectivelly done by unsetting
# X-Cascade. Default :cascade is true.
def cascade?
- return !! self.class.settings[:cascade] if self.class.settings.has_key?(:cascade)
- return !! self.class.settings[:version_options][:cascade] if self.class.settings[:version_options] && self.class.settings[:version_options].has_key?(:cascade)
+ return !!self.class.settings[:cascade] if self.class.settings.has_key?(:cascade)
+ return !!self.class.settings[:version_options][:cascade] if self.class.settings[:version_options] && self.class.settings[:version_options].has_key?(:cascade)
true
end
reset!
@@ -516,35 +548,37 @@
# For every resource add a 'OPTIONS' route that returns an HTTP 204 response
# with a list of HTTP methods that can be called. Also add a route that
# will return an HTTP 405 response for any HTTP method that the resource
# cannot handle.
def add_head_not_allowed_methods
- allowed_methods = Hash.new{|h,k| h[k] = [] }
- resources = self.class.endpoints.map do |endpoint|
- endpoint.options[:app] && endpoint.options[:app].respond_to?(:endpoints) ?
- endpoint.options[:app].endpoints.map(&:routes) :
+ allowed_methods = Hash.new { |h, k| h[k] = [] }
+ resources = self.class.endpoints.map do |endpoint|
+ if endpoint.options[:app] && endpoint.options[:app].respond_to?(:endpoints)
+ endpoint.options[:app].endpoints.map(&:routes)
+ else
endpoint.routes
+ end
end
resources.flatten.each do |route|
allowed_methods[route.route_compiled] << route.route_method
end
allowed_methods.each do |path_info, methods|
- if methods.include?('GET') && ! methods.include?("HEAD") && ! self.class.settings[:do_not_route_head]
- methods = methods | [ 'HEAD' ]
+ if methods.include?('GET') && !methods.include?("HEAD") && !self.class.settings[:do_not_route_head]
+ methods = methods | ['HEAD']
end
allow_header = (["OPTIONS"] | methods).join(", ")
unless methods.include?("OPTIONS") || self.class.settings[:do_not_route_options]
- @route_set.add_route( proc { [204, { 'Allow' => allow_header }, []]}, {
- :path_info => path_info,
- :request_method => "OPTIONS"
+ @route_set.add_route(proc { [204, { 'Allow' => allow_header }, []] }, {
+ path_info: path_info,
+ request_method: "OPTIONS"
})
end
not_allowed_methods = %w(GET PUT POST DELETE PATCH HEAD) - methods
not_allowed_methods << "OPTIONS" if self.class.settings[:do_not_route_options]
not_allowed_methods.each do |bad_method|
- @route_set.add_route( proc { [405, { 'Allow' => allow_header, 'Content-Type' => 'text/plain' }, []]}, {
- :path_info => path_info,
- :request_method => bad_method
+ @route_set.add_route(proc { [405, { 'Allow' => allow_header, 'Content-Type' => 'text/plain' }, []] }, {
+ path_info: path_info,
+ request_method: bad_method
})
end
end
end