bigcommerce 1.0.0.beta → 1.0.1

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 +115 -39
  3. data/bigcommerce.gemspec +12 -13
  4. data/lib/bigcommerce.rb +5 -34
  5. data/lib/bigcommerce/config.rb +14 -0
  6. data/lib/bigcommerce/connection.rb +26 -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 +22 -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 +47 -166
  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: f89e22fe6e4042c5231912f0594eccc6e20cf2d2
4
+ data.tar.gz: e72dc04e6ea9894c47740882e89edfc14ceb84ae
5
5
  SHA512:
6
- metadata.gz: 4b520cad6ba40ea401e4a79e15ed7d0308dc8d87599382a6cb6b02972b4e30729351832cbbccedff8054fae635b6d769c0d9970528ed448d90e5a21de483602f
7
- data.tar.gz: 81357d00e6c8cd9eec5a2d8123c68c33c36b7bb686369d82e80e30b8cbb6b9b378041f54cd21d64878e53508cedd613cb7d0aab8c4fb1183c1f04f6eb18e66ae
6
+ metadata.gz: ab56f2ab83b837201fc23c4e749633b47702a0ef48d081854b14ce220760f130675200a1513f2a8ba516419a2cf44b2e2da120289ab458130de333e5756f8077
7
+ data.tar.gz: b35e6c9009776ad58a0565d8d8785de254dfba5f1cadd10980c8c133d7fa70038485008c15bbcd6cf9f5f11f8e886b940c45ce269cb8e9a51e995207a70a53c9
data/README.md CHANGED
@@ -1,83 +1,79 @@
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'
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
+ 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
+ Also very important: For the OAuth authentication mechanism, the resources to which you have acccess depend on the scopes that the merchant has granted to your application. For more information about 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 that you can select, 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 these apps in their BigCommerce stores.
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
+ To authenticate your API client, you will need to configure the client like the following examples.
50
46
 
51
- #### Single Click (Public Apps):
47
+ ### OAuth App
52
48
 
