bigcommerce_api 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ module BigcommerceAPI
2
+ class OrderCoupon < Resource
3
+ attr_accessor :id,
4
+ :coupon_id,
5
+ :order_id,
6
+ :code,
7
+ :amount,
8
+ :type,
9
+ :discount
10
+
11
+ belongs_to :order, :coupon
12
+ end
13
+ end
@@ -1,14 +1,62 @@
1
1
  module BigcommerceAPI
2
-
3
2
  class OrderProduct < Resource
4
- attr_accessor :configurable_fields, :bin_picking_number, :cost_price_ex_tax, :base_total, :name, :fixed_shipping_cost, :price_tax, :price_ex_tax, :refund_amount, :quantity, :price_inc_tax, :product_id, :return_id, :is_refunded, :base_price, :order_id, :option_set_id, :weight, :total_ex_tax, :product_options, :event_name, :wrapping_cost_inc_tax, :wrapping_cost_ex_tax, :order_address_id, :id, :wrapping_message, :base_wrapping_cost, :cost_price_inc_tax, :total_inc_tax, :type, :applied_discounts, :wrapping_name, :cost_price_tax, :base_cost_price, :sku, :parent_order_product_id, :wrapping_cost_tax, :ebay_item_id, :quantity_shipped, :total_tax, :ebay_transaction_id, :event_date, :is_bundled_product
5
-
6
- belongs_to :option_set, {:parent_order_product => :order_product}, :ebay_item, :ebay_transaction, :return, :product, :order
7
- # reserved
3
+ attr_accessor :id,
4
+ :applied_discounts,
5
+ :base_cost_price,
6
+ :base_price,
7
+ :base_total,
8
+ :base_wrapping_cost,
9
+ :bin_picking_number,
10
+ :configurable_fields,
11
+ :cost_price_ex_tax,
12
+ :cost_price_inc_tax,
13
+ :cost_price_tax,
14
+ :ebay_item_id,
15
+ :ebay_transaction_id,
16
+ :event_date,
17
+ :event_name,
18
+ :fixed_shipping_cost,
19
+ :is_bundled_product,
20
+ :is_refunded,
21
+ :name,
22
+ :option_set_id,
23
+ :order_address_id,
24
+ :order_id,
25
+ :parent_order_product_id,
26
+ :price_ex_tax,
27
+ :price_inc_tax,
28
+ :price_tax,
29
+ :product_id,
30
+ :product_options,
31
+ :quantity,
32
+ :quantity_shipped,
33
+ :refund_amount,
34
+ :return_id,
35
+ :sku,
36
+ :total_ex_tax,
37
+ :total_inc_tax,
38
+ :total_tax,
39
+ :type,
40
+ :weight,
41
+ :wrapping_cost_ex_tax,
42
+ :wrapping_cost_inc_tax,
43
+ :wrapping_cost_tax,
44
+ :wrapping_message,
45
+ :wrapping_name
46
+
47
+ belongs_to :option_set,
48
+ { parent_order_product: :order_product },
49
+ :ebay_item,
50
+ :ebay_transaction,
51
+ :return,
52
+ :product,
53
+ :order
54
+
55
+ # reserved
8
56
  attr_accessor :orderproduct_type
9
-
10
- def shippingaddress
11
- a = BigcommerceAPI::Base.get "/orders/#{self.order_id}/shippingaddresses/#{self.order_address_id}"
57
+
58
+ def shipping_address
59
+ a = BigcommerceAPI::Base.get "/orders/#{self.order_id}/shipping_addresses/#{self.order_address_id}"
12
60
  (a.success? and !a.nil?) ? BigcommerceAPI::Shippingaddress.new(a) : nil
13
61
  end
14
62
 
@@ -38,7 +86,7 @@ module BigcommerceAPI
38
86
 
39
87
  class << self
40
88
  def all(order_id, params={})
41
- resources = BigcommerceAPI::Base.get("/orders/#{order_id}/products", :query => date_adjust(params))
89
+ resources = BigcommerceAPI::Base.get("/orders/#{order_id}/products", query: date_adjust(params))
42
90
  (resources.success? and !resources.nil?) ? resources.collect{|r| self.new(r)} : []
43
91
  end
44
92
 
@@ -47,7 +95,5 @@ module BigcommerceAPI
47
95
  (r.success? and !r.nil?) ? self.new(r) : nil
48
96
  end
49
97
  end
