Class: Grape::Endpoint

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.blockObject

Returns the value of attribute block.



19
20
21
# File 'lib/grape/endpoint.rb', line 19

def block
  @block
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



26
27
28
# File 'lib/grape/endpoint.rb', line 26

def env
  @env
end

#requestObject (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

.generate(&block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/grape/endpoint.rb', line 10

def self.generate(&block)
  c = Class.new(Grape::Endpoint)
  c.class_eval do
    @block = block
  end
  c
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.

Parameters:

  • message (String)

    The message to display.

  • status (Integer) (defaults to: 403)

    the HTTP Status Code. Defaults to 403.



46
47
48
# File 'lib/grape/endpoint.rb', line 46

def error!(message, status=403)
  throw :error, :message => 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

#paramsObject

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.

Parameters:

  • status (Integer) (defaults to: nil)

    The HTTP Status Code to return for this request.



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

#versionObject

The API version as specified in the URL.



39
# File 'lib/grape/endpoint.rb', line 39

def version; env['api.version'] end