Skip to content

Commit

Permalink
Merge a773c5f into 56d58e5
Browse files Browse the repository at this point in the history
  • Loading branch information
igorbozato committed Oct 1, 2018
2 parents 56d58e5 + a773c5f commit 0b5acc7
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 68 deletions.
17 changes: 15 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ PATH
remote: .
specs:
springboard-retail (4.3.0)
faraday (= 0.11)
faraday-cookie_jar (= 0.0.6)
hashie
json (>= 1.7.4)
patron (= 0.4.18)

GEM
remote: https://rubygems.org/
Expand All @@ -23,13 +24,22 @@ GEM
safe_yaml (~> 1.0.0)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.20180417)
unf (>= 0.0.5, < 1.0.0)
faraday (0.11.0)
multipart-post (>= 1.2, < 3)
faraday-cookie_jar (0.0.6)
faraday (>= 0.7.4)
http-cookie (~> 1.0.0)
hashie (3.6.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
json (2.1.0)
method_source (0.8.2)
mime-types (2.4.3)
multi_json (1.11.0)
multipart-post (2.0.0)
netrc (0.10.3)
patron (0.4.18)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
Expand Down Expand Up @@ -62,6 +72,9 @@ GEM
tins (~> 1.0)
thor (0.19.1)
tins (1.3.5)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.5)
webmock (1.17.4)
addressable (>= 2.2.7)
crack (>= 0.3.2)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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.

