Class: BigcommerceAPI::Resource
- Inherits:
-
Base
- Object
- Base
- BigcommerceAPI::Resource
show all
- Defined in:
- lib/bigcommerce_api/resource.rb
Direct Known Subclasses
Brand, Category, Image, Option, OptionSet, OptionSetOption, OptionValue, Order, 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, #store, #time, to_rfc2822
Constructor Details
#initialize(data) ⇒ Resource
Returns a new instance of Resource.
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/bigcommerce_api/resource.rb', line 6
def initialize(data)
data.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
self.attributes_were = data
end
|
Class Attribute Details
.belongs_to_options ⇒ Object
Returns the value of attribute belongs_to_options.
96
97
98
|
# File 'lib/bigcommerce_api/resource.rb', line 96
def belongs_to_options
@belongs_to_options
end
|
.has_many_options ⇒ Object
Returns the value of attribute has_many_options.
96
97
98
|
# File 'lib/bigcommerce_api/resource.rb', line 96
def has_many_options
@has_many_options
end
|
.has_one_options ⇒ Object
Returns the value of attribute has_one_options.
96
97
98
|
# File 'lib/bigcommerce_api/resource.rb', line 96
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
172
173
174
175
|
# File 'lib/bigcommerce_api/resource.rb', line 172
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/bigcommerce_api/resource.rb', line 138
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
177
178
179
180
|
# File 'lib/bigcommerce_api/resource.rb', line 177
def find(id)
r = BigcommerceAPI::Resource.http_request(:get, "/#{resource}/#{id}")
(r.success? and !r.nil?) ? self.new(r) : nil
end
|
.has_many(*names) ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/bigcommerce_api/resource.rb', line 98
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/bigcommerce_api/resource.rb', line 118
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
182
183
184
185
186
187
188
|
# File 'lib/bigcommerce_api/resource.rb', line 182
def http_request(verb, url, options={})
begin
BigcommerceAPI::Base.send(verb, url, options)
rescue SocketError => e
BigcommerceAPI::Result.new(:success => false, :errors => "Invalid URL")
end
end
|
.resource ⇒ Object
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/bigcommerce_api/resource.rb', line 159
def resource
out = self.name.split('::').last.downcase
last = out.split(//).last(1).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
#changed ⇒ Object
86
87
88
89
90
91
92
93
|
# File 'lib/bigcommerce_api/resource.rb', line 86
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(params = {}) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/bigcommerce_api/resource.rb', line 48
def create(params={})
url = self.resource_url
self.send(self.parent + '_id=', nil) if !self.parent.nil?
response = BigcommerceAPI::Resource.http_request(:post, "/#{url}", :body => date_adjust(params).to_json)
if response.success?
return self.class.new(response.parsed_response)
else
self.errors = response.parsed_response
return false
end
end
|
#find_for_reload ⇒ Object
62
63
64
|
# File 'lib/bigcommerce_api/resource.rb', line 62
def find_for_reload
self.class.find(self.id)
end
|
#mark_dirty! ⇒ Object
21
22
23
24
|
# File 'lib/bigcommerce_api/resource.rb', line 21
def mark_dirty!
self.attributes_were = {}
self
end
|
#parent ⇒ Object
82
83
84
|
# File 'lib/bigcommerce_api/resource.rb', line 82
def parent
nil
end
|
#reload ⇒ Object
66
67
68
69
70
71
72
|
# File 'lib/bigcommerce_api/resource.rb', line 66
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
74
75
76
|
# File 'lib/bigcommerce_api/resource.rb', line 74
def resource
self.class.name.downcase.to_s.split('::').last
end
|
#resource_url ⇒ Object
78
79
80
|
# File 'lib/bigcommerce_api/resource.rb', line 78
def resource_url
self.class.resource
end
|
#save ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/bigcommerce_api/resource.rb', line 26
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]}
response = BigcommerceAPI::Resource.http_request(:put, "/#{url}/#{self.id}", :body => body.to_json)
end
if response.success?
return self.id.nil? ? self.class.new(response.parsed_response) : true
else
self.errors = response.parsed_response
return false
end
end
|