Class: Grape::API
- Inherits:
-
Object
- Object
- Grape::API
- Defined in:
- lib/grape/api.rb
Defined Under Namespace
Modules: Helpers
Class Attribute Summary collapse
-
.route_set ⇒ Object
readonly
Returns the value of attribute route_set.
Class Method Summary collapse
- .auth(type = nil, options = {}, &block) ⇒ Object
- .build_endpoint(&block) ⇒ Object
- .call(env) ⇒ Object
- .compile_path(path) ⇒ Object
- .default_format(new_format = nil) ⇒ Object
- .delete(path_info = '', &block) ⇒ Object
- .get(path_info = '', &block) ⇒ Object
- .head(path_info = '', &block) ⇒ Object
-
.helpers(&block) ⇒ Object
Add helper methods that will be accessible from any endpoint within this namespace (and child namespaces).
-
.http_basic(options = {}, &block) ⇒ Object
Add HTTP Basic authorization to the API.
- .inherited(subclass) ⇒ Object
- .namespace(space = nil, &block) ⇒ Object (also: group, resource, resources)
-
.nest(*blocks, &block) ⇒ Object
Execute first the provided block, then each of the block passed in.
- .post(path_info = '', &block) ⇒ Object
-
.prefix(prefix = nil) ⇒ Object
Define a root prefix for your entire API.
- .put(path_info = '', &block) ⇒ Object
- .reset! ⇒ Object
- .route(method, path_info, &block) ⇒ Object
- .set(key, value) ⇒ Object
-
.settings ⇒ Object
Settings are a stack, so when we want to access them they are merged in the order pushed.
- .settings_stack ⇒ Object
- .version(*new_versions, &block) ⇒ Object
Class Attribute Details
.route_set ⇒ Object (readonly)
Returns the value of attribute route_set.
9 10 11 |
# File 'lib/grape/api.rb', line 9 def route_set @route_set end |
Class Method Details
.auth(type = nil, options = {}, &block) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/grape/api.rb', line 79 def auth(type = nil, = {}, &block) if type set(:auth, {:type => type.to_sym, :proc => block}.merge()) else settings[:auth] end end |
.build_endpoint(&block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/grape/api.rb', line 116 def build_endpoint(&block) b = Rack::Builder.new b.use Grape::Middleware::Error b.use Rack::Auth::Basic, settings[:auth][:realm], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic 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 endpoint = Grape::Endpoint.new(&block) endpoint.send :extend, helpers b.run endpoint b.to_app end |
.call(env) ⇒ Object
17 18 19 20 |
# File 'lib/grape/api.rb', line 17 def call(env) puts "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}" route_set.freeze.call(env) end |
.compile_path(path) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/grape/api.rb', line 100 def compile_path(path) parts = [] parts << prefix if prefix parts << ':version' if version parts << namespace if namespace parts << path Rack::Mount::Utils.normalize_path(parts.join('/')) end |
.default_format(new_format = nil) ⇒ Object
51 52 53 |
# File 'lib/grape/api.rb', line 51 def default_format(new_format = nil) new_format ? set(:default_format, new_format.to_sym) : settings[:default_format] end |
.delete(path_info = '', &block) ⇒ Object
136 |
# File 'lib/grape/api.rb', line 136 def delete(path_info = '', &block); route('DELETE', path_info, &block) end |
.get(path_info = '', &block) ⇒ Object
132 |
# File 'lib/grape/api.rb', line 132 def get(path_info = '', &block); route('GET', path_info, &block) end |
.head(path_info = '', &block) ⇒ Object
135 |
# File 'lib/grape/api.rb', line 135 def head(path_info = '', &block); route('HEAD', path_info, &block) end |
.helpers(&block) ⇒ Object
Add helper methods that will be accessible from any endpoint within this namespace (and child namespaces).
class ExampleAPI
helpers do
def current_user
User.find_by_id(params[:token])
end
end
end
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/grape/api.rb', line 65 def helpers(&block) if block_given? m = settings_stack.last[:helpers] || Module.new m.class_eval &block set(:helpers, m) else m = Module.new settings_stack.each do |s| m.send :include, s[:helpers] if s[:helpers] end m end end |
.http_basic(options = {}, &block) ⇒ Object
Add HTTP Basic authorization to the API.
91 92 93 94 |
# File 'lib/grape/api.rb', line 91 def http_basic( = {}, &block) [:realm] ||= "API Authorization" auth :http_basic, , &block end |
.inherited(subclass) ⇒ Object
167 168 169 |
# File 'lib/grape/api.rb', line 167 def inherited(subclass) subclass.reset! end |
.namespace(space = nil, &block) ⇒ Object Also known as: group, resource, resources
138 139 140 141 142 143 144 145 146 |
# File 'lib/grape/api.rb', line 138 def namespace(space = nil, &block) if space || block_given? nest(block) do set(:namespace, space.to_s) if space end else Rack::Mount::Utils.normalize_path(settings_stack.map{|s| s[:namespace]}.join('/')) end end |
.nest(*blocks, &block) ⇒ Object
Execute first the provided block, then each of the block passed in. Allows for simple ‘before’ setups of settings stack pushes.
151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/grape/api.rb', line 151 def nest(*blocks, &block) blocks.reject!{|b| b.nil?} if blocks.any? settings_stack << {} instance_eval &block blocks.each{|b| instance_eval &b} settings_stack.pop else instance_eval &block end end |
.post(path_info = '', &block) ⇒ Object
133 |
# File 'lib/grape/api.rb', line 133 def post(path_info = '', &block); route('POST', path_info, &block) end |
.prefix(prefix = nil) ⇒ Object
Define a root prefix for your entire API. For instance, if you had an api that you wanted to be namespaced at ‘/api/` you would do this:
prefix '/api'
43 44 45 |
# File 'lib/grape/api.rb', line 43 def prefix(prefix = nil) prefix ? set(:root_prefix, prefix) : settings[:root_prefix] end |
.put(path_info = '', &block) ⇒ Object
134 |
# File 'lib/grape/api.rb', line 134 def put(path_info = '', &block); route('PUT', path_info, &block) end |
.reset! ⇒ Object
11 12 13 14 15 |
# File 'lib/grape/api.rb', line 11 def reset! @settings = [{}] @route_set = Rack::Mount::RouteSet.new @prototype = nil end |
.route(method, path_info, &block) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/grape/api.rb', line 109 def route(method, path_info, &block) route_set.add_route(build_endpoint(&block), :path_info => Rack::Mount::Strexp.compile(compile_path(path_info)), :request_method => method ) end |
.set(key, value) ⇒ Object
33 34 35 |
# File 'lib/grape/api.rb', line 33 def set(key, value) @settings.last[key.to_sym] = value end |
.settings ⇒ Object
Settings are a stack, so when we want to access them they are merged in the order pushed.
25 26 27 |
# File 'lib/grape/api.rb', line 25 def settings @settings.inject({}){|f,h| f.merge!(h); f} end |
.settings_stack ⇒ Object
29 30 31 |
# File 'lib/grape/api.rb', line 29 def settings_stack @settings end |
.version(*new_versions, &block) ⇒ Object
47 48 49 |
# File 'lib/grape/api.rb', line 47 def version(*new_versions, &block) new_versions.any? ? nest(block){ set(:version, new_versions) } : settings[:version] end |