class BigcommerceAPI::Resource
Attributes
belongs_to_options[RW]
has_many_options[RW]
has_one_options[RW]
attributes_were[RW]
errors[RW]
Public Class Methods
all(params={})
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 184 def all(params={}) resources = BigcommerceAPI::Resource.http_request(:get, "/#{resource}", :query => date_adjust(params)) (resources.success? and !resources.nil?) ? resources.collect{|r| self.new(r)} : [] end
belongs_to(*names)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 150 def belongs_to(*names) self.belongs_to_options = names.collect{|x| x.is_a?(Hash) ? x.keys.first.to_s : x.to_s} names.each do |m| if m.is_a? Hash meth = m.keys.first.to_s resource = m.values.first.to_s else meth = m.to_s resource = m.to_s end define_method meth do obj = resource.singularize.camelize url = '/' + meth.pluralize + '/' + self.send(meth + "_id").to_s out = BigcommerceAPI::Resource.http_request(:get, "#{url}") if out and !defined?('BigcommerceAPI::' + obj).nil? (out.success? and !out.nil?) ? ('BigcommerceAPI::' + obj).constantize.new(out) : nil end end end end
find(id)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 189 def find(id) return if id.blank? r = BigcommerceAPI::Resource.http_request(:get, "/#{resource}/#{id}") (r.success? and !r.nil?) ? self.new(r) : nil end
has_many(*names)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 110 def has_many(*names) self.has_many_options = names.collect{|x| x.is_a?(Hash) ? x.keys.first.to_s : x.to_s} names.each do |m| if m.is_a? Hash meth = m.keys.first.to_s res = m.values.first.to_s else meth = m.to_s res = m.to_s end define_method meth do out = BigcommerceAPI::Resource.http_request(:get, "#{self.send(meth + '_hash')['resource']}") obj = res.singularize.camelize if out and !defined?('BigcommerceAPI::' + obj).nil? (out.success? and !out.nil?) ? out.collect{|o| ('BigcommerceAPI::' + obj).constantize.new(o)} : [] end end end end
has_one(*names)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 130 def has_one(*names) self.has_one_options = names.collect{|x| x.is_a?(Hash) ? x.keys.first.to_s : x.to_s} names.each do |m| if m.is_a? Hash meth = m.keys.first.to_s resource = m.values.first.to_s else meth = m.to_s resource = m.to_s end define_method meth do out = BigcommerceAPI::Resource.http_request(:get, "#{self.send(meth + '_resource')['resource']}") obj = resource.singularize.camelize if out and !defined?('BigcommerceAPI::' + obj).nil? (out.success? and !out.nil?) ? ('BigcommerceAPI::' + obj).constantize.new(out) : nil end end end end
http_request(verb, url, options={})
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 195 def http_request(verb, url, options={}) begin response = BigcommerceAPI::Base.send(verb, url, options) if response.code >= 400 message = case response.code when 429 "Too many requests, please retry in #{response.headers["x-retry-after"]} second." when 500 "Internal Error" else parse_errors(response) end raise BigcommerceAPI::Error.new(response.code, message) end response rescue SocketError => e BigcommerceAPI::Result.new(:success => false, :errors => "Invalid URL") end end
new(data)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 6 def initialize(data) self.assign_attributes(data) self.attributes_were = data end
resource()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 171 def resource out = self.name.split('::').last.downcase last = out.split(//).last.to_s if last == 'y' out = out.chomp('y') + 'ies' elsif last == 's' out += 'es' else out += 's' end return out end
Private Class Methods
hash_to_s(hash)
click to toggle source
recursive function to convert hash into string, e.g. {a: {b: “c”}, d: “e”} becomes “c e”
# File lib/bigcommerce_api/resource.rb, line 218 def hash_to_s(hash) if hash.is_a?(Array) hash.map do |value| hash_to_s(value) end.to_sentence elsif hash.is_a?(Hash) hash_to_s(hash.values) else hash.to_s.gsub(/[,.]$/, '') end end
parse_errors(response)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 230 def parse_errors(response) hash_to_s(response.parsed_response) end
Public Instance Methods
assign_attributes(attributes)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 39 def assign_attributes(attributes) attributes.each do |k, v| if v and v.is_a? String val = v.gsub(/\n/, '').gsub(/\t/, '').strip else val = v end k = "#{k}_hash" if !self.class.has_many_options.nil? and self.class.has_many_options.include? k k = "#{k}_resource" if !self.class.has_one_options.nil? and self.class.has_one_options.include? k k = "#{self.resource}_#{k}" if k == 'type' send(:"#{k}=", val) if self.respond_to? "#{k}=" end end
changed()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 98 def changed changed = Array.new self.attributes.each do |k, v| changed << k if v != attributes_were[k] end changed -= %w[attributes_were errors] return changed end
create()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 53 def create # delete the parent id if there is one url = self.resource_url self.send(self.parent + '_id=', nil) if !self.parent.nil? attrs = self.attributes body = Hash.new self.changed.each{|c| body[c] = attrs[c]} response = BigcommerceAPI::Resource.http_request(:post, "/#{url}", :body => body.to_json) return self.class.new(response.parsed_response) end
delete()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 67 def delete url = self.resource_url BigcommerceAPI::Resource.http_request(:delete, "/#{url}/#{self.id}") return true end
find_for_reload()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 74 def find_for_reload self.class.find(self.id) end
mark_dirty!()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 11 def mark_dirty! self.attributes_were = {} self end
parent()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 94 def parent nil end
reload()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 78 def reload updated = self.find_for_reload self.attributes.each do |k, v| self.send("#{k}=", updated.send(k)) end return self end
resource()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 86 def resource self.class.name.downcase.to_s.split('::').last end
resource_url()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 90 def resource_url self.class.resource end
save()
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 16 def save url = self.resource_url if self.id.nil? # delete the parent id if there is one self.send(self.parent + '_id=', nil) if !self.parent.nil? response = BigcommerceAPI::Resource.http_request(:post, "/#{url}", :body => self.attributes(true).to_json) else # only send updated attributes attrs = self.attributes body = Hash.new self.changed.each{|c| body[c] = attrs[c]} body.delete('date_modified') response = BigcommerceAPI::Resource.http_request(:put, "/#{url}/#{self.id}", :body => body.to_json) end self.class.new(response.parsed_response) end
update_attributes(attributes)
click to toggle source
# File lib/bigcommerce_api/resource.rb, line 35 def update_attributes(attributes) assign_attributes(attributes) && save end