From 5e184a633975d285673baf9a08c812537122803f Mon Sep 17 00:00:00 2001 From: Khoi Nguyen Date: Wed, 8 Aug 2018 16:11:43 +1000 Subject: [PATCH] Support Scripts (API v3) --- CHANGELOG.md | 1 + README.md | 16 ++++++++++++ bigcommerce.gemspec | 2 +- lib/bigcommerce/config.rb | 2 +- lib/bigcommerce/exception.rb | 2 +- lib/bigcommerce/resources/v3/script.rb | 14 +++++++++++ spec/bigcommerce/bigcommerce_spec.rb | 25 +++++++++++++++++++ .../unit/resources/v3/script_spec.rb | 10 ++++++++ 8 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 lib/bigcommerce/resources/v3/script.rb create mode 100644 spec/bigcommerce/unit/resources/v3/script_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 1da5962..0f347c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/README.md b/README.md index 50b837f..2ce96f2 100644 --- a/README.md +++ b/README.md @@ -172,5 +172,21 @@ Bigcommerce::System.raw_request(:get, 'time', connection: connection_legacy) => #> ``` +### 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) diff --git a/bigcommerce.gemspec b/bigcommerce.gemspec index 69aeafb..ef3b5a8 100644 --- a/bigcommerce.gemspec +++ b/bigcommerce.gemspec @@ -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' diff --git a/lib/bigcommerce/config.rb b/lib/bigcommerce/config.rb index c0911cd..b56e13b 100644 --- a/lib/bigcommerce/config.rb +++ b/lib/bigcommerce/config.rb @@ -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 diff --git a/lib/bigcommerce/exception.rb b/lib/bigcommerce/exception.rb index 2757ceb..112a589 100644 --- a/lib/bigcommerce/exception.rb +++ b/lib/bigcommerce/exception.rb @@ -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 diff --git a/lib/bigcommerce/resources/v3/script.rb b/lib/bigcommerce/resources/v3/script.rb new file mode 100644 index 0000000..4b15af1 --- /dev/null +++ b/lib/bigcommerce/resources/v3/script.rb @@ -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 diff --git a/spec/bigcommerce/bigcommerce_spec.rb b/spec/bigcommerce/bigcommerce_spec.rb index 77df5f4..034dadf 100644 --- a/spec/bigcommerce/bigcommerce_spec.rb +++ b/spec/bigcommerce/bigcommerce_spec.rb @@ -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' diff --git a/spec/bigcommerce/unit/resources/v3/script_spec.rb b/spec/bigcommerce/unit/resources/v3/script_spec.rb new file mode 100644 index 0000000..d6d985f --- /dev/null +++ b/spec/bigcommerce/unit/resources/v3/script_spec.rb @@ -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