class Kojn::Collection

Attributes

access_token[RW]
model[RW]
module[RW]
name[RW]
path[RW]

Public Class Methods

new(api_prefix="/api") click to toggle source
# File lib/kojn/collection.rb, line 5
def initialize(api_prefix="/api")
  self.access_token = Kojn.api_key

  self.module = self.class.to_s.singularize.underscore
  self.name   = self.module.split('/').last
  self.model  = self.module.camelize.constantize
  self.path   = "#{api_prefix}/#{self.name.pluralize}"
end

Public Instance Methods

all(options = {}) click to toggle source
# File lib/kojn/collection.rb, line 14
def all(options = {})
  parse_objects! Kojn::Net::get(self.path).body, self.model
end
create(options = {}) click to toggle source
# File lib/kojn/collection.rb, line 18
def create(options = {})
  parse_object! Kojn::Net::post(self.path, {invoice: options}).body, self.model
end
find(id, options = {}) click to toggle source
# File lib/kojn/collection.rb, line 22
def find(id, options = {})
  parse_object! Kojn::Net::get("#{self.path}/#{id}").body, self.model
end
update(id, options = {}) click to toggle source
# File lib/kojn/collection.rb, line 26
def update(id, options = {})
  parse_object! Kojn::Net::patch("#{self.path}/#{id}", options).body, self.model
end

Protected Instance Methods

parse_object!(object, klass) click to toggle source
# File lib/kojn/collection.rb, line 38
def parse_object!(object, klass)
  object = JSON.parse(object) if object.is_a? String

  klass.new(object)
end
parse_objects!(string, klass) click to toggle source
# File lib/kojn/collection.rb, line 31
def parse_objects!(string, klass)
  objects = JSON.parse(string)
  objects.collect do |t_json|
    parse_object!(t_json, klass)
  end
end