class BillForward::BillingEntity

Attributes

resource_path[RW]
_client[RW]

Public Class Methods

_load(data) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 141
def _load(data)
        build_entity(JSON.parse(data))
end
build_entity(entity) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 48
def build_entity(entity)
        if entity.is_a? Hash
                # either we are given a serialized entity
                # we must unserialize it

                # this entity should the same client as we do
                client = @_client

                return self.new(entity, client)
        end
        if entity.is_a? self
                # or we are given an already-constructed entity
                # just return it as-is

                # for consistency we might want to set this entity to use the same client as us. Let's not for now.
                return entity
        end
        if
                expectedClassName = self.name
                actualClassName = entity.class.name
                raise TypeError.new("Expected instance of either: 'Hash' or '#{expectedClassName}' at argument 'entity'. "+
                        "Instead received: '#{actualClassName}'")
        end

        new_entity
end
build_entity_array(entity_hashes) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 40
def build_entity_array(entity_hashes)
        TypeCheck.verifyObj(Array, entity_hashes, 'entity_hashes')

        entity_hashes.map do |hash|
                self.build_entity(hash)
        end
end
get_all(query_params = {}, custom_client = nil) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 137
def get_all(query_params = {}, custom_client = nil)
        self.request_many('get', '', query_params, custom_client)
end
get_by_id(id, query_params = {}, custom_client = nil) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 127
def get_by_id(id, query_params = {}, custom_client = nil)
        raise ArgumentError.new("id cannot be nil") if id.nil?

        endpoint = sprintf('%s',
                ERB::Util.url_encode(id)
                )

        self.request_first('get', endpoint, query_params, custom_client)
end
new(state_params = nil, client = nil) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 7
def initialize(state_params = nil, client = nil)
        raise AbstractInstantiateError.new('This abstract class cannot be instantiated!') if self.class == MutableEntity

        client = self.class.singleton_client if client.nil?
        state_params = {} if state_params.nil?

        TypeCheck.verifyObj(Client, client, 'client')
        TypeCheck.verifyObj(Hash, state_params, 'state_params')

        @_registered_entities = Hash.new
        @_registered_entity_arrays = Hash.new

        @_client = client
        # initiate with empty state params
        # use indifferent hash so 'id' and :id are the same
        @_state_params = HashWithIndifferentAccess.new
        # legacy Ruby gives us this 'id' chuff. we kinda need it back.
        @_state_params.instance_eval { undef id if defined? id }
        # populate state params now
        unserialize_all state_params
end
request_ambiguous(*args) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 75
def request_ambiguous(*args)
        plurality = args.shift
        verb = args.shift
        endpoint = args.shift

        payload = nil;
        haspayload = @@payload_verbs.include?(verb)
        if (haspayload)
        payload_typed = args.shift
        payload = payload_typed.serialize
end

        query_params = args.shift
        custom_client = args.shift

        client = client.nil? \
        ? singleton_client \
        : custom_client

        route = resource_path.path
        url_full = "#{route}/#{endpoint}"
        method = "#{verb}_#{plurality}"

        arguments = [url_full, query_params]
        arguments.insert(1, payload) if haspayload

        # puts verb

        client.send(method.intern, *arguments)
end
request_first_heterotyped(*args) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 113
def request_first_heterotyped(*args)
        response_type = args.shift
        arguments = ['first']+args
        result = self.send(:request_ambiguous, *arguments)
        response_type.build_entity(result)
end
request_many_heterotyped(*args) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 106
def request_many_heterotyped(*args)
        response_type = args.shift
        arguments = ['many']+args
        results = self.send(:request_ambiguous, *arguments)
        response_type.build_entity_array(results)
end
singleton_client() click to toggle source
# File lib/bill_forward/billing_entity.rb, line 36
def singleton_client
        Client.default_client
end

Public Instance Methods

[](key) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 175
def [](key)
        method_missing(key)
end
[]=(key, value) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 179
def []=(key, value)
        set_key = key.to_s+'='
        method_missing(set_key, value)
end
_dump(_level) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 146
def _dump(_level)
        to_json
end
as_json()
Alias for: to_ordered_hash
camel_case_lower(operand) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 151
def camel_case_lower(operand)
        elements = operand.split('_')
        camel_cased = elements.join('_').camelize(:lower)

        camel_cased.gsub!(/Id/, 'ID') if elements.size > 1 && elements.last =~ /id=?\Z/

        camel_cased
end
method_missing(method_id, *arguments, &block) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 150
def method_missing(method_id, *arguments, &block)
        def camel_case_lower(operand)
                elements = operand.split('_')
                camel_cased = elements.join('_').camelize(:lower)

                camel_cased.gsub!(/Id/, 'ID') if elements.size > 1 && elements.last =~ /id=?\Z/

                camel_cased
        end

        qualified = camel_case_lower method_id.to_s

        if qualified != method_id.to_s
                return self.send(qualified.intern, *arguments)
        end

        # no call to super; our criteria is all keys.
        #setter
        if /^(\w+)=$/ =~ qualified
                return set_state_param($1, arguments.first)
        end
        #getter
        get_state_param(qualified)
