class Lightspeed::Resource
Attributes
account[RW]
attributes[RW]
client[RW]
context[RW]
id[RW]
Public Class Methods
collection_name()
click to toggle source
# File lib/lightspeed/resource.rb, line 89 def self.collection_name resource_name.pluralize end
fields(fields = {})
click to toggle source
# File lib/lightspeed/resource.rb, line 34 def self.fields(fields = {}) @fields ||= [] attr_writer(*fields.keys) fields.each do |name, klass| @fields << define_method(name) do get_transformed_value(name, klass) end end @fields end
id_field()
click to toggle source
# File lib/lightspeed/resource.rb, line 93 def self.id_field "#{resource_name.camelize(:lower)}ID" end
new(client: nil, context: nil, attributes: {})
click to toggle source
# File lib/lightspeed/resource.rb, line 15 def initialize(client: nil, context: nil, attributes: {}) self.client = client self.context = context self.attributes = attributes end
relationships(*args)
click to toggle source
# File lib/lightspeed/resource.rb, line 101 def self.relationships(*args) @relationships ||= [] paired_args = args.flat_map { |r| r.is_a?(Hash) ? r.to_a : [[r, r]] } paired_args.each do |(relation_name, class_name)| method_name = relation_name.to_s.underscore.to_sym @relationships << define_method(method_name) do instance_variable_get("@#{method_name}") || get_relation(method_name, relation_name, class_name) end end @relationships end
resource_name()
click to toggle source
# File lib/lightspeed/resource.rb, line 85 def self.resource_name name.demodulize end
Public Instance Methods
as_json()
click to toggle source
# File lib/lightspeed/resource.rb, line 122 def as_json fields_to_h.merge(relationships_to_h).reject { |_, v| v.nil? || v == {} } end
Also aliased as: to_h
attributes=(attributes)
click to toggle source
# File lib/lightspeed/resource.rb, line 21 def attributes=(attributes) @attributes = attributes attributes.each do |key, value| send(:"#{key}=", value) if self.respond_to?(:"#{key}=") end self.id = send(self.class.id_field) attributes end
base_path()
click to toggle source
# File lib/lightspeed/resource.rb, line 127 def base_path if context.is_a?(Lightspeed::Collection) "#{context.base_path}/#{id}" else "#{account.base_path}/#{resource_name}/#{id}" end end
destroy()
click to toggle source
# File lib/lightspeed/resource.rb, line 80 def destroy self.attributes = delete[resource_name] self end
get_transformed_value(name, kind)
click to toggle source
# File lib/lightspeed/resource.rb, line 46 def get_transformed_value(name, kind) value = instance_variable_get("@#{name}") if value.is_a?(String) case kind when :string then value when :id, :integer then value.to_i when :datetime then DateTime.parse(value) when :boolean then value == 'true' when :decimal then BigDecimal(value) when :hash then Hash.new(value) else raise ArgumentError, "Could not transform value #{value} to a #{kind}" end else value end end
id_params()
click to toggle source
# File lib/lightspeed/resource.rb, line 97 def id_params { self.class.id_field => id } end
inspect()
click to toggle source
# File lib/lightspeed/resource.rb, line 113 def inspect "#<#{self.class.name} API#{base_path}>" end
load()
click to toggle source
# File lib/lightspeed/resource.rb, line 68 def load self.attributes = get[resource_name] if (attributes.keys - [self.class.id_field]).empty? end
read_attribute_for_serialization(method_name)
click to toggle source
# File lib/lightspeed/resource.rb, line 143 def read_attribute_for_serialization(method_name) method_name = method_name.to_sym if self.class.fields.include?(method_name) || self.class.relationships.include?(method_name) send(method_name) end end
reload()
click to toggle source
# File lib/lightspeed/resource.rb, line 72 def reload self.attributes = get[resource_name] end
resource_name()
click to toggle source
# File lib/lightspeed/resource.rb, line 139 def resource_name self.class.resource_name end
singular_path_parent()
click to toggle source
# File lib/lightspeed/resource.rb, line 135 def singular_path_parent context end
to_json()
click to toggle source
# File lib/lightspeed/resource.rb, line 118 def to_json Yajl::Encoder.encode(as_json) end
update(attributes = {})
click to toggle source
# File lib/lightspeed/resource.rb, line 76 def update(attributes = {}) self.attributes = put(body: Yajl::Encoder.encode(attributes))[resource_name] end
Private Instance Methods
collection_class()
click to toggle source
# File lib/lightspeed/resource.rb, line 161 def collection_class "Lightspeed::#{self.class.collection_name}".constantize end
context_params()
click to toggle source
# File lib/lightspeed/resource.rb, line 187 def context_params if context.respond_to?(:id_field) && respond_to?(context.id_field.to_sym) { context.id_field => context.id } else {} end end
delete()
click to toggle source
# File lib/lightspeed/resource.rb, line 211 def delete client.delete( path: resource_path ) end
fields_to_h()
click to toggle source
# File lib/lightspeed/resource.rb, line 153 def fields_to_h self.class.fields.map { |f| [f, send(f)] }.to_h end
get(params: {})
click to toggle source
# File lib/lightspeed/resource.rb, line 196 def get(params: {}) params = { load_relations: 'all' }.merge(context_params).merge(params) client.get( path: resource_path, params: params ) end
get_collection_relation(method_name, relation_name, klass)
click to toggle source
# File lib/lightspeed/resource.rb, line 173 def get_collection_relation(method_name, relation_name, klass) collection = klass.new(context: self, attributes: attributes[relation_name.to_s]) instance_variable_set("@#{method_name}", collection) end
get_relation(method_name, relation_name, class_name)
click to toggle source
# File lib/lightspeed/resource.rb, line 165 def get_relation(method_name, relation_name, class_name) klass = "Lightspeed::#{class_name}".constantize case when klass <= Lightspeed::Collection then get_collection_relation(method_name, relation_name, klass) when klass <= Lightspeed::Resource then get_resource_relation(method_name, relation_name, klass) end end
get_resource_relation(method_name, relation_name, klass)
click to toggle source
# File lib/lightspeed/resource.rb, line 178 def get_resource_relation(method_name, relation_name, klass) id_field = "#{relation_name.to_s.camelize(:lower)}ID" # parentID != #categoryID, so we can't use klass.id_field resource = if send(id_field).to_i.nonzero? rel_attributes = attributes[klass.resource_name] || { klass.id_field => send(id_field) } klass.new(context: self, attributes: rel_attributes).tap(&:load) end instance_variable_set("@#{method_name}", resource) end
put(body:)
click to toggle source
# File lib/lightspeed/resource.rb, line 204 def put(body:) client.put( path: resource_path, body: body ) end
relationships_to_h()
click to toggle source
# File lib/lightspeed/resource.rb, line 157 def relationships_to_h self.class.relationships.map { |r| [r.to_s.camelize, send(r).to_h] }.to_h end
resource_path()
click to toggle source
# File lib/lightspeed/resource.rb, line 217 def resource_path "#{base_path}.json" end