tango-client 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/README.md +76 -0
- data/Rakefile +5 -0
- data/lib/tango.rb +47 -0
- data/lib/tango/client.rb +91 -0
- data/lib/tango/default.rb +56 -0
- data/lib/tango/error.rb +78 -0
- data/lib/tango/request/json_encoded.rb +41 -0
- data/lib/tango/response/parse_json.rb +32 -0
- data/lib/tango/response/raise_error.rb +19 -0
- data/lib/tango/ssl/cacert.pem +3825 -0
- data/lib/tango/version.rb +3 -0
- data/spec/lib/tango/response/raise_error_spec.rb +0 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/faraday_stub.rb +42 -0
- data/spec/tango/client_spec.rb +42 -0
- data/spec/tango/error_spec.rb +32 -0
- data/spec/tango/request/json_encoded_spec.rb +29 -0
- data/spec/tango/response/parse_json_spec.rb +47 -0
- data/spec/tango/response/raise_error_spec.rb +41 -0
- data/tango-client.gemspec +28 -0
- metadata +212 -0
File without changes
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
if ENV['COVERAGE']
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter 'spec'
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module FaradayStub
|
4
|
+
def stub_request(connection)
|
5
|
+
unless connection.options[:stubs]
|
6
|
+
# Inject Faraday::Adapter::Test and rebuild the app
|
7
|
+
connection.instance_variable_set(:@app, nil)
|
8
|
+
index = connection.builder.handlers.size - 1
|
9
|
+
stubs = Faraday::Adapter::Test::Stubs.new
|
10
|
+
connection.builder.swap index, Faraday::Adapter::Test, stubs
|
11
|
+
connection.options[:stubs] = stubs
|
12
|
+
end
|
13
|
+
|
14
|
+
yield connection.options[:stubs] if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :stub_connection, :stub_request
|
18
|
+
|
19
|
+
def stub_get(connection, path, &block)
|
20
|
+
stub_request(connection) do |stubs|
|
21
|
+
stubs.get(path, &block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def stub_post(connection, path, body = nil, &block)
|
26
|
+
stub_request(connection) do |stubs|
|
27
|
+
stubs.post(path, body, &block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def stub_put(connection, path, body = nil, &block)
|
32
|
+
stub_request(connection) do |stubs|
|
33
|
+
stubs.put(path, body, &block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def stub_delete(connection, path, &block)
|
38
|
+
stub_request(connection) do |stubs|
|
39
|
+
stubs.delete(path, &block)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'multi_json'
|
3
|
+
require 'tango/client'
|
4
|
+
|
5
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
6
|
+
|
7
|
+
describe Tango::Client do
|
8
|
+
|
9
|
+
include FaradayStub
|
10
|
+
|
11
|
+
let(:account_credentials) {
|
12
|
+
{
|
13
|
+
:username => 'ian',
|
14
|
+
:password => 'secret'
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
let(:client) { Tango::Client.new account_credentials }
|
19
|
+
|
20
|
+
let(:url_prefix) { '/' + client.options[:version] }
|
21
|
+
|
22
|
+
subject { client }
|
23
|
+
before { stub_request(client.connection) }
|
24
|
+
|
25
|
+
describe '#get_available_balance' do
|
26
|
+
let(:response) {
|
27
|
+
{
|
28
|
+
:responseType => 'SUCCESS',
|
29
|
+
:response => { :availableBalance => 873431432 }
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
before do
|
34
|
+
stub_post(client.connection, url_prefix + '/GetAvailableBalance') do |env|
|
35
|
+
[ 200, {:request_body => env[:body]}, MultiJson.dump(response) ]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
subject { client.get_available_balance }
|
40
|
+
it { should == 873431432 }
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'tango/error'
|
2
|
+
|
3
|
+
describe Tango::Error do
|
4
|
+
describe '.from_status_code' do
|
5
|
+
context 'when status code is 404' do
|
6
|
+
subject { Tango::Error.from_status_code(404) }
|
7
|
+
its(:message) { should == 'Error: 404' }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe Tango::Error::ServerError do
|
13
|
+
describe '.from_response' do
|
14
|
+
context 'when responseType is Unknown' do
|
15
|
+
subject {
|
16
|
+
Tango::Error::ServerError.from_response(:responseType => 'Unknown',
|
17
|
+
:response => 'test')
|
18
|
+
}
|
19
|
+
|
20
|
+
it { should be_a_kind_of(Tango::Error::ServerError) }
|
21
|
+
its(:response) { should == 'test' }
|
22
|
+
end
|
23
|
+
context 'when responseType is INV_CREDENTIAL' do
|
24
|
+
subject {
|
25
|
+
Tango::Error::ServerError.from_response(:responseType => 'INV_CREDENTIAL',
|
26
|
+
:response => 'test')
|
27
|
+
}
|
28
|
+
|
29
|
+
it { should be_a_kind_of(Tango::Error::InvCredential) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'tango/request/json_encoded'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
describe Tango::Request::JsonEncoded do
|
5
|
+
let(:conn) {
|
6
|
+
Faraday::Connection.new do |conn|
|
7
|
+
conn.use Tango::Request::JsonEncoded
|
8
|
+
|
9
|
+
conn.use Faraday::Adapter::Test do |stub|
|
10
|
+
stub.post '/echo' do |env|
|
11
|
+
# echo back request body
|
12
|
+
[ 200, {:request_body => env[:body]}, env[:body] ]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
}
|
17
|
+
|
18
|
+
context 'POST {"string":"hello"} with no content type' do
|
19
|
+
let(:env) { conn.post('/echo', :string => "hello").env }
|
20
|
+
|
21
|
+
it 'dumps params to JSON' do
|
22
|
+
env[:response_headers]['Request-Body'].should == %q({"string":"hello"})
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'sets content type to application/json' do
|
26
|
+
env[:request_headers]['Content-Type'].should == 'application/json'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'tango/response/parse_json'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
describe Tango::Response::ParseJson do
|
5
|
+
let(:conn) {
|
6
|
+
Faraday::Connection.new do |conn|
|
7
|
+
conn.use Tango::Response::ParseJson
|
8
|
+
|
9
|
+
conn.use Faraday::Adapter::Test do |stub|
|
10
|
+
stub.post '/echo' do |env|
|
11
|
+
# echo back request body
|
12
|
+
[ 200, {}, env[:body] ]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
}
|
17
|
+
|
18
|
+
context 'POST {"string":"hello"} with no content type' do
|
19
|
+
let(:env) { conn.post('/echo', %q[{"string":"hello"}]).env }
|
20
|
+
|
21
|
+
it 'raises ServerError' do
|
22
|
+
expect { env }.to raise_error(Tango::Error::ServerError)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context 'POST {"responseType":"SUCCESS","response":"OK"}' do
|
26
|
+
let(:env) { conn.post('/echo', %q[{"responseType":"SUCCESS","response":"OK"}]).env }
|
27
|
+
|
28
|
+
it 'raise ServerError' do
|
29
|
+
expect { env }.to raise_error(Tango::Error::ServerError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
context 'POST {"responseType":"SUCCESS","response":{"status":"OK"}}' do
|
33
|
+
let(:env) { conn.post('/echo', %q[{"responseType":"SUCCESS","response":{"status":"OK"}}]).env }
|
34
|
+
|
35
|
+
it 'returns {:status => "OK"}' do
|
36
|
+
env[:body].should == { :status => "OK" }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
context 'POST {"responseType":"INV_INPUT"}' do
|
40
|
+
it 'raises InvInput' do
|
41
|
+
expect {
|
42
|
+
conn.post('/echo', %q[{"responseType":"INV_INPUT"}])
|
43
|
+
}.to raise_error ::Tango::Error::InvInput
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'tango/response/raise_error'
|
2
|
+
require 'faraday'
|
3
|
+
|
4
|
+
describe Tango::Response::RaiseError do
|
5
|
+
context 'error class raises if status code >= 500' do
|
6
|
+
class Error < StandardError
|
7
|
+
def self.from_status_code(status_code)
|
8
|
+
new(status_code.to_s)
|
9
|
+
end
|
10
|
+
def self.raise_on?(status_code)
|
11
|
+
status_code >= 500
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:conn) {
|
16
|
+
Faraday::Connection.new do |conn|
|
17
|
+
conn.use Tango::Response::RaiseError, Error
|
18
|
+
|
19
|
+
conn.use Faraday::Adapter::Test do |stub|
|
20
|
+
stub.get '/499' do |env|
|
21
|
+
[ 400, {}, '' ]
|
22
|
+
end
|
23
|
+
stub.get '/500' do |env|
|
24
|
+
[ 500, {}, '' ]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
}
|
29
|
+
|
30
|
+
it 'raises error if status code is 500' do
|
31
|
+
expect {
|
32
|
+
conn.get('/500')
|
33
|
+
}.to raise_exception(Error)
|
34
|
+
end
|
35
|
+
it 'does not raise error if status code is 499' do
|
36
|
+
expect {
|
37
|
+
conn.get('/499')
|
38
|
+
}.not_to raise_exception(Error)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'tango/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "tango-client"
|
7
|
+
gem.version = Tango::VERSION
|
8
|
+
gem.authors = ["Ian Yang"]
|
9
|
+
gem.email = ["ian@intridea.com"]
|
10
|
+
gem.description = %q{HTTP client to ease using Tango API}
|
11
|
+
gem.summary = %q{HTTP client to ease using Tango API}
|
12
|
+
gem.homepage = ""
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($/)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.require_paths = ["lib"]
|
18
|
+
|
19
|
+
gem.add_dependency 'faraday', '~> 0.8'
|
20
|
+
gem.add_dependency 'multi_json', '~> 1.3'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'pry'
|
23
|
+
gem.add_development_dependency 'rake'
|
24
|
+
gem.add_development_dependency 'rspec'
|
25
|
+
gem.add_development_dependency 'yard'
|
26
|
+
gem.add_development_dependency 'redcarpet'
|
27
|
+
gem.add_development_dependency 'simplecov'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tango-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ian Yang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-12-04 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: faraday
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.8'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.8'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: multi_json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: pry
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: yard
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: redcarpet
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: simplecov
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
description: HTTP client to ease using Tango API
|
143
|
+
email:
|
144
|
+
- ian@intridea.com
|
145
|
+
executables: []
|
146
|
+
extensions: []
|
147
|
+
extra_rdoc_files: []
|
148
|
+
files:
|
149
|
+
- .gitignore
|
150
|
+
- .rspec
|
151
|
+
- .yardopts
|
152
|
+
- Gemfile
|
153
|
+
- README.md
|
154
|
+
- Rakefile
|
155
|
+
- lib/tango.rb
|
156
|
+
- lib/tango/client.rb
|
157
|
+
- lib/tango/default.rb
|
158
|
+
- lib/tango/error.rb
|
159
|
+
- lib/tango/request/json_encoded.rb
|
160
|
+
- lib/tango/response/parse_json.rb
|
161
|
+
- lib/tango/response/raise_error.rb
|
162
|
+
- lib/tango/ssl/cacert.pem
|
163
|
+
- lib/tango/version.rb
|
164
|
+
- spec/lib/tango/response/raise_error_spec.rb
|
165
|
+
- spec/spec_helper.rb
|
166
|
+
- spec/support/faraday_stub.rb
|
167
|
+
- spec/tango/client_spec.rb
|
168
|
+
- spec/tango/error_spec.rb
|
169
|
+
- spec/tango/request/json_encoded_spec.rb
|
170
|
+
- spec/tango/response/parse_json_spec.rb
|
171
|
+
- spec/tango/response/raise_error_spec.rb
|
172
|
+
- tango-client.gemspec
|
173
|
+
homepage: ''
|
174
|
+
licenses: []
|
175
|
+
post_install_message:
|
176
|
+
rdoc_options: []
|
177
|
+
require_paths:
|
178
|
+
- lib
|
179
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
180
|
+
none: false
|
181
|
+
requirements:
|
182
|
+
- - ! '>='
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
segments:
|
186
|
+
- 0
|
187
|
+
hash: 4124602399217005182
|
188
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
|
+
none: false
|
190
|
+
requirements:
|
191
|
+
- - ! '>='
|
192
|
+
- !ruby/object:Gem::Version
|
193
|
+
version: '0'
|
194
|
+
segments:
|
195
|
+
- 0
|
196
|
+
hash: 4124602399217005182
|
197
|
+
requirements: []
|
198
|
+
rubyforge_project:
|
199
|
+
rubygems_version: 1.8.23
|
200
|
+
signing_key:
|
201
|
+
specification_version: 3
|
202
|
+
summary: HTTP client to ease using Tango API
|
203
|
+
test_files:
|
204
|
+
- spec/lib/tango/response/raise_error_spec.rb
|
205
|
+
- spec/spec_helper.rb
|
206
|
+
- spec/support/faraday_stub.rb
|
207
|
+
- spec/tango/client_spec.rb
|
208
|
+
- spec/tango/error_spec.rb
|
209
|
+
- spec/tango/request/json_encoded_spec.rb
|
210
|
+
- spec/tango/response/parse_json_spec.rb
|
211
|
+
- spec/tango/response/raise_error_spec.rb
|
212
|
+
has_rdoc:
|