module Finix::HalResource

Attributes

attributes[RW]

Public Instance Methods

load_page_from_response!(response) click to toggle source
# 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
method_missing(method, *args, &block) click to toggle source
# 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