bigcommerce 0.10.0 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +620 -0
  5. data/.travis.yml +15 -0
  6. data/CHANGELOG.md +11 -0
  7. data/CONTRIBUTING.md +119 -0
  8. data/DEPENDENCIES.md +7 -0
  9. data/Gemfile +15 -0
  10. data/Guardfile +22 -0
  11. data/LICENSE +15 -15
  12. data/README.md +61 -70
  13. data/RELEASING.md +64 -0
  14. data/Rakefile +8 -6
  15. data/bigcommerce.gemspec +22 -53
  16. data/examples/README.md +148 -0
  17. data/examples/configuration/legacy_auth.rb +12 -0
  18. data/examples/configuration/oauth.rb +9 -0
  19. data/examples/content/blog_post.rb +39 -0
  20. data/examples/content/blog_tag.rb +10 -0
  21. data/examples/content/redirect.rb +43 -0
  22. data/examples/customers/customer.rb +36 -0
  23. data/examples/customers/customer_address.rb +50 -0
  24. data/examples/customers/customer_group.rb +35 -0
  25. data/examples/exception_handling.rb +50 -0
  26. data/examples/geography/country.rb +16 -0
  27. data/examples/geography/state.rb +19 -0
  28. data/examples/marketing/coupon.rb +44 -0
  29. data/examples/orders/order.rb +53 -0
  30. data/examples/orders/order_coupon.rb +17 -0
  31. data/examples/orders/order_message.rb +17 -0
  32. data/examples/orders/order_product.rb +23 -0
  33. data/examples/orders/order_shipping_address.rb +23 -0
  34. data/examples/orders/order_status.rb +15 -0
  35. data/examples/orders/order_tax.rb +17 -0
  36. data/examples/orders/shipment.rb +57 -0
  37. data/examples/payments/payment_method.rb +10 -0
  38. data/examples/products/brand.rb +36 -0
  39. data/examples/products/bulk_pricing_rule.rb +47 -0
  40. data/examples/products/category.rb +37 -0
  41. data/examples/products/configurable_field.rb +29 -0
  42. data/examples/products/custom_field.rb +44 -0
  43. data/examples/products/google_product_search_mapping.rb +12 -0
  44. data/examples/products/option.rb +37 -0
  45. data/examples/products/option_set.rb +35 -0
  46. data/examples/products/option_set_option.rb +50 -0
  47. data/examples/products/option_value.rb +43 -0
  48. data/examples/products/product.rb +44 -0
  49. data/examples/products/product_image.rb +42 -0
  50. data/examples/products/product_option.rb +17 -0
  51. data/examples/products/product_review.rb +12 -0
  52. data/examples/products/product_rule.rb +54 -0
  53. data/examples/products/product_video.rb +45 -0
  54. data/examples/products/sku.rb +48 -0
  55. data/examples/shipping/shipping_method.rb +13 -0
  56. data/examples/store/store_info.rb +10 -0
  57. data/examples/system/time.rb +10 -0
  58. data/examples/tax/tax_class.rb +13 -0
  59. data/examples/webhooks/webhook.rb +29 -0
  60. data/lib/bigcommerce.rb +50 -8
  61. data/lib/bigcommerce/exception.rb +51 -0
  62. data/lib/bigcommerce/middleware/auth.rb +16 -0
  63. data/lib/bigcommerce/middleware/http_exception.rb +14 -0
  64. data/lib/bigcommerce/request.rb +89 -0
  65. data/lib/bigcommerce/resource_actions.rb +51 -0
  66. data/lib/bigcommerce/resources/content/blog_post.rb +31 -0
  67. data/lib/bigcommerce/resources/content/blog_tag.rb +17 -0
  68. data/lib/bigcommerce/resources/content/redirect.rb +21 -0
  69. data/lib/bigcommerce/resources/customers/customer.rb +30 -0
  70. data/lib/bigcommerce/resources/customers/customer_address.rb +34 -0
  71. data/lib/bigcommerce/resources/customers/customer_group.rb +21 -0
  72. data/lib/bigcommerce/resources/geography/country.rb +22 -0
  73. data/lib/bigcommerce/resources/geography/state.rb +25 -0
  74. data/lib/bigcommerce/resources/marketing/coupon.rb +30 -0
  75. data/lib/bigcommerce/resources/orders/order.rb +73 -0
  76. data/lib/bigcommerce/resources/orders/order_coupon.rb +20 -0
  77. data/lib/bigcommerce/resources/orders/order_message.rb +23 -0
  78. data/lib/bigcommerce/resources/orders/order_product.rb +64 -0
  79. data/lib/bigcommerce/resources/orders/order_shipping_address.rb +50 -0
  80. data/lib/bigcommerce/resources/orders/order_status.rb +16 -0
  81. data/lib/bigcommerce/resources/orders/order_tax.rb +23 -0
  82. data/lib/bigcommerce/resources/orders/shipment.rb +30 -0
  83. data/lib/bigcommerce/resources/payments/payment_method.rb +17 -0
  84. data/lib/bigcommerce/resources/products/brand.rb +23 -0
  85. data/lib/bigcommerce/resources/products/bulk_pricing_rule.rb +26 -0
  86. data/lib/bigcommerce/resources/products/category.rb +29 -0
  87. data/lib/bigcommerce/resources/products/configurable_field.rb +30 -0
  88. data/lib/bigcommerce/resources/products/custom_field.rb +24 -0
  89. data/lib/bigcommerce/resources/products/google_product_search_mapping.rb +26 -0
  90. data/lib/bigcommerce/resources/products/option.rb +20 -0
  91. data/lib/bigcommerce/resources/products/option_set.rb +18 -0
  92. data/lib/bigcommerce/resources/products/option_set_option.rb +19 -0
  93. data/lib/bigcommerce/resources/products/option_value.rb +15 -0
  94. data/lib/bigcommerce/resources/products/product.rb +97 -0
  95. data/lib/bigcommerce/resources/products/product_image.rb +31 -0
  96. data/lib/bigcommerce/resources/products/product_option.rb +17 -0
  97. data/lib/bigcommerce/resources/products/product_review.rb +22 -0
  98. data/lib/bigcommerce/resources/products/product_rule.rb +31 -0
  99. data/lib/bigcommerce/resources/products/product_video.rb +24 -0
  100. data/lib/bigcommerce/resources/products/sku.rb +29 -0
  101. data/lib/bigcommerce/resources/resource.rb +10 -0
  102. data/lib/bigcommerce/resources/shipping/shipping_method.rb +15 -0
  103. data/lib/bigcommerce/resources/store/store_information.rb +37 -0
  104. data/lib/bigcommerce/resources/system/time.rb +15 -0
  105. data/lib/bigcommerce/resources/tax/tax_class.rb +15 -0
  106. data/lib/bigcommerce/resources/webhooks/webhook.rb +22 -0
  107. data/lib/bigcommerce/subresource_actions.rb +43 -0
  108. data/lib/bigcommerce/version.rb +1 -4
  109. data/spec/bigcommerce/bigcommerce_spec.rb +76 -0
  110. data/spec/bigcommerce/unit/actions_spec.rb +151 -0
  111. data/spec/bigcommerce/unit/exception_spec.rb +53 -0
  112. data/spec/bigcommerce/unit/middleware/auth_spec.rb +18 -0
  113. data/spec/bigcommerce/unit/middleware/http_exception_spec.rb +40 -0
  114. data/spec/bigcommerce/unit/request_spec.rb +180 -0
  115. data/spec/bigcommerce/unit/resources/content/blog_post_spec.rb +12 -0
  116. data/spec/bigcommerce/unit/resources/content/blog_tag_spec.rb +12 -0
  117. data/spec/bigcommerce/unit/resources/content/redirect_spec.rb +12 -0
  118. data/spec/bigcommerce/unit/resources/customers/customer_address_spec.rb +21 -0
  119. data/spec/bigcommerce/unit/resources/customers/customer_group_spec.rb +12 -0
  120. data/spec/bigcommerce/unit/resources/customers/customer_spec.rb +12 -0
  121. data/spec/bigcommerce/unit/resources/geography/country_spec.rb +12 -0
  122. data/spec/bigcommerce/unit/resources/geography/state_spec.rb +21 -0
  123. data/spec/bigcommerce/unit/resources/marketing/coupon_spec.rb +12 -0
  124. data/spec/bigcommerce/unit/resources/orders/order_product_spec.rb +21 -0
  125. data/spec/bigcommerce/unit/resources/orders/order_shipping_address_spec.rb +21 -0
  126. data/spec/bigcommerce/unit/resources/orders/order_spec.rb +13 -0
  127. data/spec/bigcommerce/unit/resources/orders/shipment_spec.rb +21 -0
  128. data/spec/bigcommerce/unit/resources/payments/payment_method_spec.rb +23 -0
  129. data/spec/bigcommerce/unit/resources/products/brand_spec.rb +12 -0
  130. data/spec/bigcommerce/unit/resources/products/bulk_pricing_rule_spec.rb +21 -0
  131. data/spec/bigcommerce/unit/resources/products/category_spec.rb +12 -0
  132. data/spec/bigcommerce/unit/resources/products/configurable_field_spec.rb +21 -0
  133. data/spec/bigcommerce/unit/resources/products/custom_field_spec.rb +21 -0
  134. data/spec/bigcommerce/unit/resources/products/google_product_search_mapping_spec.rb +14 -0
  135. data/spec/bigcommerce/unit/resources/products/option_set_spec.rb +12 -0
  136. data/spec/bigcommerce/unit/resources/products/option_spec.rb +12 -0
  137. data/spec/bigcommerce/unit/resources/products/product_image_spec.rb +21 -0
  138. data/spec/bigcommerce/unit/resources/products/product_review_spec.rb +14 -0
  139. data/spec/bigcommerce/unit/resources/products/product_rule_spec.rb +21 -0
  140. data/spec/bigcommerce/unit/resources/products/product_spec.rb +13 -0
  141. data/spec/bigcommerce/unit/resources/products/product_video_spec.rb +21 -0
  142. data/spec/bigcommerce/unit/resources/products/sku_spec.rb +21 -0
  143. data/spec/bigcommerce/unit/resources/resource_spec.rb +4 -0
  144. data/spec/bigcommerce/unit/resources/store_info/store_information_spec.rb +12 -0
  145. data/spec/bigcommerce/unit/resources/system/time_spec.rb +12 -0
  146. data/spec/bigcommerce/unit/version_spec.rb +7 -0
  147. data/spec/spec_helper.rb +9 -25
  148. metadata +203 -127
  149. data/lib/big_commerce.rb +0 -1
  150. data/lib/bigcommerce/api.rb +0 -473
  151. data/lib/bigcommerce/connection.rb +0 -171
  152. data/lib/bigcommerce/product.rb +0 -13
  153. data/lib/bigcommerce/resource.rb +0 -50
  154. data/spec/big_commerce_spec.rb +0 -9
  155. data/spec/integration/orders_spec.rb +0 -18
  156. data/spec/support/integration_context.rb +0 -13
  157. data/spec/support/mock_api_context.rb +0 -10
  158. data/spec/unit/api_request_spec.rb +0 -34
  159. data/spec/unit/api_spec.rb +0 -49
  160. data/spec/unit/connection_spec.rb +0 -23
  161. data/spec/unit/date_time_spec.rb +0 -31
  162. data/spec/unit/version_spec.rb +0 -13
