class Phaxio::Resource

The base class for API resources, such as `Fax` and `PhoneNumber`.

This class is considered an implementation detail, and shouldn't be directly relied upon by users.

The only exception is that this class will continue to be the base class for all Phaxio resources, so checking whether a fax instance is a kind of Phaxio::Resource will always return true.

Attributes

attribute_list[RW]

@api private Full list of resource-specific attributes.

collection_attribute_mappings[RW]

@api private Mapping of resource-specific attributes that must be parsed into a resource collection.

normal_attribute_list[RW]

@api private List of resource-specific attributes that don't require additional processing during instance data population.

time_attribute_list[RW]

@api private List of resource-specific attributes that must be parsed into a Time object during instance data population.

raw_data[RW]

The raw response data

Private Class Methods

has_collection_attributes(attribute_hash) click to toggle source

Creates accessors for the given collection attributes and adds them to the class's internal attribute lists. @param attribute_hash [Hash<String, Symbol => Phaxio::Resource>]

A hash which has keys corresponding to the attribute name on this resource, and values
corresponding to the resource class for the collection's items.

@see Phaxio::Resource.collection_attribute_mappings

# File lib/phaxio/resource.rb, line 111
def has_collection_attributes attribute_hash
  # Array#to_h doesn't exist in 2.0.0, hence the inject here.
  attribute_hash = attribute_hash
    .map { |k, v| [ k.to_s.freeze, v ] }
    .inject({}) { |memo, obj| memo.tap { |memo| memo[obj.first] = obj.last } }
  attr_accessor *attribute_hash.keys
  self.attribute_list += attribute_hash.keys
  self.collection_attribute_mappings = self.collection_attribute_mappings.merge(attribute_hash)
end
has_normal_attributes(attribute_list) click to toggle source

Creates accessors for the given normal attributes and adds them to the class's internal attribute lists. @param attribute_list [Array]

A list of attributes as strings or symbols.

@see Phaxio::Resource.normal_attribute_list

# File lib/phaxio/resource.rb, line 86
def has_normal_attributes attribute_list
  attribute_list = attribute_list.map { |attribute_name| attribute_name.to_s.freeze }
  attr_accessor *attribute_list
  self.attribute_list += attribute_list
  self.normal_attribute_list += attribute_list
end
has_time_attributes(attribute_list) click to toggle source

Creates accessors for the given time attributes and adds them to the class's internal attribute lists. @param attribute_list [Array]

A list of attributes as strings or symbols.

@see Phaxio::Resource.time_attribute_list

# File lib/phaxio/resource.rb, line 98
def has_time_attributes attribute_list
  attribute_list = attribute_list.map { |attribute_name| attribute_name.to_s.freeze }
  attr_accessor *attribute_list
  self.attribute_list += attribute_list
  self.time_attribute_list += attribute_list
end
inherited(subclass) click to toggle source

Use the inherited hook to dynamically set each subclass's attribute lists to empty arrays upon creation.

# File lib/phaxio/resource.rb, line 123
def inherited subclass
  subclass.attribute_list = []
  subclass.normal_attribute_list = []
  subclass.time_attribute_list = []
  subclass.collection_attribute_mappings = {}
end
new(raw_data) click to toggle source

@see Phaxio::Resource.response_record

# File lib/phaxio/resource.rb, line 37
def initialize raw_data
  self.raw_data = raw_data
  populate_attributes
end
response_collection(raw_data) click to toggle source

@api private Returns a new collection of resource instances for this data. @param raw_data [Array] The raw response data from Phaxio. @return [Phaxio::Resource::Collection] A collection of Phaxio::Resource instances.

# File lib/phaxio/resource.rb, line 55
def response_collection raw_data
  Collection.new raw_data, self
end
response_record(raw_data) click to toggle source

@api private Returns a new instance of the resource for this data. @param raw_data [Hash] The raw response data from Phaxio. @return [Phaxio::Resource] The resource instance.

# File lib/phaxio/resource.rb, line 47
def response_record raw_data
  new raw_data
end

Private Instance Methods

populate_attributes() click to toggle source

Populates the instance's attributes based on the `raw_data`.

# File lib/phaxio/resource.rb, line 17
def populate_attributes
  self.class.normal_attribute_list.each do |normal_attribute|
    self.public_send "#{normal_attribute}=", raw_data[normal_attribute]
  end

  self.class.time_attribute_list.each do |time_attribute|
    time = raw_data[time_attribute]
    time = Time.parse(time) if !time.nil?
    self.public_send "#{time_attribute}=", time
  end

  self.class.collection_attribute_mappings.each do |collection_attribute, klass|
    collection = raw_data[collection_attribute] || []
    collection = {'data' => collection}
    collection = klass.response_collection(collection)
    self.public_send "#{collection_attribute}=", collection
  end
end