class Alman::ApiResource

Attributes

api_method[R]
client[R]
json[R]

Public Class Methods

add_api_attribute(name) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 56
def self.add_api_attribute(name)
  attr_accessor name.to_sym
  @api_attributes[name.to_sym] = {}
end
api_attribute_names() click to toggle source
# File lib/alman/apibits/api_resource.rb, line 52
def self.api_attribute_names
  @api_attributes.map(&:first)
end
api_subclass_fetch(name) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 120
def self.api_subclass_fetch(name)
  @api_subclasses_hash ||= {}
  if @api_subclasses_hash.has_key?(name)
    @api_subclasses_hash[name]
  end
end
api_subclasses() click to toggle source
# File lib/alman/apibits/api_resource.rb, line 116
def self.api_subclasses
  return @api_subclasses ||= Set.new
end
client() click to toggle source

Returns the default client

# File lib/alman/apibits/api_resource.rb, line 34
def self.client
  Alman.default_client
end
determine_api_attribute_value(name, raw_value) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 98
def self.determine_api_attribute_value(name, raw_value)
  if @api_attributes[name] && @api_attributes[name].has_key?(:constructor)
    klass = Util.constantize(@api_attributes[name][:constructor])
    if(klass.respond_to?(:construct))
      klass.construct(raw_value)
    else
      klass.new(raw_value)
    end
  else
    ApiObject.construct(raw_value)
  end
end
new(json=nil, api_method=nil, client=nil) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 7
def initialize(json=nil, api_method=nil, client=nil)
  refresh_from(json, api_method, client)
end
register_api_subclass(subclass, name=nil) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 127
def self.register_api_subclass(subclass, name=nil)
  @api_subclasses ||= Set.new
  @api_subclasses << subclass

  unless name.nil?
    @api_subclasses_hash ||= {}
    @api_subclasses_hash[name] = subclass
  end
end

Public Instance Methods

api_attributes() click to toggle source
# File lib/alman/apibits/api_resource.rb, line 61
def api_attributes
  ret = {}
  self.class.api_attribute_names.each do |attribute|
    ret[attribute] = self.send(attribute)
  end
  ret
end
changed_api_attributes() click to toggle source

TODO(joncalhoun): Fix this (not currently working as intended)

# File lib/alman/apibits/api_resource.rb, line 82
def changed_api_attributes
  ret = {}
  self.api_attributes.each do |name, value|
    if @json[name] != value
      ret[name] = value
    end
  end
  ret
end
clear_api_attributes() click to toggle source
# File lib/alman/apibits/api_resource.rb, line 92
def clear_api_attributes
  self.class.api_attribute_names.each do |name|
    instance_variable_set("@#{name}", nil)
  end
end
determine_api_attribute_value(name, raw_value) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 111
def determine_api_attribute_value(name, raw_value)
  self.class.determine_api_attribute_value(name, raw_value)
end
inspect() click to toggle source
# File lib/alman/apibits/api_resource.rb, line 38
def inspect
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> Attributes: " + JSON.pretty_generate(inspect_api_attributes)
end
inspect_api_attributes() click to toggle source
# File lib/alman/apibits/api_resource.rb, line 69
def inspect_api_attributes
  ret = {}
  api_attributes.each do |k, v|
    if v.is_a?(ApiResource)
      ret[k] = v.inspect_nested
    else
      ret[k] = v
    end
  end
  ret
end
inspect_nested() click to toggle source
# File lib/alman/apibits/api_resource.rb, line 43
def inspect_nested
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}>"
end
refresh_from(json={}, api_method=nil, client=nil) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 11
def refresh_from(json={}, api_method=nil, client=nil)
  unless json.is_a?(Hash)
    json = { :id => json }
  end
  json = Util.symbolize_keys(json)

  # Clear or write over any old data
  clear_api_attributes
  @api_method = api_method
  @json = Util.sorta_deep_clone(json)
  @client = client || Alman.default_client

  # Use json (not the @json, the cloned copy)
  json.each do |k, v|
    unless self.class.api_attribute_names.include?(k.to_sym)
      self.class.add_api_attribute(k.to_sym)
    end
    instance_variable_set("@#{k}", determine_api_attribute_value(k, v))
  end
  self
end
to_json(*args) click to toggle source
# File lib/alman/apibits/api_resource.rb, line 48
def to_json(*args)
  JSON.generate(api_attributes)
end