Class: BigcommerceAPI::Resource
- Inherits:
-
Base
- Object
- Base
- BigcommerceAPI::Resource
show all
- Defined in:
- lib/bigcommerce_api/resource.rb
Direct Known Subclasses
Address, Brand, Category, Coupon, Customer, Hook, Image, Option, OptionSet, OptionSetOption, OptionValue, Order, OrderCoupon, OrderProduct, OrderStatus, Product, ProductOption, Rule, Shipment, ShippingAddress, Sku, TaxClass
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
#attributes, clean!, date_adjust, default_options, #store, #time, to_rfc2822
Constructor Details
#initialize(data) ⇒ Resource
Returns a new instance of Resource.
6
7
8
9
|
# File 'lib/bigcommerce_api/resource.rb', line 6
def initialize(data)
self.assign_attributes(data)
self.attributes_were = data
end
|
Class Attribute Details
.belongs_to_options ⇒ Object
Returns the value of attribute belongs_to_options.
108
109
110
|
# File 'lib/bigcommerce_api/resource.rb', line 108
def belongs_to_options
@belongs_to_options
end
|
.has_many_options ⇒ Object
Returns the value of attribute has_many_options.
108
109
110
|
# File 'lib/bigcommerce_api/resource.rb', line 108
def has_many_options
@has_many_options
end
|
.has_one_options ⇒ Object
Returns the value of attribute has_one_options.
108
109
110
|
# File 'lib/bigcommerce_api/resource.rb', line 108
def has_one_options
@has_one_options
end
|
Instance Attribute Details
#errors ⇒ Object
Returns the value of attribute errors.
4
5
6
|
# File 'lib/bigcommerce_api/resource.rb', line 4
def errors
@errors
end
|
Class Method Details
.all(params = {}) ⇒ Object
184
185
186
187
|
# 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) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
# 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) ⇒ Object
189
190
191
192
193
|
# 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) ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# 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) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# 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 = {}) ⇒ Object
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
# 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.["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
|
.resource ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
|
# 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
|
Instance Method Details
#assign_attributes(attributes) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# 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 ⇒ Object
98
99
100
101
102
103
104
105
|
# 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 ⇒ Object
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/bigcommerce_api/resource.rb', line 53
def create
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 ⇒ Object
67
68
69
70
71
72
|
# 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 ⇒ Object
74
75
76
|
# File 'lib/bigcommerce_api/resource.rb', line 74
def find_for_reload
self.class.find(self.id)
end
|
#mark_dirty! ⇒ Object
11
12
13
14
|
# File 'lib/bigcommerce_api/resource.rb', line 11
def mark_dirty!
self.attributes_were = {}
self
end
|
#parent ⇒ Object
94
95
96
|
# File 'lib/bigcommerce_api/resource.rb', line 94
def parent
nil
end
|
#reload ⇒ Object
78
79
80
81
82
83
84
|
# 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 ⇒ Object
86
87
88
|
# File 'lib/bigcommerce_api/resource.rb', line 86
def resource
self.class.name.downcase.to_s.split('::').last
end
|
#resource_url ⇒ Object
90
91
92
|
# File 'lib/bigcommerce_api/resource.rb', line 90
def resource_url
self.class.resource
end
|
#save ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/bigcommerce_api/resource.rb', line 16
def save
url = self.resource_url
if self.id.nil?
self.send(self.parent + '_id=', nil) if !self.parent.nil?
response = BigcommerceAPI::Resource.http_request(:post, "/#{url}", :body => self.attributes(true).to_json)
else
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) ⇒ Object
35
36
37
|
# File 'lib/bigcommerce_api/resource.rb', line 35
def update_attributes(attributes)
assign_attributes(attributes) && save
end
|