class Billogram::Resource

Public Class Methods

build_objects(data) click to toggle source
# File lib/billogram/resource.rb, line 17
def build_objects(data)
  case data
  when Hash then new(data)
  when Array then data.map { |item| build_objects(item) }
  else data
  end
end
new(attributes = {}) click to toggle source
# File lib/billogram/resource.rb, line 26
def initialize(attributes = {})
  Hash(attributes).each do |key, value|
    if respond_to?("#{key}=")
      public_send("#{key}=", value)
    else
      warn("#{self.class}: unknown attribute #{key}")
    end
  end

  RelationBuilder.new(self, attributes).call
end
relation(name, type, class_override: nil) click to toggle source
# File lib/billogram/resource.rb, line 12
def relation(name, type, class_override: nil)
  relations << Relation.new(name, type, class_override: class_override)
  attr_accessor name
end
relations() click to toggle source
# File lib/billogram/resource.rb, line 8
def relations
  @relations ||= []
end

Public Instance Methods

to_hash() click to toggle source
# File lib/billogram/resource.rb, line 42
def to_hash
  instance_variables.each_with_object({}) do |variable, obj|
    value = instance_variable_get(variable)

    case value
    when Resource
      value = value.to_hash
    when Array
      value = value.map(&:to_hash)
    end

    obj[variable[1..-1]] = value
  end
end
to_json(*args) click to toggle source
# File lib/billogram/resource.rb, line 38
def to_json(*args)
  to_hash.to_json(*args)
end