50
-
51
98
  end
52
-
53
- end
99
+ end
@@ -1,8 +1,7 @@
1
1
  module BigcommerceAPI
2
-
3
2
  class OrderStatus < Resource
4
- attr_accessor :id, :order, :name
5
-
3
+ attr_accessor :id,
4
+ :order,
5
+ :name
6
6
  end
7
-
8
- end
7
+ end
@@ -1,24 +1,102 @@
1
1
  module BigcommerceAPI
2
-
3
2
  class Product < Resource
4
3
  extend BigcommerceAPI::Countable
5
4
 
6
5
  # incoming attributes
7
- attr_accessor :brand_id, :myob_expense_account, :inventory_tracking, :preorder_release_date, :tax_class_id, :sku, :date_modified, :fixed_cost_shipping_price, :categories, :meta_description, :sort_order, :related_products, :is_visible, :description, :layout_file, :meta_keywords, :rating_total, :price, :event_date_field_name, :height, :order_quantity_minimum, :myob_asset_account, :order_quantity_maximum, :peachtree_gl_account, :retail_price, :availability_description, :weight, :is_preorder_only, :date_created, :open_graph_title, :rating_count, :open_graph_type, :option_set_id, :is_featured, :date_last_imported, :option_set_display, :availability, :is_condition_shown, :name, :inventory_warning_level, :event_date_end, :cost_price, :inventory_level, :event_date_type, :upc, :open_graph_description, :depth, :custom_url, :myob_income_account, :condition, :is_price_hidden, :custom_fields, :configurable_fields, :discount_rules, :warranty, :total_sold, :view_count, :event_date_start, :price_hidden_label, :videos, :sale_price, :bin_picking_number, :preorder_message, :is_open_graph_thumbnail, :search_keywords, :is_free_shipping, :width, :type, :id, :page_title, :downloads
6
+ attr_accessor :id,
7
+ :availability,
8
+ :availability_description,
9
+ :bin_picking_number,
10
+ :brand_id,
11
+ :categories,
12
+ :condition,
13
+ :configurable_fields,
14
+ :cost_price,
15
+ :custom_fields,
16
+ :custom_url,
17
+ :date_created,
18
+ :date_last_imported,
19
+ :date_modified,
20
+ :depth,
21
+ :description,
22
+ :discount_rules,
23
+ :downloads,
24
+ :event_date_end,
25
+ :event_date_field_name,
26
+ :event_date_start,
27
+ :event_date_type,
28
+ :fixed_cost_shipping_price,
29
+ :height,
30
+ :inventory_level,
31
+ :inventory_tracking,
32
+ :inventory_warning_level,
33
+ :is_condition_shown,
34
+ :is_featured,
35
+ :is_free_shipping,
36
+ :is_open_graph_thumbnail,
37
+ :is_preorder_only,
38
+ :is_price_hidden,
39
+ :is_visible,
40
+ :layout_file,
41
+ :meta_description,
42
+ :meta_keywords,
43
+ :myob_asset_account,
44
+ :myob_expense_account,
45
+ :myob_income_account,
46
+ :name,
47
+ :open_graph_description,
48
+ :open_graph_title,
49
+ :open_graph_type,
50
+ :option_set_display,
51
+ :option_set_id,
52
+ :order_quantity_maximum,
53
+ :order_quantity_minimum,
54
+ :page_title,
55
+ :peachtree_gl_account,
56
+ :preorder_message,
57
+ :preorder_release_date,
58
+ :price,
59
+ :price_hidden_label,
60
+ :rating_count,
61
+ :rating_total,
62
+ :related_products,
63
+ :retail_price,
64
+ :sale_price,
65
+ :search_keywords,
66
+ :sku,
67
+ :sort_order,
68
+ :tax_class_id,
69
+ :total_sold,
70
+ :type,
71
+ :upc,
72
+ :videos,
73
+ :view_count,
74
+ :warranty,
75
+ :weight,
76
+ :width
77
+
8
78
  # has_many
9
- attr_accessor :options_hash, :skus_hash, :rules_hash, :images_hash
79
+ attr_accessor :options_hash,
80
+ :skus_hash,
81
+ :rules_hash,
82
+ :images_hash
10
83
 
11
84
  # has_one
12
- attr_accessor :option_set_resource, :brand_resource
85
+ attr_accessor :option_set_resource,
86
+ :brand_resource
13
87
 
