Module: Finix::HalResource

Included in:
Errors, Pagination, Resource
Defined in:
lib/finix/hal_resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/finix/hal_resource.rb', line 7

def method_missing(method, *args, &block)
  attributes = [@attributes]
  attributes << @attributes['page'] unless @attributes['page'].nil?
  attributes.each do |attrs|
    if attrs.has_key?(method.to_s)
      return attrs[method.to_s]
    end
  end

  if @attributes.empty? or (@attributes.has_key?('page') and not @attributes.has_key?('items'))
    self.refresh if self.respond_to? :refresh
    return self.send :method_missing, method, *args, &block
  end

  case method.to_s
    when /(.+)=$/ # support setting
      attr = method.to_s.chop
      @attributes[attr] = args.slice(0)
    else
      @hyperlinks.send :method_missing, method, *args, &block
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



5
6
7
# File 'lib/finix/hal_resource.rb', line 5

def attributes
  @attributes
end

Returns the value of attribute hyperlinks.



4
5
6
# File 'lib/finix/hal_resource.rb', line 4

def hyperlinks
  @hyperlinks
end

Instance Method Details

#load_page_from_response!(response) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/finix/hal_resource.rb', line 30

def load_page_from_response!(response)
  body = Finix::Utils.indifferent_read_access response.body

  hash_class = Finix::Utils.eval_class(self, IndifferentHash)
  @hyperlinks = hash_class.new
  links = body.delete('_links')
  links.each { |key, link| @hyperlinks[key.to_sym] = link[:href] } unless links.nil?

  page = body.delete('page')
  @attributes = {'items' => [], 'page' => hash_class.new(page)} # clear attributes
  if body.has_key? '_embedded'
    resource_name, resources = body.delete('_embedded').first
    @resource_class = Finix.from_hypermedia_registry resource_name
    @attributes['items'] = resources.map do |attrs|
      cls = Finix.from_hypermedia_registry resource_name, attrs
      cls.construct_from_response attrs
    end
  end

  @attributes.merge! body
end