bigcommerce 1.0.0.beta → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +98 -36
  3. data/bigcommerce.gemspec +11 -13
  4. data/lib/bigcommerce.rb +4 -33
  5. data/lib/bigcommerce/config.rb +14 -0
  6. data/lib/bigcommerce/connection.rb +24 -0
  7. data/lib/bigcommerce/exception.rb +25 -18
  8. data/lib/bigcommerce/request.rb +8 -9
  9. data/lib/bigcommerce/resource_actions.rb +11 -11
  10. data/lib/bigcommerce/resources/content/blog_post.rb +3 -5
  11. data/lib/bigcommerce/resources/content/blog_tag.rb +2 -3
  12. data/lib/bigcommerce/resources/content/redirect.rb +2 -2
  13. data/lib/bigcommerce/resources/customers/customer.rb +2 -2
  14. data/lib/bigcommerce/resources/customers/customer_address.rb +5 -5
  15. data/lib/bigcommerce/resources/customers/customer_group.rb +2 -2
  16. data/lib/bigcommerce/resources/geography/country.rb +4 -3
  17. data/lib/bigcommerce/resources/geography/state.rb +6 -5
  18. data/lib/bigcommerce/resources/marketing/banner.rb +21 -0
  19. data/lib/bigcommerce/resources/marketing/coupon.rb +2 -2
  20. data/lib/bigcommerce/resources/marketing/gift_certificates.rb +25 -0
  21. data/lib/bigcommerce/resources/orders/order.rb +46 -44
  22. data/lib/bigcommerce/resources/orders/order_coupon.rb +5 -5
  23. data/lib/bigcommerce/resources/orders/order_message.rb +8 -7
  24. data/lib/bigcommerce/resources/orders/order_product.rb +37 -36
  25. data/lib/bigcommerce/resources/orders/order_shipping_address.rb +24 -23
  26. data/lib/bigcommerce/resources/orders/order_status.rb +2 -1
  27. data/lib/bigcommerce/resources/orders/order_tax.rb +8 -7
  28. data/lib/bigcommerce/resources/orders/shipment.rb +11 -10
  29. data/lib/bigcommerce/resources/products/brand.rb +2 -2
  30. data/lib/bigcommerce/resources/products/bulk_pricing_rule.rb +6 -5
  31. data/lib/bigcommerce/resources/products/category.rb +2 -2
  32. data/lib/bigcommerce/resources/products/configurable_field.rb +6 -5
  33. data/lib/bigcommerce/resources/products/custom_field.rb +6 -5
  34. data/lib/bigcommerce/resources/products/google_product_search_mapping.rb +9 -8
  35. data/lib/bigcommerce/resources/products/option.rb +2 -2
  36. data/lib/bigcommerce/resources/products/option_set.rb +2 -2
  37. data/lib/bigcommerce/resources/products/option_set_option.rb +2 -1
  38. data/lib/bigcommerce/resources/products/option_value.rb +1 -0
  39. data/lib/bigcommerce/resources/products/product.rb +61 -61
  40. data/lib/bigcommerce/resources/products/product_image.rb +4 -4
  41. data/lib/bigcommerce/resources/products/product_option.rb +2 -1
  42. data/lib/bigcommerce/resources/products/product_review.rb +1 -5
  43. data/lib/bigcommerce/resources/products/product_rule.rb +4 -4
  44. data/lib/bigcommerce/resources/products/product_video.rb +4 -4
  45. data/lib/bigcommerce/resources/products/sku.rb +15 -9
  46. data/lib/bigcommerce/resources/shipping/shipping_method.rb +2 -1
  47. data/lib/bigcommerce/resources/store/store_information.rb +18 -14
  48. data/lib/bigcommerce/resources/system/time.rb +2 -2
  49. data/lib/bigcommerce/resources/tax/tax_class.rb +2 -1
  50. data/lib/bigcommerce/resources/webhooks/webhook.rb +2 -1
  51. data/lib/bigcommerce/subresource_actions.rb +14 -14
  52. data/lib/bigcommerce/version.rb +1 -1
  53. metadata +32 -165
  54. data/.gitignore +0 -17
  55. data/.rspec +0 -2
  56. data/.rubocop.yml +0 -620
  57. data/.travis.yml +0 -15
  58. data/CHANGELOG.md +0 -11
  59. data/CONTRIBUTING.md +0 -119
  60. data/DEPENDENCIES.md +0 -7
  61. data/Gemfile +0 -15
  62. data/Guardfile +0 -22
  63. data/LICENSE +0 -20
  64. data/RELEASING.md +0 -64
  65. data/Rakefile +0 -13
  66. data/examples/README.md +0 -148
  67. data/examples/configuration/legacy_auth.rb +0 -12
  68. data/examples/configuration/oauth.rb +0 -9
  69. data/examples/content/blog_post.rb +0 -39
  70. data/examples/content/blog_tag.rb +0 -10
  71. data/examples/content/redirect.rb +0 -43
  72. data/examples/customers/customer.rb +0 -36
  73. data/examples/customers/customer_address.rb +0 -50
  74. data/examples/customers/customer_group.rb +0 -35
  75. data/examples/exception_handling.rb +0 -50
  76. data/examples/geography/country.rb +0 -16
  77. data/examples/geography/state.rb +0 -19
  78. data/examples/marketing/coupon.rb +0 -44
  79. data/examples/orders/order.rb +0 -53
  80. data/examples/orders/order_coupon.rb +0 -17
  81. data/examples/orders/order_message.rb +0 -17
  82. data/examples/orders/order_product.rb +0 -23
  83. data/examples/orders/order_shipping_address.rb +0 -23
  84. data/examples/orders/order_status.rb +0 -15
  85. data/examples/orders/order_tax.rb +0 -17
  86. data/examples/orders/shipment.rb +0 -57
  87. data/examples/payments/payment_method.rb +0 -10
  88. data/examples/products/brand.rb +0 -36
  89. data/examples/products/bulk_pricing_rule.rb +0 -47
  90. data/examples/products/category.rb +0 -37
  91. data/examples/products/configurable_field.rb +0 -29
  92. data/examples/products/custom_field.rb +0 -44
  93. data/examples/products/google_product_search_mapping.rb +0 -12
  94. data/examples/products/option.rb +0 -37
  95. data/examples/products/option_set.rb +0 -35
  96. data/examples/products/option_set_option.rb +0 -50
  97. data/examples/products/option_value.rb +0 -43
  98. data/examples/products/product.rb +0 -44
  99. data/examples/products/product_image.rb +0 -42
  100. data/examples/products/product_option.rb +0 -17
  101. data/examples/products/product_review.rb +0 -12
  102. data/examples/products/product_rule.rb +0 -54
  103. data/examples/products/product_video.rb +0 -45
  104. data/examples/products/sku.rb +0 -48
  105. data/examples/shipping/shipping_method.rb +0 -13
  106. data/examples/store/store_info.rb +0 -10
  107. data/examples/system/time.rb +0 -10
  108. data/examples/tax/tax_class.rb +0 -13
  109. data/examples/webhooks/webhook.rb +0 -29
  110. data/spec/bigcommerce/bigcommerce_spec.rb +0 -76
  111. data/spec/bigcommerce/unit/actions_spec.rb +0 -151
  112. data/spec/bigcommerce/unit/exception_spec.rb +0 -53
  113. data/spec/bigcommerce/unit/middleware/auth_spec.rb +0 -18
  114. data/spec/bigcommerce/unit/middleware/http_exception_spec.rb +0 -40
  115. data/spec/bigcommerce/unit/request_spec.rb +0 -180
  116. data/spec/bigcommerce/unit/resources/content/blog_post_spec.rb +0 -12
  117. data/spec/bigcommerce/unit/resources/content/blog_tag_spec.rb +0 -12
  118. data/spec/bigcommerce/unit/resources/content/redirect_spec.rb +0 -12
  119. data/spec/bigcommerce/unit/resources/customers/customer_address_spec.rb +0 -21
  120. data/spec/bigcommerce/unit/resources/customers/customer_group_spec.rb +0 -12
  121. data/spec/bigcommerce/unit/resources/customers/customer_spec.rb +0 -12
  122. data/spec/bigcommerce/unit/resources/geography/country_spec.rb +0 -12
  123. data/spec/bigcommerce/unit/resources/geography/state_spec.rb +0 -21
  124. data/spec/bigcommerce/unit/resources/marketing/coupon_spec.rb +0 -12
  125. data/spec/bigcommerce/unit/resources/orders/order_product_spec.rb +0 -21
  126. data/spec/bigcommerce/unit/resources/orders/order_shipping_address_spec.rb +0 -21
  127. data/spec/bigcommerce/unit/resources/orders/order_spec.rb +0 -13
  128. data/spec/bigcommerce/unit/resources/orders/shipment_spec.rb +0 -21
  129. data/spec/bigcommerce/unit/resources/payments/payment_method_spec.rb +0 -23
  130. data/spec/bigcommerce/unit/resources/products/brand_spec.rb +0 -12
  131. data/spec/bigcommerce/unit/resources/products/bulk_pricing_rule_spec.rb +0 -21
  132. data/spec/bigcommerce/unit/resources/products/category_spec.rb +0 -12
  133. data/spec/bigcommerce/unit/resources/products/configurable_field_spec.rb +0 -21
  134. data/spec/bigcommerce/unit/resources/products/custom_field_spec.rb +0 -21
  135. data/spec/bigcommerce/unit/resources/products/google_product_search_mapping_spec.rb +0 -14
  136. data/spec/bigcommerce/unit/resources/products/option_set_spec.rb +0 -12
  137. data/spec/bigcommerce/unit/resources/products/option_spec.rb +0 -12
  138. data/spec/bigcommerce/unit/resources/products/product_image_spec.rb +0 -21
  139. data/spec/bigcommerce/unit/resources/products/product_review_spec.rb +0 -14
  140. data/spec/bigcommerce/unit/resources/products/product_rule_spec.rb +0 -21
  141. data/spec/bigcommerce/unit/resources/products/product_spec.rb +0 -13
  142. data/spec/bigcommerce/unit/resources/products/product_video_spec.rb +0 -21
  143. data/spec/bigcommerce/unit/resources/products/sku_spec.rb +0 -21
  144. data/spec/bigcommerce/unit/resources/resource_spec.rb +0 -4
  145. data/spec/bigcommerce/unit/resources/store_info/store_information_spec.rb +0 -12
  146. data/spec/bigcommerce/unit/resources/system/time_spec.rb +0 -12
  147. data/spec/bigcommerce/unit/version_spec.rb +0 -7
  148. data/spec/spec_helper.rb +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d098924abf4c82d2daa1a8dfdd09068a8960e771
