class BandCampBX::Entities::Base

Public Class Methods

keys() click to toggle source
# File lib/bandcampbx/entities/base.rb, line 10
def self.keys
  self.mappings.keys
end
map_decimal() click to toggle source
# File lib/bandcampbx/entities/base.rb, line 36
def self.map_decimal
  ->(val) { BigDecimal(val) }
end
map_int() click to toggle source
# File lib/bandcampbx/entities/base.rb, line 32
def self.map_int
  ->(val) { val.to_i }
end
map_time() click to toggle source
# File lib/bandcampbx/entities/base.rb, line 28
def self.map_time
  ->(val) { Time.parse(val) }
end
new(hash) click to toggle source
# File lib/bandcampbx/entities/base.rb, line 14
def initialize(hash)
  check_for_errors(hash)
  map_instance_variables(hash)
end
setup_readers() click to toggle source
# File lib/bandcampbx/entities/base.rb, line 6
def self.setup_readers
  keys.each {|k| attr_reader k.to_sym }
end

Public Instance Methods

inspect() click to toggle source
# File lib/bandcampbx/entities/base.rb, line 19
def inspect
  inspect_string = "#<#{self.class}:#{self.object_id} "
  self.class.keys.each do |key|
    inspect_string << "#{key}: #{send(key).inspect} "
  end
  inspect_string << " >"
  inspect_string
end

Private Instance Methods

check_for_errors(hash) click to toggle source
# File lib/bandcampbx/entities/base.rb, line 47
def check_for_errors(hash)
  if hash.has_key?("error")
    if hash["error"].has_key?("__all__")
      raise BandCampBX::StandardError.new(hash["error"]["__all__"].join(".  "))
    else
      raise BandCampBX::StandardError.new("CampBX API Error #404")
    end
  end
end
map_instance_variables(hash) click to toggle source
# File lib/bandcampbx/entities/base.rb, line 41
def map_instance_variables(hash)
  self.class.keys.each do |key|
    instance_variable_set("@#{key}", self.class.mappings[key].call(hash[key.to_s].to_s))
  end
end