@@ -0,0 +1,12 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ @product = Bigcommerce::Product.all[0]
10
+
11
+ # List product reviews
12
+ puts Bigcommerce::ProductReview.all(@product.id)
@@ -0,0 +1,54 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ @product = Bigcommerce::Product.all[0]
10
+
11
+ # List product rules
12
+ @product_rules = Bigcommerce::ProductRule.all(@product.id)
13
+ puts @product_rules
14
+
15
+ # Get a count of product rules
16
+ puts Bigcommerce::ProductRule.count(@product.id)
17
+
18
+ # Get a count of all product rules for all products
19
+ puts Bigcommerce::ProductRule.count_all
20
+
21
+ # Create a product rule
22
+ @product_rule = Bigcommerce::ProductRule.create(
23
+ @product.id,
24
+ conditions: [
25
+ {
26
+ product_option_id: 95,
27
+ option_value_id: 13,
28
+ sku_id: nil
29
+ }
30
+ ]
31
+ )
32
+ puts @product_rule
33
+
34
+ # Get a product rule
35
+ puts Bigcommerce::ProductRule.find(@product.id, @product_rule.id)
36
+
37
+ # Update a product rule
38
+ puts Bigcommerce::ProductRule.update(
39
+ @product.id,
40
+ @product_rule.id,
41
+ conditions: [
42
+ {
43
+ product_option_id: 95,
44
+ option_value_id: 12,
45
+ sku_id: nil
46
+ }
47
+ ]
48
+ )
49
+
50
+ # Delete a product rule
51
+ puts Bigcommerce::ProductRule.destroy(@product.id, @product_rule.id)
52
+
53
+ # Delete all product rules
54
+ # puts Bigcommerce::ProductRule.destroy_all(@product.id)
@@ -0,0 +1,45 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ @product = Bigcommerce::Product.all[0]
10
+
11
+ # Create a product video
12
+ @product_video = Bigcommerce::ProductVideo.create(
13
+ @product.id,
14
+ url: 'https://www.youtube.com/watch?v=T9RFb8xXZlk'
15
+ )
16
+ puts @product_video
17
+
18
+ # List product videos
19
+ @product_videos = Bigcommerce::ProductVideo.all(@product.id)
20
+ puts @product_videos
21
+
22
+ # Get a count of product videos
23
+ puts Bigcommerce::ProductVideo.count(@product.id)
24
+
25
+ # Get a count of all product videos for all products
26
+ puts Bigcommerce::ProductVideo.count_all
27
+
28
+ # Get a product video
29
+ puts Bigcommerce::ProductVideo.find(@product.id, @product_video.id)
30
+
31
+ # Update a product video
32
+ # TODO
33
+ # puts Bigcommerce::ProductVideo.update(
34
+ # @product.id,
35
+ # @product_video.id,
36
+ # url: 'https://www.youtube.com/watch?v=LXgTp40Y3zo'
37
+ # )
38
+
39
+ # Delete a product video
40
+ # TODO
41
+ # puts Bigcommerce::ProductVideo.destroy(@product.id, product_video.id)
42
+
43
+ # Delete all product videos
44
+ # TODO
45
+ # puts Bigcommerce::ProductVideo.destroy_all(@product.id)
@@ -0,0 +1,48 @@
1
+ require 'bigcommerce'
2
+ require 'securerandom'
3
+
4
+ Bigcommerce.configure do |config|
5
+ config.store_hash = ENV['BC_STORE_HASH']
6
+ config.client_id = ENV['BC_CLIENT_ID']
7
+ config.access_token = ENV['BC_ACCESS_TOKEN']
8
+ end
9
+
10
+ @product = Bigcommerce::Product.all[0]
11
+
12
+ # Create a product sku
13
+ @sku = Bigcommerce::Sku.create(
14
+ @product.id,
15
+ sku: SecureRandom.hex,
16
+ options: [
17
+ product_option_id: 95,
18
+ option_value_id: 10
19
+ ]
20
+ )
21
+ puts @sku
22
+
23
+ # List product skus
24
+ @skus = Bigcommerce::Sku.all(@product.id)
25
+ puts @skus
26
+
27
+ # Get a count of product skus
28
+ puts Bigcommerce::Sku.count
29
+
30
+ # Get a product sku
31
+ puts Bigcommerce::Sku.find(@product.id, @sku.id)
32
+
33
+ # Update a product sku
34
+ puts Bigcommerce::Sku.update(
35
+ @product.id,
36
+ @sku.id,
37
+ sku: SecureRandom.hex,
38
+ options: [
39
+ product_option_id: 95,
40
+ option_value_id: 7
41
+ ]
42
+ )
43
+
44
+ # Delete a product image
45
+ puts Bigcommerce::Sku.destroy(@product.id, @sku.id)
46
+
47
+ # Delete all product skus
48
+ # puts Bigcommerce::Sku.destroy_all(@product.id)
@@ -0,0 +1,13 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ # List shipping methods
10
+ puts Bigcommerce::ShippingMethod.all
11
+
12
+ # Get a shipping method
13
+ puts Bigcommerce::ShippingMethod.find(1)
@@ -0,0 +1,10 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ # Get store information
10
+ puts Bigcommerce::StoreInfo.info
@@ -0,0 +1,10 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ # Get current server time
10
+ puts Bigcommerce::System.time
@@ -0,0 +1,13 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ # List tax classes
10
+ puts Bigcommerce::TaxClass.all
11
+
12
+ # Get a tax classes
13
+ puts Bigcommerce::TaxClass.find(1)
@@ -0,0 +1,29 @@
1
+ require 'bigcommerce'
2
+
3
+ Bigcommerce.configure do |config|
4
+ config.store_hash = ENV['BC_STORE_HASH']
5
+ config.client_id = ENV['BC_CLIENT_ID']
6
+ config.access_token = ENV['BC_ACCESS_TOKEN']
7
+ end
8
+
9
+ # List all webhooks
10
+ puts Bigcommerce::Webhook.all
11
+
12
+ # Get a webhook
13
+ puts Bigcommerce::Webhook.find(1)
14
+
15
+ # Create a webhook
16
+ @webhook = Bigcommerce::Webhook.create(
17
+ scope: 'store/order/*',
18
+ destination: 'https://app.example.com/orders'
19
+ )
20
+
21
+ # Update a webhook
22
+ puts Bigcommerce::Webhook.update(
23
+ @webhook.id,
24
+ scope: 'store/order/*',
25
+ destination: 'https://app.example.com/products'
26
+ )
27
+
28
+ # Delete a webhook
29
+ puts Bigcommerce::Webhook.destroy(@webhook.id)
@@ -1,9 +1,51 @@
1
- require "cgi"
2
- require "json"
3
- require 'rest-client'
4
- require "uri"
5
- require 'bigcommerce/api'
6
- require 'bigcommerce/connection'
1
+ require 'hashie'
2
+ require 'faraday_middleware'
7
3
  require 'bigcommerce/version'
