Skip to content

Commit cdc661c

Browse files
authored
Merge pull request #123 from pedelman/thread_safety
Add thread safe method of querying BigCommerce resources.
2 parents 4f5f23c + a6dcfbd commit cdc661c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+203
-207
lines changed

.rubocop.yml

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1-
Metrics/LineLength:
2-
Max: 120
31
AllCops:
42
Exclude:
53
- spec/**/*
64
- .bundle/**/*
75
- bin/**/*
86
- vendor/**/*
9-
inherit_from: .rubocop_todo.yml
7+
8+
Metrics/LineLength:
9+
Max: 120
10+
11+
Metrics/AbcSize:
12+
Max: 25
13+
14+
Metrics/MethodLength:
15+
CountComments: false
16+
Max: 25
17+
18+
Style/Documentation:
19+
Enabled: false

.rubocop_todo.yml

-20
This file was deleted.

bigcommerce.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ require 'bigcommerce/version'
55
Gem::Specification.new do |s|
66
s.name = 'bigcommerce'
77
s.version = Bigcommerce::VERSION
8+
s.platform = Gem::Platform::RUBY
89
s.required_ruby_version = '>= 2.0.0'
910
s.license = 'MIT'
1011

examples/configuration/legacy_auth.rb

-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@
99
end
1010

1111
puts Bigcommerce::System.time
12-
puts Bigcommerce.api_limit

lib/bigcommerce.rb

+4-33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
require 'hashie'
22
require 'faraday_middleware'
33
require 'bigcommerce/version'
4+
require 'bigcommerce/config'
5+
require 'bigcommerce/connection'
46
require 'bigcommerce/middleware/auth'
57
require 'bigcommerce/middleware/http_exception'
68
require 'bigcommerce/resources/resource'
@@ -9,43 +11,12 @@ module Bigcommerce
911
resources = File.join(File.dirname(__FILE__), 'bigcommerce', 'resources', '**', '*.rb')
1012
Dir.glob(resources, &method(:require))
1113

12-
DEFAULTS = {
13-
base_url: 'https://api.bigcommerce.com'
14-
}.freeze
15-
16-
HEADERS = {
17-
'accept' => 'application/json',
18-
'content-type' => 'application/json',
19-
'user-agent' => 'bigcommerce-api-ruby'
20-
}.freeze
21-
2214
class << self
2315
attr_reader :api
24-
attr_accessor :api_limit
2516

2617
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)
4920
end
5021
end
5122
end

lib/bigcommerce/config.rb

+14
Original file line numberDiff line numberDiff line change
@@ -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

lib/bigcommerce/connection.rb

+24
Original file line numberDiff line numberDiff line change
@@ -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

lib/bigcommerce/request.rb

+8-9
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,29 @@ def included(base)
4343
end
4444

4545
module ClassMethods
46-
def get(path, params = nil)
46+
def get(path, params = {})
4747
response = raw_request(:get, path, params)
4848
build_response_object response
4949
end
5050

51-
def delete(path)
52-
response = raw_request(:delete, path)
51+
def delete(path, params = {})
52+
response = raw_request(:delete, path, params)
5353
response.body
5454
end
5555

56-
def post(path, params)
56+
def post(path, params = {})
5757
response = raw_request(:post, path, params)
5858
build_response_object response
5959
end
6060

61-
def put(path, params)
61+
def put(path, params = {})
6262
response = raw_request(:put, path, params)
6363
build_response_object response
6464
end
6565

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)
7069
end
7170

7271
private

lib/bigcommerce/resource_actions.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,27 @@ def all(params = {})
2424
get path.build, params
2525
end
2626

27-
def find(resource_id)
27+
def find(resource_id, params = {})
2828
raise ArgumentError if resource_id.nil?
29-
get path.build(resource_id)
29+
get path.build(resource_id), params
3030
end
3131

32-
def create(params)
32+
def create(params = {})
3333
post path.build, params
3434
end
3535

36-
def update(resource_id, params)
36+
def update(resource_id, params = {})
3737
raise ArgumentError if resource_id.nil?
3838
put path.build(resource_id), params
3939
end
4040

41-
def destroy(resource_id)
41+
def destroy(resource_id, params = {})
4242
raise ArgumentError if resource_id.nil?
43-
delete path.build(resource_id)
43+
delete path.build(resource_id), params
4444
end
4545

46-
def destroy_all
47-
delete path.build
46+
def destroy_all(params = {})
47+
delete path.build, params
4848
end
4949
end
5050
end

lib/bigcommerce/resources/content/blog_post.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ class BlogPost < Resource
2222
property :thumbnail_path
2323
property :count
2424

25-
def self.count
26-
get 'blog/posts/count'
25+
def self.count(params = {})
26+
get 'blog/posts/count', params
2727
end
2828
end
2929
end

