Skip to content

Commit 5856469

Browse files
committed
Support Scripts (API v3)
1 parent f2f885a commit 5856469

File tree

8 files changed

+70
-4
lines changed

8 files changed

+70
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Next Release
22
Your contribution here.
3+
* [#149](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/149): Add support for script and v3 api_url structure. - [@tfx](https://github.com/tfx).
34

45
* [#000](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/000): Brief description here. - [@username](https://github.com/username).
56

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,21 @@ Bigcommerce::System.raw_request(:get, 'time', connection: connection_legacy)
172172
=> #<Faraday::Response:0x007fd4a4063170 ... >>
173173
```
174174

175+
### API v3 support
176+
177+
The `Bigcommerce::Script` requires a v3 api url, which can be achieved by adding `version` to the configuration:
178+
179+
```rb
180+
connection_v3 = Bigcommerce::Connection.build(
181+
Bigcommerce::Config.new(
182+
store_hash: ENV['BC_STORE_HASH'],
183+
client_id: ENV['BC_CLIENT_ID'],
184+
access_token: ENV['BC_ACCESS_TOKEN'],
185+
version: '3'
186+
)
187+
)
188+
Bigcommerce::V3::Script.all(connection: connection_v3)
189+
```
190+
175191
## Contributing
176192
See [CONTRIBUTING.md](CONTRIBUTING.md)

bigcommerce.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lib = File.expand_path('../lib', __FILE__)
1+
lib = File.expand_path('lib', __dir__)
22
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
33
require 'bigcommerce/version'
44

lib/bigcommerce/config.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class Config < Hashie::Mash
66

77
def api_url
88
return url if auth == 'legacy'
9-
9+
version = self.version || '2'
1010
base = ENV['BC_API_ENDPOINT'].to_s.empty? ? DEFAULTS[:base_url] : ENV['BC_API_ENDPOINT']
11-
"#{base}/stores/#{store_hash}/v2"
11+
"#{base}/stores/#{store_hash}/v#{version}"
1212
end
1313
end
1414
end

lib/bigcommerce/exception.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module HttpErrors
4040
}.freeze
4141

4242
def throw_http_exception!(code, env)
43-
return unless ERRORS.keys.include? code
43+
return unless ERRORS.key? code
4444
response_headers = {}
4545
unless env.body.empty?
4646
response_headers = begin
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Scripts
2+
# Scripts are used to create front-end scripts in Stencil theme
3+
# Need to use connection version v3
4+
# https://developer.bigcommerce.com/api/v3/#/reference/scripts/content-scripts/create-a-script
5+
6+
module Bigcommerce
7+
module V3
8+
class Script < Resource
9+
include Bigcommerce::ResourceActions.new uri: 'content/scripts/%s'
10+
11+
property :data
12+
end
13+
end
14+
end

spec/bigcommerce/bigcommerce_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@
33
api.instance_variable_get('@builder').instance_variable_get('@handlers')
44
end
55

6+
describe 'version configuration' do
7+
context 'default version' do
8+
it 'should return the api_url with default version 2' do
9+
Bigcommerce.configure do |config|
10+
config.access_token = 'jksdgkjbhksjdb'
11+
config.client_id = 'negskjgdjkbg'
12+
config.store_hash = 'some_store'
13+
end
14+
expect(Bigcommerce.config.api_url).to include('/v2')
15+
end
16+
end
17+
18+
context 'custom version' do
19+
it 'should return the api_url with custom version' do
20+
Bigcommerce.configure do |config|
21+
config.access_token = 'jksdgkjbhksjdb'
22+
config.client_id = 'negskjgdjkbg'
23+
config.store_hash = 'some_store'
24+
config.version = '3'
25+
end
26+
expect(Bigcommerce.config.api_url).to include('/v3')
27+
end
28+
end
29+
end
30+
631
it 'should return a faraday object when configured' do
732
Bigcommerce.configure do |config|
833
config.url = 'http://foobar.com'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RSpec.describe Bigcommerce::V3::Script do
2+
before(:each) { @script = Bigcommerce::V3::Script }
3+
4+
describe '.all' do
5+
it 'should hit the correct path' do
6+
expect(@script).to receive(:get).with(@script.path.build, {})
7+
@script.all
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)