class Kounta::Resource
Attributes
client[RW]
Public Class Methods
coerce(data)
click to toggle source
# File lib/kounta/resource.rb, line 15 def self.coerce(data) if data.is_a? Array data.map { |item| new(item) } else new(data) end end
has_many(sym, klass, assignments, route)
click to toggle source
# File lib/kounta/resource.rb, line 33 def self.has_many(sym, klass, assignments, route) # rubocop:disable Naming/PredicateName define_method(sym) do |has_many_params = nil, *args| client.objects_from_response(klass, :get, route.call(self, has_many_params), params: args[0]) .map { |returned_klass| assign_into(returned_klass, self, assignments) } end end
has_many_in_time_range(sym, klass, assignments, route)
click to toggle source
# File lib/kounta/resource.rb, line 40 def self.has_many_in_time_range(sym, klass, assignments, route) # rubocop:disable Naming/PredicateName define_method(sym) do |has_many_params = nil, *args| client.objects_from_response_in_time_range(klass, :get, route.call(self, has_many_params), params: args[0]) .map { |returned_klass| assign_into(returned_klass, self, assignments) } end end
has_one(sym, klass, assignments, route)
click to toggle source
# File lib/kounta/resource.rb, line 23 def self.has_one(sym, klass, assignments, route) # rubocop:disable Naming/PredicateName define_method(sym) do |item_id = nil, *args| if item_id assign_into(client.object_from_response(klass, :get, route.call(self, item_id), params: args[0]), self, assignments) else assign_into(klass.new, self, assignments) end end end
new(hash = {})
click to toggle source
# File lib/kounta/resource.rb, line 47 def initialize(hash = {}) return unless hash hash.each_pair do |k, v| begin self[k] = v if respond_to? k.to_sym rescue NoMethodError raise Kounta::Errors::UnknownResourceAttribute, "Unknown attribute: #{k} on resource #{self.class}" end end end
Public Instance Methods
delete!()
click to toggle source
# File lib/kounta/resource.rb, line 81 def delete! return self if new? response = client.perform(resource_path, :delete) except!('id', 'created_at', 'updated_at') if response.status == 204 self end
ignored_properties(array = [])
click to toggle source
# File lib/kounta/resource.rb, line 94 def ignored_properties(array = []) array + %i[created_at updated_at id company_id] end
new?()
click to toggle source
# File lib/kounta/resource.rb, line 90 def new? !id end
save!()
click to toggle source
# File lib/kounta/resource.rb, line 68 def save! response = new? ? client.perform(resource_path, :post, body: to_hash) : client.perform(resource_path, :put, body: to_hash) # automatically follow redirects to resources response = client.perform(response.headers['location'], :get) if response.status == 201 response.parsed.each_pair do |k, v| self[k] = v if respond_to? k.to_sym end self end
to_hash(hash = {})
click to toggle source
# File lib/kounta/resource.rb, line 59 def to_hash(hash = {}) {}.tap do |returning| self.class.properties.each do |property| next if ignored_properties.include?(property) returning[property] = self[property] if self[property] end end.merge(hash) end
Private Instance Methods
assign_into(klass, assigner, assignments)
click to toggle source
# File lib/kounta/resource.rb, line 100 def assign_into(klass, assigner, assignments) klass.client = assigner.client assignments.each_pair do |k, v| klass[k] = assigner[v] end klass end