Skip to content

Commit f07301e

Browse files
committed
support v3 script
1 parent f2f885a commit f07301e

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

CHANGELOG.md

+1
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

+16
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::Script.all(connection: connection_v3)
189+
```
190+
175191
## Contributing
176192
See [CONTRIBUTING.md](CONTRIBUTING.md)

lib/bigcommerce/config.rb

+2-2
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
class Script < Resource
8+
include Bigcommerce::ResourceActions.new uri: 'content/scripts/%s'
9+
10+
property :id
11+
property :name
12+
property :description
13+
property :html
14+
property :src
15+
property :auto_uninstall
16+
property :load_method
17+
property :script_tag
18+
property :src
19+
property :visibility
20+
21+
end
22+
end

spec/bigcommerce/bigcommerce_spec.rb

+25
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'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RSpec.describe Bigcommerce::Script do
2+
before(:each) { @script = Bigcommerce::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)