Module: Finix::Resource::ClassMethods

Defined in:
lib/finix/resources/resource.rb

Instance Method Summary collapse

Instance Method Details

#construct_from_response(payload) ⇒ Object

this is class method, not callable from instance



106
107
108
109
110
111
112
# File 'lib/finix/resources/resource.rb', line 106

def construct_from_response(payload)
  payload = Finix::Utils.indifferent_read_access payload
  links = payload.delete('_links') || {}
  instance = self.new payload
  instance.hydrate(links)
  instance
end

#fetch(*arguments) ⇒ Object Also known as: find



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/finix/resources/resource.rb', line 114

def fetch(*arguments)
  if arguments.nil? or arguments.empty? or arguments[0].nil? or arguments[0].to_s.empty?
    href = Finix.get_href self
    # href = Finix.hypermedia_registry.key(self)
    return Finix::Utils.eval_class(self, Pagination).new :href => href
  end

  options = arguments.slice!(0) or {}
  if options.is_a? String and options =~ URI::regexp
    href = options
  else
    href = Finix.get_href(self) or Finix.get_href(self.class)
    if options.is_a? Hash
      options = Finix::Utils.indifferent_read_access options
      id = options.delete('id')
    elsif options.is_a? String
      id = options
    end
    href = "#{href}/#{id}" unless id.nil?
  end

  response = Finix.get href
  construct_from_response response.body
end

#pagination(*args) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/finix/resources/resource.rb', line 139

def pagination(*args)
  href = Finix.get_href self
  # href = Finix.hypermedia_registry.key(self)
  opts = args.slice!(0) || {}
  opts[:href] = href
  Finix::Utils.eval_class(self, Pagination).new opts
end

#retrieve(*args) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/finix/resources/resource.rb', line 149

def retrieve(*args)
  pager_or_resource = fetch *args

  if pager_or_resource.is_a? Pagination
    pager_or_resource.load! unless pager_or_resource.loaded
  end

  pager_or_resource
end