Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for script and v3 api_url structure. #149

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Next Release
Your contribution here.
* [#149](https://github.com/bigcommerce/bigcommerce-api-ruby/pull/149): Add support for script and v3 api_url structure. - [@tfx](https://github.com/tfx).

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

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,21 @@ Bigcommerce::System.raw_request(:get, 'time', connection: connection_legacy)
=> #<Faraday::Response:0x007fd4a4063170 ... >>
```

### API v3 support

The `Bigcommerce::Script` requires a v3 api url, which can be achieved by adding `version` to the configuration:

```rb
connection_v3 = Bigcommerce::Connection.build(
Bigcommerce::Config.new(
store_hash: ENV['BC_STORE_HASH'],
client_id: ENV['BC_CLIENT_ID'],
access_token: ENV['BC_ACCESS_TOKEN'],
version: '3'
)
)
Bigcommerce::V3::Script.all(connection: connection_v3)
```

## Contributing
See [CONTRIBUTING.md](CONTRIBUTING.md)
2 changes: 1 addition & 1 deletion bigcommerce.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'bigcommerce/version'

Expand Down
2 changes: 1 addition & 1 deletion lib/bigcommerce/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def api_url
return url if auth == 'legacy'

base = ENV['BC_API_ENDPOINT'].to_s.empty? ? DEFAULTS[:base_url] : ENV['BC_API_ENDPOINT']
"#{base}/stores/#{store_hash}/v2"
"#{base}/stores/#{store_hash}/v#{version || 2}"
end
end
end
2 changes: 1 addition & 1 deletion lib/bigcommerce/exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module HttpErrors
}.freeze

def throw_http_exception!(code, env)
return unless ERRORS.keys.include? code
return unless ERRORS.key? code
response_headers = {}
unless env.body.empty?
response_headers = begin
Expand Down
14 changes: 14 additions & 0 deletions lib/bigcommerce/resources/v3/script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Scripts
# Scripts are used to create front-end scripts in Stencil theme
# Need to use connection version v3
# https://developer.bigcommerce.com/api/v3/#/reference/scripts/content-scripts/create-a-script

module Bigcommerce
module V3
class Script < Resource
include Bigcommerce::ResourceActions.new uri: 'content/scripts/%s'

property :data
end
end
end
25 changes: 25 additions & 0 deletions spec/bigcommerce/bigcommerce_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
api.instance_variable_get('@builder').instance_variable_get('@handlers')
end

describe 'version configuration' do
context 'default version' do
it 'should return the api_url with default version 2' do
Bigcommerce.configure do |config|
config.access_token = 'jksdgkjbhksjdb'
config.client_id = 'negskjgdjkbg'
config.store_hash = 'some_store'
end
expect(Bigcommerce.config.api_url).to include('/v2')
end
end

context 'custom version' do
it 'should return the api_url with custom version' do
Bigcommerce.configure do |config|
config.access_token = 'jksdgkjbhksjdb'
config.client_id = 'negskjgdjkbg'
config.store_hash = 'some_store'
config.version = '3'
end
expect(Bigcommerce.config.api_url).to include('/v3')
end
end
end

it 'should return a faraday object when configured' do
Bigcommerce.configure do |config|
config.url = 'http://foobar.com'
Expand Down
10 changes: 10 additions & 0 deletions spec/bigcommerce/unit/resources/v3/script_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec.describe Bigcommerce::V3::Script do
before(:each) { @script = Bigcommerce::V3::Script }

describe '.all' do
it 'should hit the correct path' do
expect(@script).to receive(:get).with(@script.path.build, {})
@script.all
end
end
end