Class: Grape::Endpoint
- Inherits:
-
Object
- Object
- Grape::Endpoint
- Defined in:
- lib/grape/endpoint.rb
Overview
An Endpoint is the proxy scope in which all routing blocks are executed. In other words, any methods on the instance level of this class may be called from inside a ‘get`, `post`, etc. block.
Class Attribute Summary collapse
-
.block ⇒ Object
Returns the value of attribute block.
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#error!(message, status = 403) ⇒ Object
End the request and display an error to the end user with the specified message.
-
#header(key = nil, val = nil) ⇒ Object
Set an individual header or retrieve all headers that have been set.
-
#params ⇒ Object
The parameters passed into the request as well as parsed from URL segments.
-
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
-
#version ⇒ Object
The API version as specified in the URL.
Class Attribute Details
.block ⇒ Object
Returns the value of attribute block.
19 20 21 |
# File 'lib/grape/endpoint.rb', line 19 def block @block end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
26 27 28 |
# File 'lib/grape/endpoint.rb', line 26 def env @env end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
26 27 28 |
# File 'lib/grape/endpoint.rb', line 26 def request @request end |
Class Method Details
.call(env) ⇒ Object
22 23 24 |
# File 'lib/grape/endpoint.rb', line 22 def self.call(env) new.call(env) end |
Instance Method Details
#call(env) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/grape/endpoint.rb', line 77 def call(env) @env = env @header = {} @request = Rack::Request.new(@env) response_text = instance_eval &self.class.block [status, header, [response_text]] end |
#error!(message, status = 403) ⇒ Object
End the request and display an error to the end user with the specified message.
46 47 48 |
# File 'lib/grape/endpoint.rb', line 46 def error!(, status=403) throw :error, :message => , :status => status end |
#header(key = nil, val = nil) ⇒ Object
Set an individual header or retrieve all headers that have been set.
69 70 71 72 73 74 75 |
# File 'lib/grape/endpoint.rb', line 69 def header(key = nil, val = nil) if key val ? @header[key.to_s] = val : @header.delete(key.to_s) else @header end end |
#params ⇒ Object
The parameters passed into the request as well as parsed from URL segments.
30 31 32 33 34 35 36 |
# File 'lib/grape/endpoint.rb', line 30 def params @params ||= request.params.merge(env['rack.routing_args'] || {}).inject({}) do |h,(k,v)| h[k.to_s] = v h[k.to_sym] = v h end end |
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/grape/endpoint.rb', line 53 def status(status = nil) if status @status = status else return @status if @status case request.request_method.to_s.upcase when 'POST' 201 else 200 end end end |
#version ⇒ Object
The API version as specified in the URL.
39 |
# File 'lib/grape/endpoint.rb', line 39 def version; env['api.version'] end |