14
- # reserved
88
+ # reserved
15
89
  attr_accessor :product_type
16
90
 
17
91
 
18
- has_many :skus, :rules, :images, {:options => :product_options}
19
- has_one :brand, :option_set
92
+ has_many :skus,
93
+ :rules,
94
+ :images,
95
+ { options: :product_options }
96
+
97
+ has_one :brand,
98
+ :option_set
99
+
20
100
  belongs_to :tax_class
21
-
22
101
  end
23
-
24
- end
102
+ end
@@ -1,10 +1,11 @@
1
1
  module BigcommerceAPI
2
-
3
2
  class ProductOption < Resource
4
- attr_accessor :id, :display_name, :option_id, :sort_order, :is_required
3
+ attr_accessor :id,
4
+ :display_name,
5
+ :is_required,
6
+ :option_id,
7
+ :sort_order
5
8
 
6
9
  belongs_to :option
7
-
8
10
  end
9
-
10
- end
11
+ end
@@ -1,20 +1,10 @@
1
- module BigcommerceAPI
1
+ module BigcommerceAPI
2
2
 
3
3
  class Resource < Base
4
4
  attr_accessor :errors
5
5
 
6
- def initialize(data)
7
- data.each do |k, v|
8
- if v and v.is_a? String
9
- val = v.gsub(/\n/, '').gsub(/\t/, '').strip
10
- else
11
- val = v
12
- end
13
- k = "#{k}_hash" if !self.class.has_many_options.nil? and self.class.has_many_options.include? k
14
- k = "#{k}_resource" if !self.class.has_one_options.nil? and self.class.has_one_options.include? k
15
- k = "#{self.resource}_#{k}" if k == 'type'
16
- send(:"#{k}=", val) if self.respond_to? "#{k}="
17
- end
6
+ def initialize(data)
7
+ self.assign_attributes(data)
18
8
  self.attributes_were = data
19
9
  end
20
10
 
@@ -24,7 +14,7 @@ module BigcommerceAPI
24
14
  end
25
15
 
26
16
  def save
27
- url = self.resource_url
17
+ url = self.resource_url
28
18
  if self.id.nil?
29
19
  # delete the parent id if there is one
30
20
  self.send(self.parent + '_id=', nil) if !self.parent.nil?
@@ -33,30 +23,52 @@ module BigcommerceAPI
33
23
  else
34
24
  # only send updated attributes
35
25
  attrs = self.attributes
36
- body = Hash.new
26
+ body = Hash.new
37
27
  self.changed.each{|c| body[c] = attrs[c]}
28
+ body.delete('date_modified')
38
29
  response = BigcommerceAPI::Resource.http_request(:put, "/#{url}/#{self.id}", :body => body.to_json)
39
30
  end
40
- if response.success?
41
- return self.id.nil? ? self.class.new(response.parsed_response) : true
42
- else
43
- self.errors = response.parsed_response
44
- return false
31
+
32
+ self.class.new(response.parsed_response)
33
+ end
34
+
35
+ def update_attributes(attributes)
36
+ assign_attributes(attributes) && save
37
+ end
38
+
39
+ def assign_attributes(attributes)
40
+ attributes.each do |k, v|
41
+ if v and v.is_a? String
42
+ val = v.gsub(/\n/, '').gsub(/\t/, '').strip
43
+ else
44
+ val = v
45
+ end
46
+ k = "#{k}_hash" if !self.class.has_many_options.nil? and self.class.has_many_options.include? k
47
+ k = "#{k}_resource" if !self.class.has_one_options.nil? and self.class.has_one_options.include? k
48
+ k = "#{self.resource}_#{k}" if k == 'type'
49
+ send(:"#{k}=", val) if self.respond_to? "#{k}="
45
50
  end
46
51
  end
47
52
 
48
- def create(params={})
53
+ def create
49
54
  # delete the parent id if there is one
50
55
  url = self.resource_url
51
56
  self.send(self.parent + '_id=', nil) if !self.parent.nil?
52
57
 
