Class: Grape::Hal::Endpoint

Inherits:
Object
  • Object
show all
Includes:
Dsl
Defined in:
lib/grape/hal/endpoint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_api, context_path) ⇒ Endpoint

Returns a new instance of Endpoint.



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

def initialize(current_api, context_path)
  @current_api = current_api
  @context_path = context_path
  @entries = []
  add_self
end

Instance Attribute Details

#context_pathObject (readonly)

Returns the value of attribute context_path.



7
8
9
# File 'lib/grape/hal/endpoint.rb', line 7

def context_path
  @context_path
end

#current_apiObject (readonly)

Returns the value of attribute current_api.



7
8
9
# File 'lib/grape/hal/endpoint.rb', line 7

def current_api
  @current_api
end

#entriesObject (readonly)

Returns the value of attribute entries.



7
8
9
# File 'lib/grape/hal/endpoint.rb', line 7

def entries
  @entries
end

Instance Method Details

#generate_hal(base_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/grape/hal/endpoint.rb', line 38

def generate_hal(base_path)
  hal_array = entries.map do |entry|
    key = entry[:rel] || entry[:path]

    value = {href: get_full_path(base_path, entry)}
    value[:title] = entry[:title] if entry[:title]
    value[:templated] = true if is_templated?(entry)
    [key, value]
  end
  {:_links => Hash[hal_array]}
end

#hal_for(path, options = {}, &block) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/grape/hal/endpoint.rb', line 16

def hal_for(path, options = {}, &block)
  entries << {
      path: path,
      rel: options[:rel],
      title: options[:title]
  }
  current_api.hal_for path, &block
end

#mount(api) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/grape/hal/endpoint.rb', line 25

def mount(api)
  api.endpoints.each do |endpoint|
    options = endpoint.options
    options[:path].each do |path|
      entries << {
          path: path,
          rel: options[:route_options][:rel],
          title: options[:route_options][:description]
      }
    end
  end
end