53
- - ```client_id```: Obtained from the "My Apps" section on the [developer portal](http://developer.bigcommerce.com).
49
+ - ```client_id```: Obtained from the on the BigCommerce [Developer Portal's](http://developer.bigcommerce.com) "My Apps" section.
54
50
  - ```access_token```: Obtained after a token exchange in the auth callback.
55
51
  - ```store_hash```: Also obtained after the token exchange.
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 on which to authenticate.
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
- 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.
76
+ If you are using your own, self-signed, certificate, you can pass SSL options to Faraday. This is not required, but might be useful in special edge cases.
81
77
 
82
78
  ```rb
83
79
  Bigcommerce.configure do |config|
@@ -94,7 +90,87 @@ end
94
90
  For more information about configuring SSL with Faraday, please see the following:
95
91
 
96
92
  - [Faraday SSL example](https://gist.github.com/mislav/938183)
97
- - [Setting up SSL certificates](https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates)
93
+ - [Faraday: Setting up SSL certificates](https://github.com/lostisland/faraday/wiki/Setting-up-SSL-certificates)
94
+
95
+ ### Customer Login API
96
+ If you want to generate tokens for storefront login using the Customer Login API, you need to configure your app's client secret.
97
+
98
+ - ```store_hash```: The store hash of the store you are operating against.
99
+ - ```client_id```: Obtained from the on the BigCommerce [Developer Portal's](http://developer.bigcommerce.com) "My Apps" section.
100
+ - ```client_secret```: Obtained from the on the BigCommerce [Developer Portal's](http://developer.bigcommerce.com) "My Apps" section.
101
+
102
+ ```rb
103
+ Bigcommerce.configure do |config|
104
+ config.store_hash = ENV['BC_STORE_HASH']
105
+ config.client_id = ENV['BC_CLIENT_ID']
106
+ config.client_secret = ENV['BC_CLIENT_SECRET']
107
+ end
108
+ ```
109
+
110
+ ## Usage
111
+ For full examples of using the API client, please see the [examples folder](examples) and refer to BigCommerce's [developer documentation](https://developer.bigcommerce.com/api).
112
+
113
+ Example:
114
+
115
+ ```rb
116
+ # Configure the client to talk to a given store
117
+ Bigcommerce.configure do |config|
118
+ config.store_hash = ENV['BC_STORE_HASH']
119
+ config.client_id = ENV['BC_CLIENT_ID']
120
+ config.access_token = ENV['BC_ACCESS_TOKEN']
121
+ end
122
+
123
+ # Make an API request for a given resource
124
+ Bigcommerce::System.time
125
+ => #<Bigcommerce::System time=1466801314>
126
+ ```
127
+
128
+ ### Thread Safety
129
+
130
+ The `Bigcommerce.configure` method is NOT thread-safe. This mechanism is designed for applications or CLI's (command-line interfaces) where thread safety is not a concern. If you need to guarantee thread safety, we support this alternative mechanism to make thread-safe API requests:
131
+
132
+ Rather then setting up a single `connection` for all API requests, you would construct a new connection for each thread. If you can ensure that each of these connections is stored in a thread-safe manner, you can pass the `connection` as you query the resource.
133
+
134
+ This connection is nothing more than a `Faraday::Connection` – so if you want to write your own, or to use your own adapters, you can feel free. Please refer to this gem's [connection class](https://github.com/bigcommerce/bigcommerce-api-ruby/blob/master/lib/bigcommerce/connection.rb) for more details.
135
+
136
+ ##### OAuth
137
+
138
+ ```rb
139
+ connection = Bigcommerce::Connection.build(
140
+ Bigcommerce::Config.new(
141
+ store_hash: ENV['BC_STORE_HASH'],
142
+ client_id: ENV['BC_CLIENT_ID'],
143
+ access_token: ENV['BC_ACCESS_TOKEN']
144
+ )
145
+ )
146
+ => #<Faraday::Connection:0x007fbf95068978 ... >>
147
+
148
+ Bigcommerce::System.time(connection: connection)
149
+ => #<Bigcommerce::System time=1466546702>
150
+
151
+ Bigcommerce::System.raw_request(:get, 'time', connection: connection)
152
+ => #<Faraday::Response:0x007fd4a4063170 ... >>
153
+ ```
154
+
155
+ ##### Basic Auth
156
+
157
+ ```rb
158
+ connection_legacy = Bigcommerce::Connection.build(
159
+ Bigcommerce::Config.new(
160
+ auth: 'legacy',
161
+ url: ENV['BC_API_ENDPOINT_LEGACY'],
162
+ username: ENV['BC_USERNAME'],
163
+ api_key: ENV['BC_API_KEY']
164
+ )
165
+ )
166
+ => #<Faraday::Connection:0x007fbf95068978 ... >>
167
+
168
+ Bigcommerce::System.time(connection: connection_legacy)
169
+ => #<Bigcommerce::System time=1466546702>
170
+
171
+ Bigcommerce::System.raw_request(:get, 'time', connection: connection_legacy)
172
+ => #<Faraday::Response:0x007fd4a4063170 ... >>
173
+ ```
98
174
 
99
175
  ## Contributing
100
176
  See [CONTRIBUTING.md](CONTRIBUTING.md)
@@ -4,25 +4,24 @@ 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 = ['BigCommerce Engineering']
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.11'
24
+ s.add_dependency 'faraday_middleware', '~> 0.11'
25
+ s.add_dependency 'hashie', '~> 3.4'
26
+ s.add_dependency 'jwt', '~> 1.5.4'
28
27
  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
- attr_reader :api
24
- attr_accessor :api_limit
15
+ attr_reader :api, :config
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,26 @@
1
+ module Bigcommerce
2
+ module Connection
3
+ HEADERS = {
4
+ 'accept' => 'application/json',
5
+ 'content-type' => 'application/json',
6
+ 'user-agent' => 'bigcommerce-api-ruby',
7
+ 'accept-encoding' => 'gzip'
8
+ }.freeze
9
+
10
+ def self.build(config)
11
+ ssl_options = config.ssl || {}
12
+ Faraday.new(url: config.api_url, ssl: ssl_options) do |conn|
13
+ conn.request :json
14
+ conn.headers = HEADERS
15
+ if config.auth == 'legacy'
16
+ conn.use Faraday::Request::BasicAuthentication, config.username, config.api_key
17
+ else
18
+ conn.use Bigcommerce::Middleware::Auth, config
19
+ end
20
+ conn.use Bigcommerce::Middleware::HttpException
21
+ conn.use FaradayMiddleware::Gzip
22
+ conn.adapter Faraday.default_adapter
23
+ end
24
+ end
25
+ end
26
+ 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 StandardError
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