Skip to content

Commit

Permalink
Support Scripts (API v3)
Browse files Browse the repository at this point in the history
  • Loading branch information
tfx committed Aug 10, 2018
1 parent f2f885a commit 9455c83
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 4 deletions.
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::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
4 changes: 2 additions & 2 deletions lib/bigcommerce/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Config < Hashie::Mash

def api_url
return url if auth == 'legacy'

version = self.version || '2'
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}"
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

0 comments on commit 9455c83

Please sign in to comment.