end
serialize() click to toggle source
# File lib/bill_forward/billing_entity.rb, line 228
def serialize
        to_json
end
serialize_field(field) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 188
def serialize_field(field)
        if field.is_a? BillingEntity
                serialize_field field.state_params
        elsif field.is_a? Array
                # return a transformed array, where each field has been serialized
                field.map do |iterand|
                        serialize_field iterand
                end
        elsif field.is_a? Hash
                # the lengths I have to go to achieve immutability in Ruby 1.8.7
                clone = hash_with_type_at_top field
                clone.each do |key, value|
                        clone[key] = serialize_field value
                end
                clone
        else
                field
        end
end
state_params() click to toggle source
# File lib/bill_forward/billing_entity.rb, line 184
def state_params
        @_state_params
end
to_json(*a) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 214
def to_json(*a)
        to_ordered_hash.to_json
end
to_ordered_hash() click to toggle source
# File lib/bill_forward/billing_entity.rb, line 208
def to_ordered_hash
        serialize_field self
end
Also aliased as: as_json
to_s() click to toggle source
# File lib/bill_forward/billing_entity.rb, line 223
def to_s
        parsed = to_unordered_hash
        JSON.pretty_generate(parsed)
end
to_unordered_hash() click to toggle source
# File lib/bill_forward/billing_entity.rb, line 218
def to_unordered_hash
        json_string = to_json.to_s
        JSON.parse(json_string)
end

Protected Instance Methods

get_state_param(key) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 256
def get_state_param(key)
        @_state_params[key]
end
hash_with_type_at_top(hash) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 233
def hash_with_type_at_top(hash)
        new_hash = OrderedHashWithDotAccess.new

        # API presently requires '@type' (if present) to be first key in JSON
        if hash.has_key? '@type'
                # insert existing @type as first element in ordered hash
                new_hash['@type'] = hash.with_indifferent_access['@type']
        end

        # add key-value pairs excepting '@type' back in
        # no, we don't care about the order of these.
        hash.with_indifferent_access.reject {|key, value| key == '@type'}.each do |key, value|
                new_hash[key] = value
        end

        return new_hash
end
set_state_param(key, value) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 251
def set_state_param(key, value)
        @_state_params[key] = value
        get_state_param(key)
end
unserialize_all(hash) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 260
def unserialize_all(hash)
        TypeCheck.verifyObj(Hash, hash, 'hash')

        hash.each do |key, value|
                unserialized = unserialize_one value
                set_state_param(key, unserialized)
        end
end
unserialize_array(array) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 286
def unserialize_array(array)
        TypeCheck.verifyObj(Array, array, 'array')

        array.each_with_index do |value, index|
                # recurse down, so that all nested hashes get same treatment
                unserialized = unserialize_one value

                # replace with unserialized version
                array[index] = unserialized
        end

        array
end
unserialize_array_of_entities(key, entity_class, hash) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 328
def unserialize_array_of_entities(key, entity_class, hash)
        # ensure that the provided entity class derives from BillingEntity
        TypeCheck.verifyClass(BillingEntity, entity_class, 'entity_class')
        TypeCheck.verifyObj(Hash, hash, 'hash')

        # register the array of entities as one that requires bespoke serialization
        @_registered_entity_arrays[key] = entity_class
        # if key exists in the provided hash, add it to current entity's model
        if hash.has_key? key
                entities = entity_class.build_entity_array(hash[key])
                set_state_param(key, entities)
        end
end
unserialize_entity(key, entity_class, hash) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 310
def unserialize_entity(key, entity_class, hash)
        # ensure that the provided entity class derives from BillingEntity
        TypeCheck.verifyClass(BillingEntity, entity_class, 'entity_class')
        TypeCheck.verifyObj(Hash, hash, 'hash')

        # register the entity as one that requires bespoke serialization
        @_registered_entities[key] = entity_class
        # if key exists in the provided hash, add it to current entity's model
        if hash.has_key? key
                val = hash[key]
                if (val.nil?)
                        return
                end
                entity = entity_class.build_entity(val)
                set_state_param(key, entity)
        end
end
unserialize_hash(hash) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 269
def unserialize_hash(hash)
        TypeCheck.verifyObj(Hash, hash, 'hash')

        # API presently requires '@type' (if present) to be first key in JSON
        hash = hash_with_type_at_top(hash)

        hash.each do |key, value|
                # recurse down, so that all nested hashes get same treatment
                unserialized = unserialize_one value

                # replace with unserialized version
                hash[key] = unserialized
        end

        hash
end
unserialize_one(value) click to toggle source
# File lib/bill_forward/billing_entity.rb, line 300
def unserialize_one(value)
        if value.is_a? Hash
                return unserialize_hash(value)
        end
        if value.is_a? Array
                return unserialize_array(value)
        end
        value
end