Sha256: d10d7861ccc9765bb4feaca49f61821991cfb1e8f9723e5278e8318b2c593d4b
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
module Billogram class Resource DEFAULT_OPTIONS = { page: 1, page_size: 50 } class << self def relations @relations ||= { one: Set.new, many: Set.new } end def endpoint(value = nil) @endpoint = value if value @endpoint || "#{name.sub('Billogram::', '').downcase}s" end def search(options = {}) query = DEFAULT_OPTIONS.merge(options) response = Billogram.client.get("#{endpoint}", {query: query}) build_objects(response) end def fetch(id) response = Billogram.client.get("#{endpoint}/#{id}") build_objects(response) end def create(attributes) response = Billogram.client.post("#{endpoint}", {body: attributes.to_json}) build_objects(response) end def update(id, attributes) response = Billogram.client.put("#{endpoint}/#{id}", {body: attributes.to_json}) build_objects(response) end def delete(id) Billogram.client.put("#{endpoint}/#{id}") end def relation(relation_name, relation_type = :one) relations[relation_type] << relation_name attr_accessor relation_name end def build_objects(data) case data when Hash then new(data) when Array then data.map{|item| build_objects(item) } else data end end end def initialize(attributes = {}) Hash(attributes).each do |key, value| public_send("#{key}=", value) if respond_to?(key) end RelationBuilder.new(self, attributes).call end def update(attributes) self.class.update(id, attributes) end def delete self.class.delete(id) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
billogram-0.3.1 | lib/billogram/resource.rb |