Skip to content
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
16 changes: 1 addition & 15 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
source "https://rubygems.org"

gem 'rake', '~> 11.1', '>= 11.1.2'

group :test do
gem 'rspec'
gem 'simplecov', :require => false
gem "codeclimate-test-reporter", :require => false
gem 'coveralls', :require => false
gem 'scrutinizer-ocular', :require => false
end

group :docs do
gem 'yard', :git => 'https://github.com/trevorrowe/yard.git', branch: 'frameless'
gem 'yard-sitemap', '~> 1.0'
gem 'rdiscount'
end
gemspec
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ response = wepay.call('/account/create', access_token, {
})
```

## Configuration

WePay can be configured in a block:

ruby
```
WePay.configure do |config|
# Switch Faraday web adapter (default Net::HTTP)
config.http_adapter = :patron
end
```

You can also access the configuration object:

ruby
```
config = WePay.configuration

config.http_adapter = :patron
```

## Testing

Firstly, run `bundle install` to download and install the dependencies.
Expand All @@ -74,6 +95,13 @@ You can run the tests as follows:
make test
```

## Console

Start a REPL with the WePay library and dependencies loaded:

```
bin/console
```

## API Reference

Expand Down
4 changes: 4 additions & 0 deletions bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require 'wepay'
require 'irb'
IRB.start
54 changes: 34 additions & 20 deletions lib/we_pay/base_request.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'json'
require 'net/http'
require 'net/https'
require 'uri'
require 'faraday'

module WePay
module BaseRequest
Expand Down Expand Up @@ -29,36 +28,31 @@ def response
private

def raw_response
@raw_response ||= client.start do |c|
c.request(request)
end
end
@raw_response ||= client.post do |r|
r.url(uri.path)

def request
@request ||= Net::HTTP::Post.new(uri.path, default_headers).tap do |r|
if params.any?
r.body = params.to_json
end

if access_token
r.add_field('Authorization', "Bearer #{access_token}")
r.headers['Authorization'] = "Bearer #{access_token}"
end

if api_version
r.add_field('Api-Version', @api_version)
r.headers['Api-Version'] = api_version
end
end
end

def client
@client ||= Net::HTTP.new(uri.host, uri.port).tap do |c|
c.read_timeout = 30
c.use_ssl = true
end
end

def uri
@uri ||= URI.join(api_endpoint, normalized_path)
@client ||= Faraday.new(
uri,
headers: default_headers,
request: default_request_opts,
ssl: default_ssl_opts,
adapter: http_adapter
)
end

def normalized_path
Expand All @@ -67,11 +61,31 @@ def normalized_path
path.prepend('/')
end

def uri
URI.parse(api_endpoint + normalized_path)
end

def default_headers
{
'Content-Type' => 'application/json',
'User-Agent' => 'WePay Ruby SDK'
content_type: 'application/json',
user_agent: 'WePay Ruby SDK'
}
end

def default_request_opts
{
timeout: 30
}
end

def default_ssl_opts
{
verify: true
}
end

def http_adapter
WePay.configuration.http_adapter
end
end
end
9 changes: 0 additions & 9 deletions lib/we_pay/client.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
##
# Copyright (c) 2012-2016 WePay.
#
# http://opensource.org/licenses/Apache2.0
##

require 'cgi'
require 'json'
require 'net/http'
require 'net/https'

module WePay

Expand Down
19 changes: 19 additions & 0 deletions lib/we_pay/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'singleton'

module WePay
class Configuration
include ::Singleton

attr_writer :http_adapter

def http_adapter
@http_adapter || default_http_adapter
end

private

def default_http_adapter
:net_http
end
end
end
14 changes: 13 additions & 1 deletion lib/wepay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@
# The root WePay namespace.
##
module WePay
autoload :Client, 'we_pay/client'
autoload :Configuration, 'we_pay/configuration'

autoload :Client, 'we_pay/client'
autoload :BaseRequest, 'we_pay/base_request'
autoload :TestRequest, 'we_pay/test_request'
autoload :ProductionRequest, 'we_pay/production_request'

class << self

def configure(&block)
block.call(configuration)
end

def configuration
WePay::Configuration.instance
end
end
end
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
SimpleCov.start
SimpleCov.start do
add_filter "/spec/"
end

require 'wepay'

Expand Down
34 changes: 34 additions & 0 deletions spec/we_pay/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

RSpec.describe WePay::Configuration do

subject { described_class.instance }

describe "as a singleton" do
it "returns an instance" do
expect(described_class.instance).to be_an_instance_of(described_class)
end
end

describe "#http_adapter" do
context "by default" do
it "returns Net Http" do
expect(subject.http_adapter).to eq(:net_http)
end
end

context "with an explicit adapter" do
before do
subject.http_adapter = :patron
end

after do
subject.http_adapter = nil
end

it "returns the adapter" do
expect(subject.http_adapter).to eq(:patron)
end
end
end
end
65 changes: 40 additions & 25 deletions spec/we_pay/production_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,30 @@
RSpec.describe WePay::ProductionRequest do

describe "#response" do
let(:http_response) { double("net::http::response") }
let(:http_post) { double("net::http::post") }
let(:http_client) { double("net::http") }
let(:client) { double("faraday::connection") }
let(:request) { double("faraday::request") }
let(:response) { double("faraday::response") }

let(:header_opts) do
{
content_type: 'application/json',
user_agent: 'WePay Ruby SDK'
}
end

let(:request_opts) do
{
timeout: 30
}
end

let(:ssl_opts) do
{
verify: true
}
end

let(:default_http_adapter) { :net_http }

subject do
described_class.new(
Expand All @@ -19,31 +40,25 @@
end

before do
allow(http_response).to receive(:body).and_return({ response: 'response' }.to_json)

allow(Net::HTTP::Post).to receive(:new).with(
"/path",
'Content-Type' => 'application/json',
'User-Agent' => 'WePay Ruby SDK'
).and_return(http_post)

allow(http_post).to receive(:body=).with({ data: 'data' }.to_json)
allow(http_post).to receive(:add_field).with('Authorization', "Bearer access_token")
allow(http_post).to receive(:add_field).with('Api-Version', "api_version")

allow(Net::HTTP).to receive(:new).with(
"wepayapi.com",
443
).and_return(http_client)

allow(http_client).to receive(:read_timeout=).with(30)
allow(http_client).to receive(:use_ssl=).with(true)
allow(http_client).to receive(:request).with(http_post).and_return(http_response)
allow(http_client).to receive(:start).and_yield(http_client)
allow(Faraday).to receive(:new).with(
URI.parse("https://wepayapi.com/v2/path"),
headers: header_opts,
request: request_opts,
ssl: ssl_opts,
adapter: default_http_adapter
).and_return(client)

allow(client).to receive(:post).and_yield(request).and_return(response)

allow(request).to receive(:url).with("/v2/path")
allow(request).to receive(:body=).with({data: 'data'}.to_json)
allow(request).to receive(:headers).and_return({})

expect(response).to receive(:body).and_return({ status: :ok}.to_json)
end

it "returns response data" do
expect(subject.response).to eq({ 'response' => 'response' })
expect(subject.response).to eq('status' => 'ok')
end
end
end
Loading