Class: Hyperdrive::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperdrive/endpoint.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.env ⇒ Object (readonly)

Returns the value of attribute env.



21
22
23
# File 'lib/hyperdrive/endpoint.rb', line 21

def env
  @env
end

.headers ⇒ Object

Returns the value of attribute headers.



22
23
24
# File 'lib/hyperdrive/endpoint.rb', line 22

def headers
  @headers
end

.media_type ⇒ Object (readonly)

Returns the value of attribute media_type.



21
22
23
# File 'lib/hyperdrive/endpoint.rb', line 21

def media_type
  @media_type
end

.params ⇒ Object (readonly)

Returns the value of attribute params.



21
22
23
# File 'lib/hyperdrive/endpoint.rb', line 21

def params
  @params
end

.request ⇒ Object (readonly)

Returns the value of attribute request.



21
22
23
# File 'lib/hyperdrive/endpoint.rb', line 21

def request
  @request
end

.resource ⇒ Object (readonly)

Returns the value of attribute resource.



21
22
23
# File 'lib/hyperdrive/endpoint.rb', line 21

def resource
  @resource
end

Class Method Details

.body ⇒ Object



73
74
75
76
77
# File 'lib/hyperdrive/endpoint.rb', line 73

def self.body
  body = instance_eval(&resource.request_handler(env['REQUEST_METHOD']))
  body = '' if env['REQUEST_METHOD'] == 'DELETE'
  body
end

.call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/hyperdrive/endpoint.rb', line 6

def self.call(env)
  @env = env
  @request = Rack::Request.new(@env)
  @media_type = env['hyperdrive.media_type']
  @params = env['hyperdrive.params']
  @resource = env['hyperdrive.resource']
  @headers = Hyperdrive::Values.default_headers.dup
  @headers.merge!('Allow' => resource.allowed_methods.join(', '), 'Content-Type' => @media_type)

  response.finish
end

.error(status, message) ⇒ Object

Raises:



58
59
60
# File 'lib/hyperdrive/endpoint.rb', line 58

def self.error(status, message)
  raise Errors::HTTPError.new(message, status)
end

.json? ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/hyperdrive/endpoint.rb', line 25

def self.json?
  media_type =~ /json$/
end

.page ⇒ Object



33
34
35
# File 'lib/hyperdrive/endpoint.rb', line 33

def self.page
  env['hyperdrive.page']
end

.per_page ⇒ Object



37
38
39
# File 'lib/hyperdrive/endpoint.rb', line 37

def self.per_page
  env['hyperdrive.per_page']
end

.render(body) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hyperdrive/endpoint.rb', line 41

def self.render(body)
  case body
  when Array, Hash
    if json?
      MultiJson.dump(body)
    else
      env.logger.error "ENDPOINT: Can't serialize response automatically"
      raise Errors::NoResponse.new
    end
  when String
    body
  else
    env.logger.debug "ENDPOINT: Coerceing response to string. Probably not what you want"
    body.to_s
  end
end

.response ⇒ Object



79
80
81
# File 'lib/hyperdrive/endpoint.rb', line 79

def self.response
  ::Rack::Response.new(render(body), status, headers)
end

.status ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/hyperdrive/endpoint.rb', line 62

def self.status
  case env['REQUEST_METHOD']
  when 'POST'
    201
  when 'DELETE'
    204
  else
    200
  end
end

.xml? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/hyperdrive/endpoint.rb', line 29

def self.xml?
  media_type =~ /xml$/
end