lib/bigcommerce/resources/content/blog_tag.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class BlogTag < Resource
99
property :tag
1010
property :post_ids
1111

12-
def self.all
13-
get path.build
12+
def self.all(params = {})
13+
get path.build, params
1414
end
1515
end
1616
end

lib/bigcommerce/resources/content/redirect.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Redirect < Resource
1414
property :forward
1515
property :url
1616

17-
def self.count
18-
get 'redirects/count'
17+
def self.count(params = {})
18+
get 'redirects/count', params
1919
end
2020
end
2121
end

lib/bigcommerce/resources/customers/customer.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Customer < Resource
2323
property :addresses
2424
property :tax_exempt_category
2525

26-
def self.count
27-
get 'customers/count'
26+
def self.count(params = {})
27+
get 'customers/count', params
2828
end
2929
end
3030
end

lib/bigcommerce/resources/customers/customer_address.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ class CustomerAddress < Resource
2323
property :country_iso2
2424
property :phone
2525

26-
def self.count_all
27-
get 'customers/addresses/count'
26+
def self.count_all(params = {})
27+
get 'customers/addresses/count', params
2828
end
2929

30-
def self.count(customer_id)
31-
get "customers/#{customer_id}/addresses/count"
30+
def self.count(customer_id, params = {})
31+
get "customers/#{customer_id}/addresses/count", params
3232
end
3333
end
3434
end

lib/bigcommerce/resources/customers/customer_group.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class CustomerGroup < Resource
1414
property :category_access
1515
property :discount_rules
1616

17-
def self.count
18-
get 'customer_groups/count'
17+
def self.count(params = {})
18+
get 'customer_groups/count', params
1919
end
2020
end
2121
end

lib/bigcommerce/resources/geography/country.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Country < Resource
1616
property :country_iso3
1717
property :states
1818

19-
def self.count
20-
get 'countries/count'
19+
def self.count(params = {})
20+
get 'countries/count', params
2121
end
2222
end
2323
end

lib/bigcommerce/resources/geography/state.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class State < Resource
1515
property :state_abbreviation
1616
property :country_id
1717

18-
def self.count(country_id)
19-
get "countries/#{country_id}/states/count"
18+
def self.count(country_id, params = {})
19+
get "countries/#{country_id}/states/count", params
2020
end
2121

22-
def self.count_all
23-
get 'countries/states/count'
22+
def self.count_all(params = {})
23+
get 'countries/states/count', params
2424
end
2525
end
2626
end

lib/bigcommerce/resources/marketing/coupon.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Coupon < Resource
2323
property :restricted_to
2424
property :shipping_methods
2525

26-
def self.count
27-
get 'coupons/count'
26+
def self.count(params = {})
27+
get 'coupons/count', params
2828
end
2929
end
3030
end

lib/bigcommerce/resources/marketing/gift_certificates.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Gift Certificates
22
# Code that can be applied by customers to their order to provide full or partial payment.
3-
# No URL at this time
3+
# https://developer.bigcommerce.com/api/stores/v2/gift_certificates
44

55
module Bigcommerce
66
class GiftCertificates < Resource

lib/bigcommerce/resources/orders/order_product.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ class OrderProduct < Resource
5454
property :parent_order_product_id
5555
property :count
5656

57-
def self.count(order_id)
58-
get "orders/#{order_id}/products/count"
57+
def self.count(order_id, params = {})
58+
get "orders/#{order_id}/products/count", params
5959
end
6060

61-
def self.count_all
62-
get 'orders/products/count'
61+
def self.count_all(params = {})
62+
get 'orders/products/count', params
6363
end
6464
end
6565
end

lib/bigcommerce/resources/orders/order_shipping_address.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ class OrderShippingAddress < Resource
4040
property :handling_cost_tax_class_id
4141
property :count
4242

43-
def self.count(order_id)
44-
get "orders/#{order_id}/shipping_addresses/count"
43+
def self.count(order_id, params = {})
44+
get "orders/#{order_id}/shipping_addresses/count", params
4545
end
4646

47-
def self.count_all
48-
get 'orders/shipping_addresses/count'
47+
def self.count_all(params = {})
48+
get 'orders/shipping_addresses/count', params
4949
end
5050
end
5151
end

lib/bigcommerce/resources/orders/shipment.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ class Shipment < Resource
2020
property :shipping_provider
2121
property :count
2222

23-
def self.count(order_id)
24-
get "orders/#{order_id}/shipments/count"
23+
def self.count(order_id, params = {})
24+
get "orders/#{order_id}/shipments/count", params
2525
end
2626

27-
def self.count_all
28-
get 'orders/shipments/count'
27+
def self.count_all(params = {})
28+
get 'orders/shipments/count', params
2929
end
3030
end
3131
end

0 commit comments

Comments
 (0)