53
- response = BigcommerceAPI::Resource.http_request(:post, "/#{url}", :body => date_adjust(params).to_json)
54
- if response.success?
55
- return self.class.new(response.parsed_response)
56
- else
57
- self.errors = response.parsed_response
58
- return false
59
- end
58
+ attrs = self.attributes
59
+ body = Hash.new
60
+ self.changed.each{|c| body[c] = attrs[c]}
61
+
62
+ response = BigcommerceAPI::Resource.http_request(:post, "/#{url}", :body => body.to_json)
63
+
64
+ return self.class.new(response.parsed_response)
65
+ end
66
+
67
+ def delete
68
+ url = self.resource_url
69
+ BigcommerceAPI::Resource.http_request(:delete, "/#{url}/#{self.id}")
70
+
71
+ return true
60
72
  end
61
73
 
62
74
  def find_for_reload
@@ -92,7 +104,7 @@ module BigcommerceAPI
92
104
  return changed
93
105
  end
94
106
 
95
- class << self
107
+ class << self
96
108
  attr_accessor :has_many_options, :has_one_options, :belongs_to_options
97
109
 
98
110
  def has_many(*names)
@@ -134,7 +146,7 @@ module BigcommerceAPI
134
146
  end
135
147
  end
136
148
  end
137
-
149
+
138
150
  def belongs_to(*names)
139
151
  self.belongs_to_options = names.collect{|x| x.is_a?(Hash) ? x.keys.first.to_s : x.to_s}
140
152
  names.each do |m|
@@ -156,41 +168,73 @@ module BigcommerceAPI
156
168
  end
157
169
  end
158
170
 
159
- def resource
160
- out = self.name.split('::').last.downcase
161
- last = out.split(//).last(1).to_s
171
+ def resource
172
+ out = self.name.split('::').last.downcase
173
+ last = out.split(//).last.to_s
162
174
  if last == 'y'
163
175
  out = out.chomp('y') + 'ies'
164
- elsif last == 's'
165
- out += 'es'
166
- else
167
- out += 's'
168
- end
169
- return out
170
- end
171
-
172
- def all(params={})
173
- resources = BigcommerceAPI::Resource.http_request(:get, "/#{resource}", :query => date_adjust(params))
174
- (resources.success? and !resources.nil?) ? resources.collect{|r| self.new(r)} : []
175
- end
176
-
177
- def find(id)
176
+ elsif last == 's'
177
+ out += 'es'
178
+ else
179
+ out += 's'
180
+ end
181
+ return out
182
+ end
183
+
184
+ def all(params={})
185
+ resources = BigcommerceAPI::Resource.http_request(:get, "/#{resource}", :query => date_adjust(params))
186
+ (resources.success? and !resources.nil?) ? resources.collect{|r| self.new(r)} : []
187
+ end
188
+
189
+ def find(id)
190
+ return if id.blank?
178
191
  r = BigcommerceAPI::Resource.http_request(:get, "/#{resource}/#{id}")
179
- (r.success? and !r.nil?) ? self.new(r) : nil
180
- end
192
+ (r.success? and !r.nil?) ? self.new(r) : nil
193
+ end
181
194
 
182
195
  def http_request(verb, url, options={})
183
196
  begin
184
- BigcommerceAPI::Base.send(verb, url, options)
197
+ response = BigcommerceAPI::Base.send(verb, url, options)
198
+ if response.code >= 400
199
+ message = case response.code
200
+ when 429
201
+ "Too many requests, please retry in #{response.headers["x-retry-after"]} second."
202
+ when 500
203
+ "Internal Error"
204
+ else
205
+ parse_errors(response)
206
+ end
207
+ raise BigcommerceAPI::Error.new(response.code, message)
208
+ end
209
+ response
185
210
  rescue SocketError => e
186
211
  BigcommerceAPI::Result.new(:success => false, :errors => "Invalid URL")
187
212
  end
188
213
  end
189
- end # end class methods
214
+
215
+ private
216
+
217
+ # recursive function to convert hash into string, e.g. {a: {b: "c"}, d: "e"} becomes "c e"
218
+ def hash_to_s(hash)
219
+ if hash.is_a?(Array)
220
+ hash.map do |value|
221
+ hash_to_s(value)
222
+ end.to_sentence
223
+ elsif hash.is_a?(Hash)
224
+ hash_to_s(hash.values)
225
+ else
226
+ hash.to_s.gsub(/[,.]$/, '')
227
+ end
228
+ end
229
+
230
+ def parse_errors(response)
231
+ hash_to_s(response.parsed_response)
232
+ end
233
+ end # end class methods
190
234
 
191
235
  private
192
- attr_accessor :attributes_were
236
+ attr_accessor :attributes_were
193
237
 
194
238
  end
195
239
 
196
- end
240
+ end