class Lightspeed::Collection
Constants
- PER_PAGE
Attributes
context[RW]
resources[RW]
Public Class Methods
collection_name()
click to toggle source
# File lib/lightspeed/collection.rb, line 104 def self.collection_name name.demodulize end
new(context:, attributes: nil)
click to toggle source
# File lib/lightspeed/collection.rb, line 12 def initialize(context:, attributes: nil) self.context = context instantiate(attributes) end
resource_class()
click to toggle source
# File lib/lightspeed/collection.rb, line 112 def self.resource_class "Lightspeed::#{resource_name}".constantize end
resource_name()
click to toggle source
# File lib/lightspeed/collection.rb, line 108 def self.resource_name collection_name.singularize end
Public Instance Methods
account()
click to toggle source
# File lib/lightspeed/collection.rb, line 17 def account context.account end
all(params: {})
click to toggle source
# File lib/lightspeed/collection.rb, line 58 def all(params: {}) enum_page(params: params).to_a.flatten(1) end
all_loaded()
click to toggle source
# File lib/lightspeed/collection.rb, line 46 def all_loaded each_loaded.to_a end
as_json()
click to toggle source
# File lib/lightspeed/collection.rb, line 124 def as_json return if all_loaded.empty? { resource_name => all_loaded.as_json } end
Also aliased as: to_h
base_path()
click to toggle source
# File lib/lightspeed/collection.rb, line 116 def base_path "#{account.base_path}/#{resource_name}" end
client()
click to toggle source
# File lib/lightspeed/collection.rb, line 25 def client return context if context.is_a?(Lightspeed::Client) account.client end
create(attributes = {})
click to toggle source
# File lib/lightspeed/collection.rb, line 92 def create(attributes = {}) instantiate(post(body: Yajl::Encoder.encode(attributes))).first end
destroy(id)
click to toggle source
# File lib/lightspeed/collection.rb, line 100 def destroy(id) instantiate(delete(id)).first end
each(per_page: PER_PAGE, params: {}, &block)
click to toggle source
# File lib/lightspeed/collection.rb, line 84 def each(per_page: PER_PAGE, params: {}, &block) enum(per_page: per_page, params: params).each(&block) end
each_loaded()
click to toggle source
# File lib/lightspeed/collection.rb, line 41 def each_loaded @resources ||= {} @resources.each_value end
each_page(per_page: PER_PAGE, params: {}, &block)
click to toggle source
# File lib/lightspeed/collection.rb, line 62 def each_page(per_page: PER_PAGE, params: {}, &block) enum_page(per_page: per_page, params: params).each(&block) end
enum(per_page: PER_PAGE, params: {})
click to toggle source
# File lib/lightspeed/collection.rb, line 76 def enum(per_page: PER_PAGE, params: {}) Enumerator.new do |yielder| each_page(per_page: per_page, params: params) do |page| page.each { |resource| yielder << resource } end end end
enum_page(per_page: PER_PAGE, params: {})
click to toggle source
# File lib/lightspeed/collection.rb, line 66 def enum_page(per_page: PER_PAGE, params: {}) Enumerator.new do |yielder| loop.with_index do |_, n| resources = page(n, per_page: per_page, params: params) yielder << resources raise StopIteration if resources.length < per_page end end end
find(id)
click to toggle source
# File lib/lightspeed/collection.rb, line 88 def find(id) first(params: { resource_class.id_field => id }) || handle_not_found(id) end
first(params: {})
click to toggle source
# File lib/lightspeed/collection.rb, line 30 def first(params: {}) params = params.merge(limit: 1) instantiate(get(params: params)).first end
first_loaded()
click to toggle source
# File lib/lightspeed/collection.rb, line 50 def first_loaded each_loaded.first end
inspect()
click to toggle source
# File lib/lightspeed/collection.rb, line 120 def inspect "#<#{self.class.name} API#{base_path}>" end
load_relations_default()
click to toggle source
# File lib/lightspeed/collection.rb, line 139 def load_relations_default 'all' end
page(n, per_page: PER_PAGE, params: {})
click to toggle source
# File lib/lightspeed/collection.rb, line 134 def page(n, per_page: PER_PAGE, params: {}) params = params.merge(limit: per_page, offset: per_page * n) instantiate(get(params: params)) end
size(params: {})
click to toggle source
# File lib/lightspeed/collection.rb, line 35 def size(params: {}) params = params.merge(limit: 1, load_relations: nil) get(params: params)['@attributes']['count'].to_i end
Also aliased as: length
size_loaded()
click to toggle source
# File lib/lightspeed/collection.rb, line 54 def size_loaded @resources.size end
to_json()
click to toggle source
# File lib/lightspeed/collection.rb, line 130 def to_json Yajl::Encoder.encode(as_json) end
unload()
click to toggle source
# File lib/lightspeed/collection.rb, line 21 def unload @resources = {} end
update(id, attributes = {})
click to toggle source
# File lib/lightspeed/collection.rb, line 96 def update(id, attributes = {}) instantiate(put(id, body: Yajl::Encoder.encode(attributes))).first end
Private Instance Methods
collection_path()
click to toggle source
# File lib/lightspeed/collection.rb, line 206 def collection_path "#{base_path}.json" end
context_params()
click to toggle source
# File lib/lightspeed/collection.rb, line 149 def context_params if context.class.respond_to?(:id_field) && resource_class.method_defined?(context.class.id_field.to_sym) { context.class.id_field => context.id } else {} end end
delete(id)
click to toggle source
# File lib/lightspeed/collection.rb, line 200 def delete(id) client.delete( path: resource_path(id) ) end
get(params: {})
click to toggle source
# File lib/lightspeed/collection.rb, line 175 def get(params: {}) params = { load_relations: load_relations_default } .merge(context_params) .merge(params) .compact client.get( path: collection_path, params: params ) end
handle_not_found(id)
click to toggle source
# File lib/lightspeed/collection.rb, line 145 def handle_not_found(id) raise Lightspeed::Error::NotFound, "Could not find a #{resource_name} with #{resource_class.id_field}=#{id}" end
instantiate(response)
click to toggle source
# File lib/lightspeed/collection.rb, line 158 def instantiate(response) return [] unless response.is_a?(Hash) @resources ||= {} Array.wrap(response[resource_name]).map do |resource| resource = resource_class.new(context: self, attributes: resource) @resources[resource.id] = resource end end
post(body:)
click to toggle source
# File lib/lightspeed/collection.rb, line 186 def post(body:) client.post( path: collection_path, body: body ) end
put(id, body:)
click to toggle source
# File lib/lightspeed/collection.rb, line 193 def put(id, body:) client.put( path: resource_path(id), body: body ) end
resource_class()
click to toggle source
# File lib/lightspeed/collection.rb, line 167 def resource_class self.class.resource_class end
resource_name()
click to toggle source
# File lib/lightspeed/collection.rb, line 171 def resource_name self.class.resource_name end
resource_path(id)
click to toggle source
# File lib/lightspeed/collection.rb, line 210 def resource_path(id) "#{base_path}/#{id}.json" end