Class: PYR::ResponseObject

Inherits:
LazyRecord::Base
  • Object
show all
Defined in:
lib/pyr/response_object.rb

Overview

The ResponseObject is the parent class of all objects instantiated from the response body.

Direct Known Subclasses

District, OfficeLocation, Rep, State, Zcta

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ ResponseObject

Pass in an options hash to instantiate the object and set the data attributes. First iterates through the hash converting nested hashes and arrays of hashes into other PYR::ResponseObjects if possible. Hash keys that are not defined as attributes with ‘attr_accessor` or `lr_has_many` will not be accessible in the parent object.



19
20
21
22
23
24
# File 'lib/pyr/response_object.rb', line 19

def initialize(opts = {}, &block)
  new_opts = opts.each_with_object({}) do |(key, val), memo|
    memo[key] = convert_json_object_to_pyr_resource(key, val) || val
  end
  super(new_opts, &block)
end

Instance Method Details

#callObject

Send a request to the API for the object.



32
33
34
# File 'lib/pyr/response_object.rb', line 32

def call
  PYR.call self
end

#controllerObject

The controller name, based on the object’s class name.



27
28
29
# File 'lib/pyr/response_object.rb', line 27

def controller
  @controller ||= self.class.to_s.split('::').last.tableize
end