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
@api private Full list of resource-specific attributes.
@api private Mapping of resource-specific attributes that must be parsed into a resource collection.
@api private List of resource-specific attributes that don't require additional processing during instance data population.
@api private List of resource-specific attributes that must be parsed into a Time object during instance data population.
The raw response data
Private Class Methods
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
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
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
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
@see Phaxio::Resource.response_record
# File lib/phaxio/resource.rb, line 37 def initialize raw_data self.raw_data = raw_data populate_attributes end
@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
Private Instance Methods
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