4
- data.tar.gz: d31b1ab3c0f9c52df72d16c06a00c72f615e21e0
3
+ metadata.gz: 699fc230ea09d9242c9cbfb7e1d2183891127627
4
+ data.tar.gz: 94e9e4eeb4a9b8cdf53ff2bd26f78612d8457d25
5
5
  SHA512:
6
- metadata.gz: 4b520cad6ba40ea401e4a79e15ed7d0308dc8d87599382a6cb6b02972b4e30729351832cbbccedff8054fae635b6d769c0d9970528ed448d90e5a21de483602f
7
- data.tar.gz: 81357d00e6c8cd9eec5a2d8123c68c33c36b7bb686369d82e80e30b8cbb6b9b378041f54cd21d64878e53508cedd613cb7d0aab8c4fb1183c1f04f6eb18e66ae
6
+ metadata.gz: 131581aaa53a9933c978f7649530128857e7a46e467c091b77effc27828ca895388bc78edfdd0cf5bb58bb14f8041a55417b90f52af98aab82480c3748563cc0
7
+ data.tar.gz: 88d65c0fa84ad4d4968e58b7780eaac14408d5ad123eafe1c1b158c04822ade1007cbf3c460c95235765efdb548baa0c34a4778b9a60283b8f45c4efa07479a1
data/README.md CHANGED
@@ -1,54 +1,50 @@
1
- # Bigcommerce
1
+ # BigCommerce API Ruby
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/bigcommerce.png)](https://rubygems.org/gems/bigcommerce)
4
- [![Build Status](https://travis-ci.org/bigcommerce/bigcommerce-api-ruby.png?branch=master)](https://travis-ci.org/bigcommerce/bigcommerce-api-ruby)
5
- [![Dependency Status](https://gemnasium.com/bigcommerce/bigcommerce-api-ruby.png?travis)](https://gemnasium.com/bigcommerce/bigcommerce-api-ruby)
6
- [![Code Climate](https://codeclimate.com/github/bigcommerce/bigcommerce-api-ruby.png)](https://codeclimate.com/github/bigcommerce/bigcommerce-api-ruby)
7
- [![Coverage Status](https://coveralls.io/repos/bigcommerce/bigcommerce-api-ruby/badge.png?branch=master)](https://coveralls.io/r/bigcommerce/bigcommerce-api-ruby?branch=master)
3
+ [![Gem Version](https://badge.fury.io/rb/bigcommerce.svg)](https://rubygems.org/gems/bigcommerce)
4
+ [![Build Status](https://travis-ci.org/bigcommerce/bigcommerce-api-ruby.svg?branch=master)](https://travis-ci.org/bigcommerce/bigcommerce-api-ruby)
5
+ [![Dependency Status](https://gemnasium.com/bigcommerce/bigcommerce-api-ruby.svg?travis)](https://gemnasium.com/bigcommerce/bigcommerce-api-ruby)
8
6
 
9
- This is the official Bigcommerce API client to support our Rest API. You can find more information about becoming a Bigcommerce developer here: [developer.bigcommerce.com](http://developer.bigcommerce.com).
10
-
11
- #### ⚠️ A note about the current client: ⚠️
12
- This is a preview release of the 1.0.0 version of the Bigcommerce API Client. Please report issues if they come up.
13
-
14
- We have introduced a new major version of the API client and it is a complete rewrite (for the better). If you want to see the old version of the API client, please view it here: [Bigcommerce API client v0.x](https://github.com/bigcommerce/bigcommerce-api-ruby/tree/0.x). We recommend that developers upgrade to the latest client, but we will still support our developers who are unable to upgrade.
7
+ This is the official BigCommerce API client to support our Stores API. You can find more information about becoming a BigCommerce developer here: [developer.bigcommerce.com](http://developer.bigcommerce.com).
15
8
 
16
9
 
17
10
  ## Installation
18
- Bigcommerce is available on Rubygems:
11
+ BigCommerce is available on [RubyGems](https://rubygems.org/gems/bigcommerce).
19
12
 
20
13
  ```sh
21
- gem install bigcommerce --pre
14
+ gem install bigcommerce
22
15
  ```
23
16
 
24
17
  You can also add it to your Gemfile.
25
18
 
26
19
  ```rb
27
- gem 'bigcommerce', '>= 1.0.0.beta'
20
+ gem 'bigcommerce', '~> 1.0.0'
28
21
  ```
29
22
 
23
+ ## Requirements
24
+ - Ruby 2.0.0 or newer. Please refer to the `.travis.yml` to see which versions we officially support.
25
+
30
26
  ## Getting Started
31
- In order to make requests to our API, you must register as a developer and have your credentials ready.
27
+ In order to make requests to our API, you must register as a developer and have your credentials ready.
32
28
 
33
- We currently have two different authentication schemes you can use depending on your use-case.
29
+ Its also very important to note that for the OAuth authentication mechanism, the resources you have acccess to depend on the scopes which your application has been granted by the merchant. For more information about the Store API scopes, see: [OAuth Scopes](https://developer.bigcommerce.com/api/scopes).
34
30
 
35
- #### Public Apps
36
- Public apps can be submitted to Bigcommerce App Store, allowing other businesses to install it in their Bigcommerce stores.
31
+ ## Authentication
32
+ We currently have two different authentication schemes you can use depending on your use case.
37
33
 
38
- [More Information](https://developer.bigcommerce.com/api/using-oauth-intro)
34
+ ### OAuth
35
+ OAuth apps can be submitted to [BigCommerce App Store](https://www.bigcommerce.com/apps), allowing other merchants to install it in their BigCommerce store.
39
36
 
40
- #### Private Apps
41
- To develop a custom integration for one store, your app needs to use Basic Authentication.
37
+ __[More Information](https://developer.bigcommerce.com/api/using-oauth-intro)__
42
38
 
43
- [More Information](https://developer.bigcommerce.com/api/legacy/basic-auth)
39
+ ### Basic Authentication (Legacy)
40
+ To develop a custom integration for one store, your app needs to use Basic Authentication.
44
41
 
45
- ## Usage
46
- For full examples on using the API client, please see the [examples folder](examples).
42
+ __[More Information](https://developer.bigcommerce.com/api/legacy/basic-auth)__
47
43
 
48
44
  ## Configuration
49
- In order to authenticate the API client, you will need to configure the client like this:
45
+ In order to authenticate the API client, you will need to configure the client like the following.
50
46
 
51
- #### Single Click (Public Apps):
47
+ ### OAuth App
52
48
 
53
49
  - ```client_id```: Obtained from the "My Apps" section on the [developer portal](http://developer.bigcommerce.com).
54
50
  - ```access_token```: Obtained after a token exchange in the auth callback.
@@ -56,26 +52,26 @@ In order to authenticate the API client, you will need to configure the client l
56
52
 
57
53
  ```rb
58
54
  Bigcommerce.configure do |config|
59
- config.store_hash = 'store_hash'
60
- config.client_id = 'client_id'
61
- config.access_token = 'access_token'
55
+ config.store_hash = ENV['BC_STORE_HASH']
56
+ config.client_id = ENV['BC_CLIENT_ID']
57
+ config.access_token = ENV['BC_ACCESS_TOKEN']
62
58
  end
63
59
  ```
64
60
 
65
- #### Private Apps:
61
+ ### Basic Authentication (Legacy)
66
62
 
67
- To get all the private app credentials, simply visit your store admin page and navigate to the Settings > Legacy API Settings. Once there, you can create a new username to authenticate with.
63
+ To get all the basic auth credentials, simply visit your store admin page and navigate to the `Advanced Settings > Legacy API Settings`. Once there, you can create a new legacy api account to authenticate with.
68
64
 
69
65
  ```rb
70
66
  Bigcommerce.configure do |config|
71
67
  config.auth = 'legacy'
72
- config.url = 'https://api_path.com'
73
- config.username = 'username'
74
- config.api_key = 'api_key'
68
+ config.url = ENV['BC_API_ENDPOINT_LEGACY']
69
+ config.username = ENV['BC_USERNAME']
70
+ config.api_key = ENV['BC_API_KEY']
75
71
  end
76
72
  ```
77
73
 
78
- __SSL Configuration:__
74
+ __SSL Configuration__
79
75
 
80
76
  If you are using your own self-signed certificate, you can pass SSL options to Faraday. This is not required, but may be useful in special edge cases.
81
77
 
@@ -96,5 +92,71 @@ For more information about configuring SSL with Faraday, please see the followin
96
92
  - [Faraday SSL example](https://gist.github.com/mislav/938183)
97
93
  - [Setting up SSL certificates](https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates)
98
94
 
95
+
96
+ ## Usage
97
+ For full examples on using the API client, please see the [examples folder](examples) and refer to the [developer documentation](https://developer.bigcommerce.com/api).
98
+
99
+ Example:
100
+
101
+ ```rb
102
+ # Configure the client to talk to a given store
103
+ Bigcommerce.configure do |config|
104
+ config.store_hash = ENV['BC_STORE_HASH']
105
+ config.client_id = ENV['BC_CLIENT_ID']
106
+ config.access_token = ENV['BC_ACCESS_TOKEN']
107
+ end
108
+
109
+ # Make an API request for a given resource
110
+ Bigcommerce::System.time
111
+ => #<Bigcommerce::System time=1466801314>
112
+ ```
113
+
114
+ ### Thread Safety
115
+
116
+ The `Bigcommerce.configure` method is NOT thread safe. This mechanism is designed for applications or cli where thread safety is not a concern. If you need to guarantee thread safety, we support another mechanism to make threadsafe API requests.
117
+
118
+ Rather then setting up a single `connection` for all API requests, you will want to construct a new connection for each thread. If you can make sure that each of these connections is stored in a thread safe manner, you can pass the `connection` as you query the resource.
119
+
120
+ This connection is nothing more than a `Faraday::Connection` so if you want to write your own, or use your own adapers, you can feel free. Please refer to the [connection class](https://github.com/bigcommerce/bigcommerce-api-ruby/blob/master/lib/bigcommerce/connection.rb) for more details.
121
+
122
+ ##### OAuth
123
+
124
+ ```rb
125
+ connection = Bigcommerce::Connection.build(
126
+ Bigcommerce::Config.new(
127
+ store_hash: ENV['BC_STORE_HASH'],
128
+ client_id: ENV['BC_CLIENT_ID'],
129
+ access_token: ENV['BC_ACCESS_TOKEN']
130
+ )
131
+ )
132
+ => #<Faraday::Connection:0x007fbf95068978 ... >>
133
+
134
+ Bigcommerce::System.time(connection: connection)
135
+ => #<Bigcommerce::System time=1466546702>
136
+
137
+ Bigcommerce::System.raw_request(:get, 'time', connection: connection)
138
+ => #<Faraday::Response:0x007fd4a4063170 ... >>
139
+ ```
140
+
141
+ ##### Basic Auth
142
+
143
+ ```rb
144
+ connection_legacy = Bigcommerce::Connection.build(
145
+ Bigcommerce::Config.new(
146
+ auth: 'legacy',
147
+ url: ENV['BC_API_ENDPOINT_LEGACY'],
148
+ username: ENV['BC_USERNAME'],
149
+ api_key: ENV['BC_API_KEY']
150
+ )
151
+ )
152
+ => #<Faraday::Connection:0x007fbf95068978 ... >>
153
+
154
+ Bigcommerce::System.time(connection: connection_legacy)
155
+ => #<Bigcommerce::System time=1466546702>
156
+
157
+ Bigcommerce::System.raw_request(:get, 'time', connection: connection_legacy)
158
+ => #<Faraday::Response:0x007fd4a4063170 ... >>
159
+ ```
160
+
99
161
  ## Contributing
100
162
  See [CONTRIBUTING.md](CONTRIBUTING.md)
@@ -4,25 +4,23 @@ require 'bigcommerce/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'bigcommerce'
7
- s.homepage = 'https://github.com/bigcommerce/bigcommerce-api-ruby'
8
- s.authors = ['Patrick Edelman']
9
7
  s.version = Bigcommerce::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.required_ruby_version = '>= 2.0.0'
10
10
  s.license = 'MIT'
11
11
 
12
- s.description = 'Bigcommerce API Ruby client library. Allows developers to \
13
- easily communicate with the Bigcommerce API, for either CLI tools or public \
14
- apps. More info: http://developer.bigcommerce.com'
15
- s.summary = 'Ruby client library for the Bigcommerce v2 API'
12
+ s.authors = ['Patrick Edelman']
13
+ s.homepage = 'https://github.com/bigcommerce/bigcommerce-api-ruby'
14
+ s.summary = 'Ruby client library for the BigCommerce API'
15
+ s.description = s.summary
16
16
 
17
- s.required_ruby_version = '>= 1.9.3'
18
17
  s.require_paths = ['lib']
19
- s.files = `git ls-files`.split($ORS)
20
- s.test_files = Dir['spec/**/*.rb']
18
+ s.files = Dir['README.md', 'lib/**/*', 'bigcommerce.gemspec']
21
19
 
22
- s.add_development_dependency 'rake'
23
20
  s.add_development_dependency 'bundler'
21
+ s.add_development_dependency 'rake'
24
22
 
25
- s.add_dependency 'faraday'
26
- s.add_dependency 'faraday_middleware'
27
- s.add_dependency 'hashie'
23
+ s.add_dependency 'faraday', '~> 0.9'
24
+ s.add_dependency 'faraday_middleware', '~> 0.10.0'
25
+ s.add_dependency 'hashie', '~> 3.4'
28
26
  end
@@ -1,6 +1,8 @@
1
1
  require 'hashie'
2
2
  require 'faraday_middleware'
3
3
  require 'bigcommerce/version'
4
+ require 'bigcommerce/config'
5
+ require 'bigcommerce/connection'
4
6
  require 'bigcommerce/middleware/auth'
5
7
  require 'bigcommerce/middleware/http_exception'
6
8
  require 'bigcommerce/resources/resource'
@@ -9,43 +11,12 @@ module Bigcommerce
9
11
  resources = File.join(File.dirname(__FILE__), 'bigcommerce', 'resources', '**', '*.rb')
10
12
  Dir.glob(resources, &method(:require))
11
13
 
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
14
  class << self
23
15
  attr_reader :api
24
- attr_accessor :api_limit
25
16
 
26
17
  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"
18
+ @config = Bigcommerce::Config.new.tap { |h| yield(h) }
19
+ @api = Bigcommerce::Connection.build(@config)
49
20
  end
50
21
  end
51
22
  end
@@ -0,0 +1,14 @@
1
+ module Bigcommerce
2
+ class Config < Hashie::Mash
3
+ DEFAULTS = {
4
+ base_url: 'https://api.bigcommerce.com'
5
+ }.freeze
6
+
7
+ def api_url
8
+ return url if auth == 'legacy'
9
+
10
+ base = ENV['BC_API_ENDPOINT'].to_s.empty? ? DEFAULTS[:base_url] : ENV['BC_API_ENDPOINT']
11
+ "#{base}/stores/#{store_hash}/v2"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ module Bigcommerce
2
+ module Connection
3
+ HEADERS = {
4
+ 'accept' => 'application/json',
5
+ 'content-type' => 'application/json',
6
+ 'user-agent' => 'bigcommerce-api-ruby'
7
+ }.freeze
8
+
9
+ def self.build(config)
10
+ ssl_options = config.ssl if config.auth == 'legacy'
11
+ Faraday.new(url: config.api_url, ssl: ssl_options) do |conn|
12
+ conn.request :json
13
+ conn.headers = HEADERS
14
+ if config.auth == 'legacy'
15
+ conn.use Faraday::Request::BasicAuthentication, config.username, config.api_key
16
+ else
17
+ conn.use Bigcommerce::Middleware::Auth, config
18
+ end
19
+ conn.use Bigcommerce::Middleware::HttpException
20
+ conn.adapter Faraday.default_adapter
21
+ end
22
+ end
23
+ end
24
+ end
@@ -6,19 +6,19 @@ module Bigcommerce
6
6
  end
7
7
  end
8
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
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
22
  class BandwidthLimitExceeded < HttpError; end
23
23
 
24
24
  module HttpErrors
@@ -37,15 +37,22 @@ module Bigcommerce
37
37
  503 => Bigcommerce::ServiceUnavailable,
38
38
  504 => Bigcommerce::GatewayTimeout,
39
39
  509 => Bigcommerce::BandwidthLimitExceeded
40
- }
40
+ }.freeze
41
41
 
42
42
  def throw_http_exception!(code, env)
43
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
44
+ response_headers = {}
45
+ unless env.body.empty?
46
+ response_headers = begin
47
+ JSON.parse(env.body, symbolize_names: true)
48
+ rescue
49
+ {}
50
+ end
47
51
  end
48
- fail ERRORS[code].new(error_hash), env.body
52
+ unless env[:response_headers] && env[:response_headers]['X-Retry-After'].nil?
53
+ response_headers[:retry_after] = env[:response_headers]['X-Retry-After'].to_i
54
+ end
55
+ raise ERRORS[code].new(response_headers), env.body
49
56
  end
50
57
  end
51
58
  end
@@ -43,30 +43,29 @@ module Bigcommerce
43
43
  end
44
44
 
45
45
  module ClassMethods
46
- def get(path, params = nil)
46
+ def get(path, params = {})
47
47
  response = raw_request(:get, path, params)
48
48
  build_response_object response
49
49
  end
50
50
 
51
- def delete(path)
52
- response = raw_request(:delete, path)
51
+ def delete(path, params = {})
52
+ response = raw_request(:delete, path, params)
53
53
  response.body
54
54
  end
55
55
 
56
- def post(path, params)
56
+ def post(path, params = {})
57
57
  response = raw_request(:post, path, params)
58
58
  build_response_object response
59
59
  end
60
60
 
61
- def put(path, params)
61
+ def put(path, params = {})
62
62
  response = raw_request(:put, path, params)
63
63
  build_response_object response
64
64
  end
65
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
66
+ def raw_request(method, path, params = {})
67
+ client = params.delete(:connection) || Bigcommerce.api
68
+ client.send(method, path.to_s, params)
70
69
  end
71
70
 
72
71
  private
@@ -24,27 +24,27 @@ module Bigcommerce
24
24
  get path.build, params
25
25
  end
26
26
 
27
- def find(resource_id)
28
- fail ArgumentError if resource_id.nil?
29
- get path.build(resource_id)
27
+ def find(resource_id, params = {})
28
+ raise ArgumentError if resource_id.nil?
29
+ get path.build(resource_id), params
30
30
  end
31
31
 
32
- def create(params)
32
+ def create(params = {})
33
33
  post path.build, params
34
34
  end
35
35
 
36
- def update(resource_id, params)
37
- fail ArgumentError if resource_id.nil?
36
+ def update(resource_id, params = {})
37
+ raise ArgumentError if resource_id.nil?
38
38
  put path.build(resource_id), params
39
39
  end
40
40
 
41
- def destroy(resource_id)
42
- fail ArgumentError if resource_id.nil?
43
- delete path.build(resource_id)
41
+ def destroy(resource_id, params = {})
42
+ raise ArgumentError if resource_id.nil?
43
+ delete path.build(resource_id), params
44
44
  end
45
45
 
46
- def destroy_all
47
- delete path.build
46
+ def destroy_all(params = {})
47
+ delete path.build, params
48
48
  end
49
49
  end
50
50
  end