It is a wrapper around the [Patron](http://toland.github.com/patron/) HTTP client library. Supports MRI 1.9+.
It is a wrapper around the [Faraday](https://github.com/lostisland/faraday) HTTP client library.

You can find [documentation here](http://rdoc.info/github/springboardretail/springboard-client-ruby).

Expand Down
59 changes: 37 additions & 22 deletions lib/springboard/client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rubygems'
require 'patron'
require 'faraday'
require 'faraday-cookie_jar'
require 'json'
require 'logger'

require 'springboard/client/errors'

Expand Down Expand Up @@ -40,17 +42,17 @@ class Client
# @option opts [String] :token Springboard API Token
def initialize(base_uri, opts={})
@base_uri = URI.parse(base_uri)
configure_session(base_uri, opts)
configure_connection(base_uri, opts)
end

##
# Returns the underlying Patron session
# Returns the underlying Faraday connection
#
# @see http://patron.rubyforge.org/Patron/Session.html Patron::Session docs
# @see https://www.rubydoc.info/gems/faraday/Faraday/Connection Faraday::Connection docs
#
# @return [Patron::Session]
def session
@session ||= Patron::Session.new
# @return [Faraday::Connection]
def connection
@connection ||= Faraday.new
end

##
Expand All @@ -61,7 +63,7 @@ def session
#
# @return [String, Boolean] The debug argument
def debug=(debug)
session.enable_debug(debug == true ? nil : debug)
connection.response :logger, debug_logger(debug), bodies: true
end

##
Expand Down Expand Up @@ -222,12 +224,12 @@ def make_request(method, uri, headers=false, body=false)
args = [prepare_uri(uri).to_s]
args.push prepare_request_body(body) unless body === false
args.push headers unless headers === false
new_response session.__send__(method, *args)
new_response connection.__send__(method, *args)
end

def raise_on_fail(response)
if !response.success?
error = RequestFailed.new "Request failed with status: #{response.status_line}"
error = RequestFailed.new "Request failed with status: #{response.status}"
error.response = response
raise error
end
Expand All @@ -236,22 +238,35 @@ def raise_on_fail(response)

def prepare_uri(uri)
uri = URI.parse(uri)
uri.to_s.gsub(/^#{base_uri.to_s}|^#{base_uri.path}/, '')
uri.to_s
.gsub(/^#{base_uri.to_s}|^#{base_uri.path}/, '')
.gsub(/^\//, '')
end

def new_response(patron_response)
Response.new patron_response, self
def new_response(faraday_response)
Response.new faraday_response, self
end

def configure_session(base_url, opts)
session.base_url = base_url
session.headers['Content-Type'] = 'application/json'
session.headers['Authorization'] = "Bearer #{opts[:token]}" if opts[:token]
session.handle_cookies
session.insecure = opts[:insecure] if opts.has_key?(:insecure)
session.timeout = DEFAULT_TIMEOUT
session.connect_timeout = DEFAULT_CONNECT_TIMEOUT
self.debug = opts[:debug] if opts.has_key?(:debug)
def configure_connection(base_url, opts)
connection.url_prefix= base_url

connection.use :cookie_jar

connection.headers['Content-Type'] = 'application/json'
connection.headers['Authorization'] = "Bearer #{opts[:token]}" if opts[:token]

connection.ssl[:verify] = false if opts.has_key?(:insecure)

connection.options.timeout = DEFAULT_TIMEOUT
connection.options.open_timeout = DEFAULT_CONNECT_TIMEOUT

if opts.has_key?(:debug)
connection.response :logger, debug_logger(opts[:debug]), bodies: true
end
end

def debug_logger(debug)
Logger.new(debug == true ? STDOUT : debug)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/springboard/client/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def success?
end

##
# Delegates missing methods to the underlying Patron::Response.
# Delegates missing methods to the underlying Faraday::Response.
#
# @see http://patron.rubyforge.org/Patron/Response.html Patron::Response docs
# @see https://www.rubydoc.info/gems/faraday/Faraday/Response Faraday::Response docs
def method_missing(method, *args, &block)
@response.respond_to?(method) ? @response.__send__(method, *args, &block) : super
end
Expand Down
2 changes: 1 addition & 1 deletion spec/shared_client_context.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
shared_context "client" do
let(:base_url) { "http://bozo.com/api" }
let(:client) { Springboard::Client.new(base_url) }
let(:session) { client.session }
let(:connection) { client.connection }
end
15 changes: 10 additions & 5 deletions spec/springboard/client/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
include_context "client"

let(:raw_body) { '{"key":"value"}' }
let(:raw_headers) { 'X-Custom-Header: Hi' }
let(:raw_headers) { { 'X-Custom-Header' => 'Hi' } }
let(:status_code) { 200 }
let(:path) { '/path' }
let(:patron_response) { Patron::Response.new(path, status_code, 0, raw_headers, raw_body) }
let(:response) { Springboard::Client::Response.new(patron_response, client) }
let(:faraday_response) do
env = Faraday::Env.new(:get, raw_body, path, nil, nil, nil, nil,
nil, raw_body, raw_headers, status_code)

Faraday::Response.new(env)
end
let(:response) { Springboard::Client::Response.new(faraday_response, client) }

describe "body" do
describe "if raw body is valid JSON" do
Expand Down Expand Up @@ -54,7 +59,7 @@

describe "resource" do
describe "when Location header is returned" do
let(:raw_headers) { 'Location: /new/path' }
let(:raw_headers) { { Location: '/new/path' } }

it "should be a Springboard::Client::Resource" do
expect(response.resource).to be_a Springboard::Client::Resource
Expand All @@ -66,7 +71,7 @@
end

describe "when Location header is not returned" do
let(:raw_headers) { '' }
let(:raw_headers) { Hash.new }

it "should be nil" do
expect(response.resource).to be_nil
Expand Down
74 changes: 40 additions & 34 deletions spec/springboard/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
describe Springboard::Client do
include_context "client"

describe "session" do
it "should be a Patron::Session" do
expect(client.session).to be_a Patron::Session
describe "connection" do
it "should be a Faraday::Connection" do
expect(client.connection).to be_a Faraday::Connection
end
end

describe "auth" do
it "should attempt to authenticate with the given username and password" do
request_stub = stub_request(:post, "#{base_url}/auth/identity/callback").with \
:body => "auth_key=coco&password=boggle",
:headers => {'Content-Type' => 'application/x-www-form-urlencoded'}
request_stub = stub_request(:post, "#{base_url}/auth/identity/callback")
client.auth(:username => 'coco', :password => 'boggle')
expect(request_stub).to have_been_requested
end
Expand All @@ -37,48 +35,48 @@
end

describe "initialize" do
it "should call configure_session" do
expect_any_instance_of(Springboard::Client).to receive(:configure_session).with(base_url, {:x => 'y'})
it "should call configure_connection" do
expect_any_instance_of(Springboard::Client).to receive(:configure_connection).with(base_url, {:x => 'y'})
Springboard::Client.new(base_url, :x => 'y')
end
end

describe "configure_session" do
it "should set the session's base_url" do
expect(session).to receive(:base_url=).with(base_url)
client.__send__(:configure_session, base_url, :x => 'y')
describe "configure_connection" do
it "should set the connection's url_prefix" do
client.__send__(:configure_connection, base_url, :x => 'y')
expect(connection.url_prefix.to_s).to eq(base_url)
end

it "should enable cookies" do
expect(session).to receive(:handle_cookies)
client.__send__(:configure_session, base_url, :x => 'y')
expect(connection).to receive(:use).with(:cookie_jar)
client.__send__(:configure_connection, base_url, :x => 'y')
end

it "should allow setting insecure on the session" do
expect(session).to receive(:insecure=).with(true)
client.__send__(:configure_session, base_url, :insecure => true)
it "should allow setting insecure on the connection" do
client.__send__(:configure_connection, base_url, :insecure => true)
expect(connection.ssl[:verify]).to be false
end

it "set the default timeout" do
client.__send__(:configure_session, base_url, {})
expect(client.session.timeout).to eq(Springboard::Client::DEFAULT_TIMEOUT)
client.__send__(:configure_connection, base_url, {})
expect(connection.options.timeout).to eq(Springboard::Client::DEFAULT_TIMEOUT)
end

it "set the default connect timeout" do
client.__send__(:configure_session, base_url, {})
expect(client.session.connect_timeout).to eq(Springboard::Client::DEFAULT_CONNECT_TIMEOUT)
client.__send__(:configure_connection, base_url, {})
expect(connection.options.open_timeout).to eq(Springboard::Client::DEFAULT_CONNECT_TIMEOUT)
end

context 'headers' do
let(:headers) { double('headers') }
before do
allow(session).to receive(:headers).and_return(headers)
allow(connection).to receive(:headers).and_return(headers)
end

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')
client.__send__(:configure_session, base_url, :token => 'token')
client.__send__(:configure_connection, base_url, :token => 'token')
end
end
end
Expand Down Expand Up @@ -111,15 +109,23 @@

describe "debug=" do
context "with a file path" do
it "should pass the path to enable_debug on the Patron session" do
expect(client.session).to receive(:enable_debug).with('/path/to/log')
it "should pass the path to Faraday logger" do
logger = double

allow(Logger).to receive(:new).with('/path/to/log').and_return(logger)

expect(client.connection).to receive(:response).with(:logger, logger, bodies: true)
client.debug = '/path/to/log'
end
end

context "with true" do
it "should pass nil to enable_debug on the Patron session" do
expect(client.session).to receive(:enable_debug).with(nil)
it "should pass a Logger on STDOUT to Faraday logger" do
logger = double

allow(Logger).to receive(:new).with(STDOUT).and_return(logger)

expect(client.connection).to receive(:response).with(:logger, logger, bodies: true)
client.debug = true
end
end
Expand All @@ -128,8 +134,8 @@
[:get, :head, :delete].each do |method|
bang_method = "#{method}!"
describe method do
it "should call session's #{method}" do
expect(session).to receive(method).with('/relative/path')
it "should call connection's #{method}" do
expect(connection).to receive(method).with('relative/path')
client.__send__(method, '/relative/path')
end

Expand Down Expand Up @@ -157,7 +163,7 @@
it "should raise an exception on failure" do
response = double(Springboard::Client::Response)
expect(response).to receive(:success?).and_return(false)
expect(response).to receive(:status_line).and_return('404 Not Found')
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)
end
Expand All @@ -168,8 +174,8 @@
[:put, :post].each do |method|
bang_method = "#{method}!"
describe method do
it "should call session's #{method}" do
expect(session).to receive(method).with('/relative/path', 'body')
it "should call connection's #{method}" do
expect(connection).to receive(method).with('relative/path', 'body')
client.__send__(method, '/relative/path', 'body')
end

Expand All @@ -181,7 +187,7 @@

it "should serialize the request body as JSON if it is a hash" do
body_hash = {:key1 => 'val1', :key2 => 'val2'}
expect(session).to receive(method).with('/path', body_hash.to_json)
expect(connection).to receive(method).with('path', body_hash.to_json)
client.__send__(method, '/path', body_hash)
end

Expand Down Expand Up @@ -211,7 +217,7 @@
it "should raise an exception on failure" do
response = double(Springboard::Client::Response)
expect(response).to receive(:success?).and_return(false)
expect(response).to receive(:status_line).and_return('404 Not Found')
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)
Expand Down
3 changes: 2 additions & 1 deletion springboard-retail.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Gem::Specification.new do |s|

s.required_rubygems_version = '>= 1.3.6'

s.add_runtime_dependency 'patron', '0.4.18'
s.add_runtime_dependency 'faraday', '0.11'
s.add_runtime_dependency 'faraday-cookie_jar', '0.0.6'
s.add_runtime_dependency 'json', '>= 1.7.4'
s.add_runtime_dependency 'hashie'

Expand Down
Binary file added vendor/cache/domain_name-0.5.20180417.gem
Binary file not shown.
Binary file added vendor/cache/faraday-0.11.0.gem
Binary file not shown.
Binary file added vendor/cache/faraday-cookie_jar-0.0.6.gem
Binary file not shown.
Binary file added vendor/cache/http-cookie-1.0.3.gem
Binary file not shown.
Binary file added vendor/cache/multipart-post-2.0.0.gem
Binary file not shown.
Binary file removed vendor/cache/patron-0.4.18.gem
Binary file not shown.
Binary file added vendor/cache/unf-0.1.4.gem
Binary file not shown.
Binary file added vendor/cache/unf_ext-0.0.7.5.gem
Binary file not shown.

0 comments on commit 0b5acc7

Please sign in to comment.