Class: Hyperdrive::HATEOAS

Inherits:
Object
  • Object
show all
Extended by:
Values
Defined in:
lib/hyperdrive/hateoas.rb

Class Method Summary collapse

Methods included from Values

default_config, default_cors_options, default_headers, definable_request_methods, http_request_methods, request_methods, supported_request_methods

Class Method Details

.body ⇒ Object



53
54
55
56
57
58
59
# File 'lib/hyperdrive/hateoas.rb', line 53

def self.body
  if content_type =~ /json$/
    MultiJson.dump(response)
  else
    raise Errors::NotAcceptable.new(@env['HTTP_ACCEPT'])
  end
end

.call(env) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/hyperdrive/hateoas.rb', line 7

def self.call(env)
  @env = env
  if hyperdrive.resources.empty? || env['PATH_INFO'] != '/'
    raise Hyperdrive::Errors::NotFound
  end

  [200, headers, [body]]
end

.content_type ⇒ Object



24
25
26
# File 'lib/hyperdrive/hateoas.rb', line 24

def self.content_type
  @env['hyperdrive.accept'].best_of(media_types)
end

.endpoints ⇒ Object



37
38
39
40
41
# File 'lib/hyperdrive/hateoas.rb', line 37

def self.endpoints
  hyperdrive.resources.map do |_,resource|
    resource.to_hash
  end
end

.headers ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/hyperdrive/hateoas.rb', line 28

def self.headers
  {
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Allow-Methods' => 'GET, HEAD, OPTIONS',
    'Allow' => 'GET, HEAD, OPTIONS',
    'Content-Type' => content_type
  }
end

.media_types ⇒ Object



18
19
20
21
22
# File 'lib/hyperdrive/hateoas.rb', line 18

def self.media_types
  %w(hal+json json).map do |media_type|
    "application/vnd.#{hyperdrive.config[:vendor]}+#{media_type}"
  end + %w(application/hal+json application/json)
end

.response ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/hyperdrive/hateoas.rb', line 43

def self.response
  {
    _links: { self: { href: '/' } },
    name: hyperdrive.config[:name],
    description: hyperdrive.config[:description],
    vendor: hyperdrive.config[:vendor],
    resources: endpoints
  }
end