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

Lines changed: 13 additions & 3 deletions
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

Lines changed: 0 additions & 20 deletions
This file was deleted.

bigcommerce.gemspec

Lines changed: 1 addition & 0 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 4 additions & 33 deletions
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

Lines changed: 14 additions & 0 deletions
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

Lines changed: 24 additions & 0 deletions
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

Lines changed: 8 additions & 9 deletions
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

Lines changed: 8 additions & 8 deletions
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)