8
- require 'bigcommerce/resource'
9
- require 'bigcommerce/product'
4
+ require 'bigcommerce/middleware/auth'
5
+ require 'bigcommerce/middleware/http_exception'
6
+ require 'bigcommerce/resources/resource'
7
+
8
+ module Bigcommerce
9
+ resources = File.join(File.dirname(__FILE__), 'bigcommerce', 'resources', '**', '*.rb')
10
+ Dir.glob(resources, &method(:require))
11
+
12
+ DEFAULTS = {
13
+ base_url: 'https://api.bigcommerce.com'
14
+ }
15
+
16
+ HEADERS = {
17
+ 'accept' => 'application/json',
18
+ 'content-type' => 'application/json',
19
+ 'user-agent' => 'bigcommerce-api-ruby'
20
+ }
21
+
22
+ class << self
23
+ attr_reader :api
24
+ attr_accessor :api_limit
25
+
26
+ def configure
27
+ config = Hashie::Mash.new
28
+ yield(config)
29
+ ssl_options = config.ssl if config.auth == 'legacy'
30
+
31
+ @api = Faraday.new(url: build_url(config), ssl: ssl_options) do |conn|
32
+ conn.request :json
33
+ conn.headers = HEADERS
34
+ if config.auth == 'legacy'
35
+ conn.use Faraday::Request::BasicAuthentication, config.username, config.api_key
36
+ else
37
+ conn.use Bigcommerce::Middleware::Auth, config
38
+ end
39
+ conn.use Bigcommerce::Middleware::HttpException
40
+ conn.adapter Faraday.default_adapter
41
+ end
42
+ end
43
+
44
+ def build_url(config)
45
+ return config.url if config.auth == 'legacy'
46
+
47
+ base = ENV['BC_API_ENDPOINT'].nil? ? DEFAULTS[:base_url] : ENV['BC_API_ENDPOINT']
48
+ "#{base}/stores/#{config.store_hash}/v2"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,51 @@
1
+ module Bigcommerce
2
+ class HttpError < StandardError
3
+ attr_accessor :response_headers
4
+ def initialize(headers)
5
+ @response_headers = headers
6
+ end
7
+ end
8
+
9
+ class BadRequest < HttpError; end
10
+ class Unauthorized < HttpError; end
11
+ class Forbidden < HttpError; end
12
+ class NotFound < HttpError; end
13
+ class MethodNotAllowed < HttpError; end
14
+ class NotAccepted < HttpError; end
15
+ class TimeOut < HttpError; end
16
+ class ResourceConflict < HttpError; end
17
+ class TooManyRequests < HttpError; end
18
+ class InternalServerError < HttpError; end
19
+ class BadGateway < HttpError; end
20
+ class ServiceUnavailable < HttpError; end
21
+ class GatewayTimeout < HttpError; end
22
+ class BandwidthLimitExceeded < HttpError; end
23
+
24
+ module HttpErrors
25
+ ERRORS = {
26
+ 400 => Bigcommerce::BadRequest,
27
+ 401 => Bigcommerce::Unauthorized,
28
+ 403 => Bigcommerce::Forbidden,
29
+ 404 => Bigcommerce::NotFound,
30
+ 405 => Bigcommerce::MethodNotAllowed,
31
+ 406 => Bigcommerce::NotAccepted,
32
+ 408 => Bigcommerce::TimeOut,
33
+ 409 => Bigcommerce::ResourceConflict,
34
+ 429 => Bigcommerce::TooManyRequests,
35
+ 500 => Bigcommerce::InternalServerError,
36
+ 502 => Bigcommerce::BadGateway,
37
+ 503 => Bigcommerce::ServiceUnavailable,
38
+ 504 => Bigcommerce::GatewayTimeout,
39
+ 509 => Bigcommerce::BandwidthLimitExceeded
40
+ }
41
+
42
+ def throw_http_exception!(code, env)
43
+ return unless ERRORS.keys.include? code
44
+ error_hash = env.body.empty? ? {} : JSON.parse(env.body, symbolize_names: true)
45
+ unless env[:response_headers]['X-Retry-After'].nil?
46
+ error_hash[:retry_after] = env[:response_headers]['X-Retry-After'].to_i
47
+ end
48
+ fail ERRORS[code].new(error_hash), env.body
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,16 @@
1
+ module Bigcommerce
2
+ module Middleware
3
+ class Auth < Faraday::Middleware
4
+ def initialize(app, options = {})
5
+ @app = app
6
+ @options = options
7
+ end
8
+
9
+ def call(env)
10
+ env[:request_headers]['X-Auth-Client'] = @options[:client_id]
11
+ env[:request_headers]['X-Auth-Token'] = @options[:access_token]
12
+ @app.call env
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ require 'bigcommerce/exception'
2
+
3
+ module Bigcommerce
4
+ module Middleware
5
+ class HttpException < Faraday::Response::Middleware
6
+ include Bigcommerce::HttpErrors
7
+
8
+ def on_complete(env)
9
+ throw_http_exception! env[:status].to_i, env
10
+ env
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,89 @@
1
+ require 'json'
2
+
3
+ module Bigcommerce
4
+ class PathBuilder
5
+ attr_reader :uri
6
+
7
+ def initialize(uri)
8
+ @uri = uri
9
+ end
10
+
11
+ # This takes the @uri and inserts the keys to form a path.
12
+ # To start we make sure that for nil/numeric values, we wrap those into an
13
+ # array. We then scan the string for %d and %s to find the number of times
14
+ # we possibly need to insert keys into the URI. Next, we check the size of
15
+ # the keys array, if the keys size is less than the number of possible keys
16
+ # in the URI, we will remove the trailing %d or %s, then remove the
17
+ # trailing /. We then pass the keys into the uri to form the path.
18
+ # ex. foo/%d/bar/%d => foo/1/bar/2
19
+ def build(keys = [])
20
+ keys = [] if keys.nil?
21
+ keys = [keys] if keys.is_a? Numeric
22
+ ids = uri.scan('%d').count + uri.scan('%s').count
23
+ str = ids > keys.size ? uri.chomp('%d').chomp('%s').chomp('/') : uri
24
+ (str % keys).chomp('/')
25
+ end
26
+
27
+ def to_s
28
+ @uri
29
+ end
30
+ end
31
+
32
+ class Request < Module
33
+ def initialize(uri)
34
+ @uri = uri
35
+ end
36
+
37
+ def included(base)
38
+ base.extend ClassMethods
39
+ path_builder = PathBuilder.new @uri
40
+ base.define_singleton_method :path do
41
+ path_builder
42
+ end
43
+ end
44
+
45
+ module ClassMethods
46
+ def get(path, params = nil)
47
+ response = raw_request(:get, path, params)
48
+ build_response_object response
49
+ end
50
+
51
+ def delete(path)
52
+ response = raw_request(:delete, path)
53
+ response.body
54
+ end
55
+
56
+ def post(path, params)
57
+ response = raw_request(:post, path, params)
58
+ build_response_object response
59
+ end
60
+
61
+ def put(path, params)
62
+ response = raw_request(:put, path, params)
63
+ build_response_object response
64
+ end
65
+
66
+ def raw_request(method, path, params = nil)
67
+ response = Bigcommerce.api.send(method, path.to_s, params)
68
+ Bigcommerce.api_limit = response.headers['X-BC-ApiLimit-Remaining']
69
+ response
70
+ end
71
+
72
+ private
73
+
74
+ def build_response_object(response)
75
+ json = parse response.body
76
+ if json.is_a? Array
77
+ json.map { |obj| new obj }
78
+ else
79
+ new json
80
+ end
81
+ end
82
+
83
+ def parse(json)
84
+ return [] if json.empty?
85
+ JSON.parse(json, symbolize_names: true)
86
+ end
87
+ end
88
+ end
89
+ end