From a51a90f85627025dad3fd5326e81a9c207f18a72 Mon Sep 17 00:00:00 2001 From: Derek Stotz Date: Tue, 19 May 2020 16:14:33 -0500 Subject: [PATCH] Rebrand Ruby client to Heartland Retail HRTL-50 --- .travis.yml | 1 + .yardopts | 2 +- Gemfile.lock | 6 +- LICENSE | 2 +- README.md | 66 +++++----- Rakefile | 6 +- ...retail.gemspec => heartland-retail.gemspec | 8 +- lib/heartland-retail.rb | 1 + lib/{springboard => heartland}/client.rb | 41 ++++-- lib/{springboard => heartland}/client/body.rb | 2 +- .../client/collection.rb | 4 +- .../client/errors.rb | 2 +- .../client/resource.rb | 8 +- .../client/response.rb | 3 +- lib/{springboard => heartland}/client/uri.rb | 2 +- lib/springboard-retail.rb | 1 - .../client/body_spec.rb | 5 +- .../client/resource_spec.rb | 12 +- .../client/response_spec.rb | 16 +-- .../client/uri_spec.rb | 2 +- .../{springboard => heartland}/client_spec.rb | 122 ++++++++++++++---- spec/shared_client_context.rb | 2 +- spec/spec_helper.rb | 4 +- 23 files changed, 204 insertions(+), 114 deletions(-) rename springboard-retail.gemspec => heartland-retail.gemspec (62%) create mode 100644 lib/heartland-retail.rb rename lib/{springboard => heartland}/client.rb (87%) rename lib/{springboard => heartland}/client/body.rb (93%) rename lib/{springboard => heartland}/client/collection.rb (96%) rename lib/{springboard => heartland}/client/errors.rb (92%) rename lib/{springboard => heartland}/client/resource.rb (97%) rename lib/{springboard => heartland}/client/response.rb (98%) rename lib/{springboard => heartland}/client/uri.rb (99%) delete mode 100644 lib/springboard-retail.rb rename spec/{springboard => heartland}/client/body_spec.rb (88%) rename spec/{springboard => heartland}/client/resource_spec.rb (96%) rename spec/{springboard => heartland}/client/response_spec.rb (85%) rename spec/{springboard => heartland}/client/uri_spec.rb (99%) rename spec/{springboard => heartland}/client_spec.rb (67%) diff --git a/.travis.yml b/.travis.yml index b409072..6192b6d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ rvm: - 2.0.0 - 2.1.0 - 2.2.0 + - 2.6.0 addons: code_climate: diff --git a/.yardopts b/.yardopts index 18b08c8..7ed963e 100644 --- a/.yardopts +++ b/.yardopts @@ -1 +1 @@ ---title "Springboard Client" +--title "Heartland Retail Client" diff --git a/Gemfile.lock b/Gemfile.lock index 9fcbd0a..ad1ebc1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - springboard-retail (5.0.0.beta1) + heartland-retail (5.0.0.beta1) faraday hashie json (>= 1.7.4) @@ -74,11 +74,11 @@ PLATFORMS DEPENDENCIES codeclimate-test-reporter (~> 1.0.0) coveralls + heartland-retail! pry rake rspec (~> 3.2) - springboard-retail! webmock BUNDLED WITH - 1.16.2 + 1.17.2 diff --git a/LICENSE b/LICENSE index 0abceb6..78b6717 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 Springboard Retail +Copyright (c) 2014 Heartland Retail Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 6e3be40..5333586 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# Springboard Retail Client +# Heartland Retail API Client [![Gem Version](https://badge.fury.io/rb/springboard-retail.png)](http://badge.fury.io/rb/springboard-retail) [![Build Status](https://travis-ci.org/springboardretail/springboard-client-ruby.png?branch=master)](https://travis-ci.org/springboardretail/springboard-client-ruby) [![Code Climate](https://codeclimate.com/github/springboardretail/springboard-client-ruby.png)](https://codeclimate.com/github/springboardretail/springboard-client-ruby) [![Coverage Status](https://coveralls.io/repos/github/springboardretail/springboard-client-ruby/badge.svg?branch=master)](https://coveralls.io/github/springboardretail/springboard-client-ruby?branch=master) -This is the [Springboard Retail](http://springboardretail.com/) (a point-of-sale/retail management system) client library for Ruby. It provides access to the Springboard Retail HTTP API. +This is the [Heartland Retail](http://heartlandretail.us/) (a point-of-sale/retail management system) client library for Ruby. It provides access to the Heartland Retail HTTP API. It is a wrapper around the [Faraday](https://github.com/lostisland/faraday) HTTP client library. @@ -17,17 +17,17 @@ You need a recent version of libcurl and a sane build environment. Debian/Ubuntu: -``` +```bash sudo apt-get install build-essential libcurl4-openssl-dev -gem install springboard-retail +gem install heartland-retail ``` ## Connecting ```ruby -require 'springboard-retail' -springboard = Springboard::Client.new( - 'https://example.myspringboard.us/api', +require 'heartland-retail' +heartland = HeartlandRetail::Client.new( + 'https://example.retail.heartland.us/api/', token: 'secret_token' ) ``` @@ -35,12 +35,12 @@ springboard = Springboard::Client.new( ## Resource oriented ```ruby -resource = springboard[:items][1234] +resource = heartland[:items][1234] response = resource.get response = resource.delete # Query string generation: -resource1 = springboard[:items] +resource1 = heartland[:items] resource2 = resource.query(:key1 => 'val1', 'key with spaces' => 'val with spaces') resource2.uri.to_s # => "/items?key%20with%20spaces=val%20with%20spaces&key1=val1" @@ -49,47 +49,50 @@ resource2.uri.to_s ## URI oriented ```ruby -response = springboard.get '/items/1234' -response = springboard.delete '/items/1234' -item_count = springboard.count '/items' +response = heartland.get '/items/1234' +response = heartland.delete '/items/1234' +item_count = heartland.count '/items' ``` ## Collection Resources ### Enumerable + Resources include Ruby's Enumerable module for easy iteration over collections: ```ruby -springboard[:items].each do |item| +heartland[:items].each do |item| puts item['description'] end -item_count = springboard[:items].count +item_count = heartland[:items].count -usernames = springboard[:users].map {|user| user['login']} +usernames = heartland[:users].map {|user| user['login']} ``` ### Filtering -Resources have a `filter` method that support's Springboard's advanced filter syntax: + +Resources have a `filter` method that support's Heartland Retail's advanced filter syntax: ```ruby -active_users = springboard[:users].filter(:active => true) +active_users = heartland[:users].filter(:active => true) active_users.each do |user| # do something with each active user end # filter returns a new resource which allows for chaining: -items = springboard[:items] +items = heartland[:items] active_items = items.filter(:active => true) active_items.filter(:price => {'$gt' => 10}).each do |item| # ... end # filtering custom fields: -springboard[:items].filter('custom@size'=> 'XL') +heartland[:items].filter('custom@size'=> 'XL') ``` ### Sorting + Resources have a `sort` method that accepts any number of sort options. Note that each call to sort overwrites any previous sorts. ```ruby @@ -103,6 +106,7 @@ end ``` ### Returning select fields + Resources have a `only` method that accepts any number of field keys to return only the selected fields. Note that each call to `only` overwrites any previous fields. ```ruby @@ -157,8 +161,8 @@ The `embed` method accepts one or more arguments as symbols or strings. It suppo Issuing deletes while iterating over a collection resource can cause the pagination to shift resulting in unexpected behavior. Use `while_results` when you want to: -* Consume messages from a queue, deleting each message after it has been processed. -* Delete all resources in a collection that doesn't support a top-level DELETE method. +- Consume messages from a queue, deleting each message after it has been processed. +- Delete all resources in a collection that doesn't support a top-level DELETE method. For example: @@ -177,20 +181,20 @@ passed through untouched: ```ruby # this: -springboard[:some_collection].post :a => 1, :b => 2 +heartland[:some_collection].post :a => 1, :b => 2 # is equivalent to this: -springboard[:some_collection].post '{"a":1,"b":2}' +heartland[:some_collection].post '{"a":1,"b":2}' ``` ## Response ```ruby -response = springboard[:items][1].get +response = heartland[:items][1].get response.status # Response status code as an Integer response.success? # true/false depending on whether 'status' indicates non-error -response.body # Returns a Springboard::Client::Body object (see below) +response.body # Returns a HeartlandRetail::Client::Body object (see below) response.raw_body # Returns the raw response body as a string response[:some_key] # Returns the corresponding key from 'body' response.headers # Response headers as a Hash @@ -237,17 +241,17 @@ response.raw_body All HTTP request methods have a bang variant that raises an exception on failure: ```ruby -response = springboard[:i_dont_exist].get +response = heartland[:i_dont_exist].get response.status # => 404 -springboard[:i_dont_exist].get! -# Raises Springboard::Client::RequestFailed exception +heartland[:i_dont_exist].get! +# Raises HeartlandRetail::Client::RequestFailed exception # To access the response from the exception: begin - springboard[:i_dont_exist].get! -rescue Springboard::Client::RequestFailed => error + heartland[:i_dont_exist].get! +rescue HeartlandRetail::Client::RequestFailed => error puts error.response.status end # => 404 @@ -264,5 +268,5 @@ client.debug = true client.debug = '/path/to/file.log' # Same values can be passed via :debug option to client constructor -client = Springboard::Client.new '', :debug => true +client = HeartlandRetail::Client.new '', :debug => true ``` diff --git a/Rakefile b/Rakefile index 3aaea39..f84b3a2 100644 --- a/Rakefile +++ b/Rakefile @@ -8,11 +8,11 @@ RSpec::Core::RakeTask.new(:spec) task :default => :spec -desc "Start a console with a Springboard::Client instance" +desc "Start a console with a HeartlandRetail::Client instance" task :console do - require 'springboard/client' + require 'heartland/client' require 'pry' - CLIENT = Springboard::Client.new(ENV['URI']) + CLIENT = HeartlandRetail::Client.new(ENV['URI']) CLIENT.auth :username => ENV['USER'], :password => ENV['PASSWORD'] Pry.start end diff --git a/springboard-retail.gemspec b/heartland-retail.gemspec similarity index 62% rename from springboard-retail.gemspec rename to heartland-retail.gemspec index d80338c..49af8af 100644 --- a/springboard-retail.gemspec +++ b/heartland-retail.gemspec @@ -1,9 +1,9 @@ Gem::Specification.new do |s| - s.name = 'springboard-retail' - s.version = '5.0.0.beta1' + s.name = 'heartland-retail' + s.version = '5.0.0' s.platform = Gem::Platform::RUBY - s.authors = ['Jay Stotz'] - s.summary = 'Springboard Retail API client library' + s.authors = ['Jay Stotz', 'Derek Stotz'] + s.summary = 'Heartland Retail API client library' s.required_rubygems_version = '>= 1.3.6' diff --git a/lib/heartland-retail.rb b/lib/heartland-retail.rb new file mode 100644 index 0000000..01bd228 --- /dev/null +++ b/lib/heartland-retail.rb @@ -0,0 +1 @@ +require_relative 'heartland/client' diff --git a/lib/springboard/client.rb b/lib/heartland/client.rb similarity index 87% rename from lib/springboard/client.rb rename to lib/heartland/client.rb index 74f472a..ead8197 100644 --- a/lib/springboard/client.rb +++ b/lib/heartland/client.rb @@ -3,13 +3,13 @@ require 'json' require 'logger' -require 'springboard/client/errors' +require_relative 'client/errors' ## -# Springboard namespace -module Springboard +# HeartlandRetail namespace +module HeartlandRetail ## - # The main point of interaction for the Springboard Client library. + # The main point of interaction for the Heartland Retail Client library. # # Client code must successfully authenticate with the API via the {#auth} # method before calling any HTTP methods or the API will return authorization @@ -42,7 +42,7 @@ class Client # @param [String] base_uri Base URI # @option opts [Boolean, String] :debug Pass true to debug to stdout. Pass a String to debug to given filename. # @option opts [Boolean] :insecure Disable SSL certificate verification - # @option opts [String] :token Springboard API Token + # @option opts [String] :token Heartland Retail API Token def initialize(base_uri, opts={}) @base_uri = URI.parse(base_uri) @opts = opts @@ -69,10 +69,10 @@ def debug=(debug) # # @return [true] # - # @option opts [String] :username Springboard username - # @option opts [String] :password Springboard password + # @option opts [String] :username Heartland Retail username + # @option opts [String] :password Heartland Retail password def auth(opts={}) - warn "[DEPRECATION] `auth` is deprecated. Please use `Springboard::Client.new '#{base_uri}', token: 'secret_token'` instead." + warn "[DEPRECATION] `auth` is deprecated. Please use `HeartlandRetail::Client.new '#{base_uri}', token: 'secret_token'` instead." unless opts[:username] && opts[:password] raise "Must specify :username and :password" @@ -87,7 +87,7 @@ def auth(opts={}) @session_cookie = response.headers['set-cookie'] return true else - raise AuthFailed, "Springboard auth failed" + raise AuthFailed, "Heartland Retail auth failed" end end @@ -278,7 +278,22 @@ def debug_logger(debug) end end -require 'springboard/client/resource' -require 'springboard/client/response' -require 'springboard/client/body' -require 'springboard/client/uri' +## +# Springboard namespace as alias of HeartlandRetail namespace for backwards compatability +module Springboard + include HeartlandRetail + + ## + # HeartlandRetail::Client with added deprecation warning for Springboard namespace + class Client < HeartlandRetail::Client + def initialize(base_uri, opts={}) + warn "[DEPRECATION] `Springboard::Client.new` is deprecated. Please use `HeartlandRetail::Client.new` instead." + super + end + end +end + +require_relative 'client/resource' +require_relative 'client/response' +require_relative 'client/body' +require_relative 'client/uri' diff --git a/lib/springboard/client/body.rb b/lib/heartland/client/body.rb similarity index 93% rename from lib/springboard/client/body.rb rename to lib/heartland/client/body.rb index 14ef884..510c446 100644 --- a/lib/springboard/client/body.rb +++ b/lib/heartland/client/body.rb @@ -1,6 +1,6 @@ require 'hashie' -module Springboard +module HeartlandRetail class Client ## # An indifferent Hash to represent parsed response bodies. diff --git a/lib/springboard/client/collection.rb b/lib/heartland/client/collection.rb similarity index 96% rename from lib/springboard/client/collection.rb rename to lib/heartland/client/collection.rb index 03a6d5f..f2bc55d 100644 --- a/lib/springboard/client/collection.rb +++ b/lib/heartland/client/collection.rb @@ -1,4 +1,4 @@ -module Springboard +module HeartlandRetail class Client ## # Mixin provides {Resource} with special methods for convenient interaction @@ -49,7 +49,7 @@ def empty? ## # Returns a new resource with the given filters added to the query string. # - # @see https://github.com/springboard/springboard-retail/blob/master/api/doc/filtering.md Springboard collection API filtering docs + # @see https://github.com/springboard/springboard-retail/blob/master/api/doc/filtering.md Heartland Retail collection API filtering docs # # @param [String, Hash] new_filters Hash or JSON string of new filters # diff --git a/lib/springboard/client/errors.rb b/lib/heartland/client/errors.rb similarity index 92% rename from lib/springboard/client/errors.rb rename to lib/heartland/client/errors.rb index 5d04cbf..73fdfcb 100644 --- a/lib/springboard/client/errors.rb +++ b/lib/heartland/client/errors.rb @@ -1,4 +1,4 @@ -module Springboard +module HeartlandRetail class Client ## # API request failure diff --git a/lib/springboard/client/resource.rb b/lib/heartland/client/resource.rb similarity index 97% rename from lib/springboard/client/resource.rb rename to lib/heartland/client/resource.rb index cc33fe4..ea941d5 100644 --- a/lib/springboard/client/resource.rb +++ b/lib/heartland/client/resource.rb @@ -1,6 +1,6 @@ -require 'springboard/client/collection' +require_relative 'collection' -module Springboard +module HeartlandRetail class Client ## # An representation of an API resource identified by a URI. Allows @@ -23,13 +23,13 @@ class Resource attr_reader :uri ## - # The underlying Springboard Client. + # The underlying HeartlandRetail Client. # # @return [Client] attr_reader :client ## - # @param [Springboard::Client] client + # @param [HeartlandRetail::Client] client # @param [Addressable::URI, #to_s] uri def initialize(client, uri_or_path) @client = client diff --git a/lib/springboard/client/response.rb b/lib/heartland/client/response.rb similarity index 98% rename from lib/springboard/client/response.rb rename to lib/heartland/client/response.rb index f773e17..dbd6670 100644 --- a/lib/springboard/client/response.rb +++ b/lib/heartland/client/response.rb @@ -1,4 +1,4 @@ -module Springboard +module HeartlandRetail class Client ## # An API response including body, headers, and status information. @@ -82,4 +82,3 @@ def parse_body end end end - diff --git a/lib/springboard/client/uri.rb b/lib/heartland/client/uri.rb similarity index 99% rename from lib/springboard/client/uri.rb rename to lib/heartland/client/uri.rb index d2d6a4f..f16ed7b 100644 --- a/lib/springboard/client/uri.rb +++ b/lib/heartland/client/uri.rb @@ -1,6 +1,6 @@ require 'uri' -module Springboard +module HeartlandRetail class Client ## # A wrapper around URI diff --git a/lib/springboard-retail.rb b/lib/springboard-retail.rb deleted file mode 100644 index 4602fb5..0000000 --- a/lib/springboard-retail.rb +++ /dev/null @@ -1 +0,0 @@ -require 'springboard/client' diff --git a/spec/springboard/client/body_spec.rb b/spec/heartland/client/body_spec.rb similarity index 88% rename from spec/springboard/client/body_spec.rb rename to spec/heartland/client/body_spec.rb index 8fd06cc..3fecd3a 100644 --- a/spec/springboard/client/body_spec.rb +++ b/spec/heartland/client/body_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' -describe Springboard::Client::Body do +describe HeartlandRetail::Client::Body do let(:hash) { {"key1" => "val1", "key2" => {"subkey1" => "subval1"}} } - let(:body) { Springboard::Client::Body.new(hash)} + let(:body) { HeartlandRetail::Client::Body.new(hash)} describe "[]" do it "should support string keys" do @@ -29,4 +29,3 @@ end end end - diff --git a/spec/springboard/client/resource_spec.rb b/spec/heartland/client/resource_spec.rb similarity index 96% rename from spec/springboard/client/resource_spec.rb rename to spec/heartland/client/resource_spec.rb index 6bd506e..3bb8e20 100644 --- a/spec/springboard/client/resource_spec.rb +++ b/spec/heartland/client/resource_spec.rb @@ -1,14 +1,14 @@ require 'spec_helper' -describe Springboard::Client::Resource do +describe HeartlandRetail::Client::Resource do include_context "client" let(:resource_path) { '/some/path' } - let(:resource) { Springboard::Client::Resource.new(client, resource_path) } + let(:resource) { HeartlandRetail::Client::Resource.new(client, resource_path) } describe "[]" do it "should return a new resource" do - expect(resource["subpath"]).to be_a Springboard::Client::Resource + expect(resource["subpath"]).to be_a HeartlandRetail::Client::Resource expect(resource["subpath"].object_id).not_to eq(resource.object_id) end @@ -271,12 +271,12 @@ resource.while_results do |result| # nothing end - end.to raise_error(Springboard::Client::RequestFailed) + end.to raise_error(HeartlandRetail::Client::RequestFailed) end end describe "exists?" do - let(:response) { double(Springboard::Client::Response) } + let(:response) { double(HeartlandRetail::Client::Response) } it "should return true if the response indicates success" do allow(response).to receive(:success?).and_return(true) @@ -296,7 +296,7 @@ allow(response).to receive(:success?).and_return(false) expect(client).to receive(:head).with(resource.uri, false).and_return(response) expect { resource.exists? }.to raise_error { |e| - expect(e).to be_a Springboard::Client::RequestFailed + expect(e).to be_a HeartlandRetail::Client::RequestFailed expect(e.response).to be === response expect(e.message).to eq("Request during call to 'exists?' resulted in non-404 error.") } diff --git a/spec/springboard/client/response_spec.rb b/spec/heartland/client/response_spec.rb similarity index 85% rename from spec/springboard/client/response_spec.rb rename to spec/heartland/client/response_spec.rb index dbe00d1..47a0c57 100644 --- a/spec/springboard/client/response_spec.rb +++ b/spec/heartland/client/response_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Springboard::Client::Response do +describe HeartlandRetail::Client::Response do include_context "client" let(:raw_body) { '{"key":"value"}' } @@ -13,12 +13,12 @@ Faraday::Response.new(env) end - let(:response) { Springboard::Client::Response.new(faraday_response, client) } + let(:response) { HeartlandRetail::Client::Response.new(faraday_response, client) } describe "body" do describe "if raw body is valid JSON" do - it "should return a Springboard::Client::Body" do - expect(response.body).to be_a Springboard::Client::Body + it "should return a HeartlandRetail::Client::Body" do + expect(response.body).to be_a HeartlandRetail::Client::Body end it "should wrap the parsed response body" do @@ -30,7 +30,7 @@ let(:raw_body) { 'I am not JSON!' } it "should raise an informative error" do expect { response.body }.to raise_error \ - Springboard::Client::BodyError, + HeartlandRetail::Client::BodyError, "Can't parse response body. (Hint: Try the raw_body method.)" end end @@ -39,7 +39,7 @@ let(:raw_body) { '' } it "should raise an informative error" do expect { response.body }.to raise_error \ - Springboard::Client::BodyError, + HeartlandRetail::Client::BodyError, "Response body is empty. (Hint: If you just created a new resource, try: response.resource.get)" end end @@ -61,8 +61,8 @@ describe "when Location header is returned" do let(:raw_headers) { { Location: '/new/path' } } - it "should be a Springboard::Client::Resource" do - expect(response.resource).to be_a Springboard::Client::Resource + it "should be a HeartlandRetail::Client::Resource" do + expect(response.resource).to be_a HeartlandRetail::Client::Resource end it "should have the Location header value as its URL" do diff --git a/spec/springboard/client/uri_spec.rb b/spec/heartland/client/uri_spec.rb similarity index 99% rename from spec/springboard/client/uri_spec.rb rename to spec/heartland/client/uri_spec.rb index 8627775..732b0c0 100644 --- a/spec/springboard/client/uri_spec.rb +++ b/spec/heartland/client/uri_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Springboard::Client::URI do +describe HeartlandRetail::Client::URI do let(:uri) { described_class.parse('/relative/path') } describe "subpath" do diff --git a/spec/springboard/client_spec.rb b/spec/heartland/client_spec.rb similarity index 67% rename from spec/springboard/client_spec.rb rename to spec/heartland/client_spec.rb index 553028f..003800c 100644 --- a/spec/springboard/client_spec.rb +++ b/spec/heartland/client_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Springboard::Client do +describe HeartlandRetail::Client do include_context "client" describe "connection" do @@ -34,7 +34,7 @@ it "should raise an AuthFailed if auth fails" do stub_request(:post, "#{base_url}/auth/identity/callback").to_return(:status => 401) expect { client.auth(:username => 'someone', :password => 'wrong') }.to \ - raise_error(Springboard::Client::AuthFailed, "Springboard auth failed") + raise_error(HeartlandRetail::Client::AuthFailed, "Heartland Retail auth failed") end it "should store the session cookie" do @@ -46,8 +46,8 @@ describe "initialize" do it "should call configure_connection!" do - expect_any_instance_of(Springboard::Client).to receive(:configure_connection!) - Springboard::Client.new(base_url, :x => 'y') + expect_any_instance_of(HeartlandRetail::Client).to receive(:configure_connection!) + HeartlandRetail::Client.new(base_url, :x => 'y') end end @@ -65,12 +65,12 @@ it "set the default timeout" do client.__send__(:configure_connection!) - expect(connection.options.timeout).to eq(Springboard::Client::DEFAULT_TIMEOUT) + expect(connection.options.timeout).to eq(HeartlandRetail::Client::DEFAULT_TIMEOUT) end it "set the default connect timeout" do client.__send__(:configure_connection!) - expect(connection.options.open_timeout).to eq(Springboard::Client::DEFAULT_CONNECT_TIMEOUT) + expect(connection.options.open_timeout).to eq(HeartlandRetail::Client::DEFAULT_CONNECT_TIMEOUT) end context 'headers' do @@ -84,25 +84,25 @@ it 'sets Content-Type header' do expect(headers).to receive(:[]=).once.with('Content-Type', 'application/json') expect(headers).to receive(:[]=).once.with('Authorization', 'Bearer token') - Springboard::Client.new(base_url, token: 'token') + HeartlandRetail::Client.new(base_url, token: 'token') end end end describe "[]" do it "should return a resource object with the given path string and client" do - expect(client["path"]).to be_a Springboard::Client::Resource + expect(client["path"]).to be_a HeartlandRetail::Client::Resource expect(client[:path].uri.to_s).to eq("#{base_url}/path") end it "should return a resource object when given a path as a symbol" do - expect(client[:path]).to be_a Springboard::Client::Resource + expect(client[:path]).to be_a HeartlandRetail::Client::Resource expect(client[:path].uri.to_s).to eq("#{base_url}/path") end it "should return a resource object when given a path as a URI" do uri = 'path'.to_uri - expect(client[uri]).to be_a Springboard::Client::Resource + expect(client[uri]).to be_a HeartlandRetail::Client::Resource expect(client[uri].uri.to_s).to eq("#{base_url}/path") end @@ -142,38 +142,37 @@ client.__send__(method, '/relative/path') end - it "should return a Springboard::Client::Response" do + it "should return a HeartlandRetail::Client::Response" do stub_request(method, "#{base_url}/relative/path") response = client.__send__(method, '/relative/path') - expect(response).to be_a Springboard::Client::Response + expect(response).to be_a HeartlandRetail::Client::Response end it "should remove redundant base path prefix from URL if present" do stub_request(method, "#{base_url}/relative/path") response = client.__send__(method, '/api/relative/path') - expect(response).to be_a Springboard::Client::Response + expect(response).to be_a HeartlandRetail::Client::Response end end describe bang_method do it "should call #{method}" do - response = double(Springboard::Client::Response) + response = double(HeartlandRetail::Client::Response) expect(response).to receive(:success?).and_return(true) expect(client).to receive(method).with('/path', false).and_return(response) expect(client.__send__(bang_method, '/path')).to be === response end it "should raise an exception on failure" do - response = double(Springboard::Client::Response) + response = double(HeartlandRetail::Client::Response) expect(response).to receive(:success?).and_return(false) expect(response).to receive(:status).and_return(404) expect(client).to receive(method).with('/path', false).and_return(response) - expect { client.send(bang_method, '/path') }.to raise_error(Springboard::Client::RequestFailed) + expect { client.send(bang_method, '/path') }.to raise_error(HeartlandRetail::Client::RequestFailed) end end end - [:put, :post].each do |method| bang_method = "#{method}!" describe method do @@ -186,10 +185,10 @@ client.__send__(method, '/relative/path', 'body') end - it "should return a Springboard::Client::Response" do + it "should return a HeartlandRetail::Client::Response" do stub_request(method, "#{base_url}/relative/path") response = client.__send__(method, '/relative/path', 'body') - expect(response).to be_a Springboard::Client::Response + expect(response).to be_a HeartlandRetail::Client::Response end it "should serialize the request body as JSON if it is a hash" do @@ -219,19 +218,19 @@ describe bang_method do it "should call #{method}" do - response = double(Springboard::Client::Response) + response = double(HeartlandRetail::Client::Response) expect(response).to receive(:success?).and_return(true) expect(client).to receive(method).with('/path', 'body', false).and_return(response) expect(client.__send__(bang_method, '/path', 'body')).to be === response end it "should raise an exception on failure" do - response = double(Springboard::Client::Response) + response = double(HeartlandRetail::Client::Response) expect(response).to receive(:success?).and_return(false) expect(response).to receive(:status).and_return(404) expect(client).to receive(method).with('/path', 'body', false).and_return(response) expect { client.send(bang_method, '/path', 'body') }.to raise_error { |error| - expect(error).to be_a(Springboard::Client::RequestFailed) + expect(error).to be_a(HeartlandRetail::Client::RequestFailed) expect(error.response).to be === response } end @@ -241,7 +240,7 @@ describe "each_page" do it "should request each page of the collection and yield each response to the block" do responses = (1..3).map do |p| - response = double(Springboard::Client::Response) + response = double(HeartlandRetail::Client::Response) allow(response).to receive(:[]).with('pages').and_return(3) expect(client).to receive(:get!).with("/things?page=#{p}&per_page=20".to_uri).and_return(response) @@ -258,7 +257,7 @@ describe "each" do it "should request each page of the collection and yield each individual result to the block" do all_results = (1..3).inject([]) do |results, p| - response = double(Springboard::Client::Response) + response = double(HeartlandRetail::Client::Response) allow(response).to receive(:[]).with('pages').and_return(3) page_results = 20.times.map {|i| "Page #{p} result #{i+1}"} @@ -279,7 +278,7 @@ describe "count" do it "should request the first page/record of the collection and return the total" do - response = double(Springboard::Client::Response) + response = double(HeartlandRetail::Client::Response) allow(response).to receive(:[]).with('total').and_return(17) expect(client).to receive(:get!).with("/things?page=1&per_page=1".to_uri) .and_return(response) @@ -287,3 +286,76 @@ end end end + +describe Springboard::Client do + include_context "client" + let(:client) { Springboard::Client.new(base_url) } + + describe "initialize" do + it "should call configure_connection! and return a HeartlandRetail::Client" do + expect_any_instance_of(HeartlandRetail::Client).to receive(:configure_connection!) + client = Springboard::Client.new(base_url, :x => 'y') + expect(client).to be_a HeartlandRetail::Client + end + + it "should include a deprecation warning" do + deprecation_message = "[DEPRECATION] `Springboard::Client.new` is deprecated. Please use `HeartlandRetail::Client.new` instead.\n" + expect { Springboard::Client.new(base_url) }.to output(deprecation_message).to_stderr + end + end + + describe "errors" do + describe "legacy error handling" do + describe "AuthFailed" do + it "should raise a Springboard class error if auth fails" do + stub_request(:post, "#{base_url}/auth/identity/callback").to_return(:status => 401) + expect { client.auth(:username => 'someone', :password => 'wrong') }.to \ + raise_error(Springboard::Client::AuthFailed, "Heartland Retail auth failed") + end + + it "should rescue a Springboard class error" do + stub_request(:post, "#{base_url}/auth/identity/callback").to_return(:status => 401) + rescued = nil + begin + client.auth(:username => 'someone', :password => 'wrong') + rescue Springboard::Client::AuthFailed => err + rescued = true + end + expect(rescued).to eq(true) + end + end + + describe "RequestFailed" do + it "should raise an Springboard class error if request fails" do + response = double(Springboard::Client::Response) + expect(response).to receive(:success?).and_return(false) + expect(response).to receive(:status).and_return(404) + expect(client).to receive(:get).with('/path', false).and_return(response) + expect { client.get!('/path') }.to raise_error(Springboard::Client::RequestFailed) + end + + it "should rescue a Springboard class error" do + response = double(Springboard::Client::Response) + expect(response).to receive(:success?).and_return(false) + expect(response).to receive(:status).and_return(404) + expect(client).to receive(:get).with('/path', false).and_return(response) + rescued = nil + begin + client.get!('/path') + rescue Springboard::Client::RequestFailed => err + rescued = true + end + expect(rescued).to eq(true) + end + end + end + end + + [:get, :head, :delete, :put, :post, :debug=, :[], :auth, :connection].each do |method| + describe method do + it "should respond to the HeartlandRetail::Client method #{method}" do + expect(client).to respond_to(method) + end + end + end +end diff --git a/spec/shared_client_context.rb b/spec/shared_client_context.rb index 07da52e..7fc86c6 100644 --- a/spec/shared_client_context.rb +++ b/spec/shared_client_context.rb @@ -1,5 +1,5 @@ shared_context "client" do let(:base_url) { "http://bozo.com/api" } - let(:client) { Springboard::Client.new(base_url) } + let(:client) { HeartlandRetail::Client.new(base_url) } let(:connection) { client.connection } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2681b4b..f06501d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,7 @@ require 'coveralls' Coveralls.wear! -require 'springboard-retail' +require 'heartland-retail' require 'webmock/rspec' require 'shared_client_context' @@ -12,6 +12,6 @@ class String def to_uri - Springboard::Client::URI.parse(self) + HeartlandRetail::Client::URI.parse(self) end end