lib/grape/api.rb in grape-0.1.4 vs lib/grape/api.rb in grape-0.1.5

- old
+ new

@@ -1,7 +1,8 @@ require 'rack/mount' require 'rack/auth/basic' +require 'rack/auth/digest/md5' require 'logger' module Grape # The API class is the primary entry point for # creating Grape APIs. Users should subclass this @@ -48,11 +49,11 @@ # Define a root URL prefix for your entire # API. def prefix(prefix = nil) prefix ? set(:root_prefix, prefix) : settings[:root_prefix] end - + # Specify an API version. # # @example API with legacy support. # class MyAPI < Grape::API # version 'v2' @@ -77,10 +78,43 @@ # supported. def default_format(new_format = nil) new_format ? set(:default_format, new_format.to_sym) : settings[:default_format] end + # Specify the format for error messages. + # May be `:json` or `:txt` (default). + def error_format(new_format = nil) + new_format ? set(:error_format, new_format.to_sym) : settings[:error_format] + end + + # Specify the default status code for errors. + def default_error_status(new_status = nil) + new_status ? set(:default_error_status, new_status) : settings[:default_error_status] + end + + # Allows you to rescue certain exceptions that occur to return + # a grape error rather than raising all the way to the + # server level. + # + # @example Rescue from custom exceptions + # class ExampleAPI < Grape::API + # class CustomError < StandardError; end + # + # rescue_from CustomError + # end + # + # @overload rescue_from(*exception_classes, options = {}) + # @param [Array] exception_classes A list of classes that you want to rescue, or + # the symbol :all to rescue from all exceptions. + # @param [Hash] options Options for the rescue usage. + # @option options [Boolean] :backtrace Include a backtrace in the rescue response. + def rescue_from(*args) + set(:rescue_options, args.pop) if args.last.is_a?(Hash) + set(:rescue_all, true) and return if args.include?(:all) + set(:rescued_errors, args) + end + # Add helper methods that will be accessible from any # endpoint within this namespace (and child namespaces). # # @example Define some helpers. # class ExampleAPI < Grape::API @@ -103,11 +137,11 @@ m end end # Add an authentication type to the API. Currently - # only `:http_basic` is supported. + # 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)) else settings[:auth] @@ -120,10 +154,16 @@ # @option options [String] :realm "API Authorization" The HTTP Basic realm. def http_basic(options = {}, &block) options[:realm] ||= "API Authorization" auth :http_basic, options, &block end + + def http_digest(options = {}, &block) + options[:realm] ||= "API Authorization" + options[:opaque] ||= "secret" + auth :http_digest, options, &block + end # Defines a route that will be recognized # by the Grape API. # # @param methods [HTTP Verb] One or more HTTP verbs that are accepted by this route. Set to `:any` if you want any verb to be accepted. @@ -215,11 +255,17 @@ end end def build_endpoint(&block) b = Rack::Builder.new - b.use Grape::Middleware::Error + b.use Grape::Middleware::Error, + :default_status => settings[:default_error_status] || 403, + :rescue_all => settings[:rescue_all], + :rescued_errors => settings[:rescued_errors], + :format => settings[:error_format] || :txt, + :rescue_options => settings[:rescue_options] b.use Rack::Auth::Basic, settings[:auth][:realm], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic + b.use Rack::Auth::Digest::MD5, settings[:auth][:realm], settings[:auth][:opaque], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_digest b.use Grape::Middleware::Prefixer, :prefix => prefix if prefix b.use Grape::Middleware::Versioner, :versions => (version if version.is_a?(Array)) if version b.use Grape::Middleware::Formatter, :default_format => default_format || :json middleware.each{|m| b.use *m }