borneo 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/lib/borneo.rb +1 -0
- data/lib/borneo/client.rb +30 -3
- data/lib/borneo/method_proxy.rb +24 -12
- data/lib/borneo/mock.rb +6 -0
- data/lib/borneo/mock/api.rb +9 -0
- data/lib/borneo/mock/service.rb +34 -0
- data/lib/borneo/rspec.rb +17 -0
- data/lib/borneo/version.rb +1 -1
- data/spec/core/client_spec.rb +41 -0
- data/spec/mock/service_spec.rb +64 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfe703be7f73d5c997eeb801e0e4d4c17dc92c23
|
4
|
+
data.tar.gz: 04731283ad3ef4c40d0e9ebe18de0e11873c85bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fee1d042eacd9c9331e6e7b826cf74d8551e02ac42ac9365b94047d1b22ecaf76fb54bda04bc812c30da712cac1bec69481ecc6cf738a5ca781a5909131d6313
|
7
|
+
data.tar.gz: 36e34278b5471fdc491266363633bddba72164d0a9c322c9e03a0716d782d9e9803f7bdb660d86248c5643c038b13c6a48e49a1507a2b8d1b1848b78f62328bb
|
data/README.md
CHANGED
@@ -48,6 +48,28 @@ The objects returned from the methods behave like normal Ruby objects:
|
|
48
48
|
Borneo raises an error if the operation wasn't permitted. If the access token is
|
49
49
|
stale, the library will try to refresh it once before raising an error.
|
50
50
|
|
51
|
+
## Mocking
|
52
|
+
|
53
|
+
To help test these services, you can switch the library into 'mocking mode' by:
|
54
|
+
|
55
|
+
Borneo::Client.enable_mocking!
|
56
|
+
|
57
|
+
Before each test, you may want to clear the existing mock returns by calling:
|
58
|
+
|
59
|
+
Borneo::Client.reset_mocks!
|
60
|
+
|
61
|
+
If you are using RSpec, both of these can be done by adding the following to your `spec_helper.rb`:
|
62
|
+
|
63
|
+
require 'borneo/rspec'
|
64
|
+
|
65
|
+
Declaring a simple mock response can then be done as follows:
|
66
|
+
|
67
|
+
Borneo::Client.stub_service('plus', 'v1').activities.list.to_return {:items => [ ... ]}
|
68
|
+
|
69
|
+
Using the RSpec helpers, this may also be simplified to:
|
70
|
+
|
71
|
+
stub_service('plus', 'v1').activities.list.to_return {:items => [ ... ]}
|
72
|
+
|
51
73
|
## Contributing
|
52
74
|
|
53
75
|
1. Fork it
|
data/lib/borneo.rb
CHANGED
data/lib/borneo/client.rb
CHANGED
@@ -2,20 +2,47 @@ require 'google/api_client'
|
|
2
2
|
|
3
3
|
class Borneo::Client
|
4
4
|
|
5
|
-
|
5
|
+
@@should_mock_requests = false
|
6
6
|
|
7
|
-
|
7
|
+
attr_reader :client_id, :client_secret, :redirect_url, :application_name, :application_version
|
8
|
+
|
9
|
+
def self.enable_mocking!
|
10
|
+
@@should_mock_requests = true
|
11
|
+
@@service_stubs = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.disable_mocking!
|
15
|
+
@@should_mock_requests = false
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.reset_mocks!
|
19
|
+
@@service_stubs = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.stub_service(service_name, service_version)
|
23
|
+
@@service_stubs[[service_name, service_version]] ||= Borneo::Mock::Service.new(self, service_name, service_version)
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(client_id, client_secret, redirect_url, application_name = "Borneo Application", application_version = "0.0.1")
|
8
27
|
@client_id = client_id
|
9
28
|
@client_secret = client_secret
|
10
29
|
@redirect_url = redirect_url
|
30
|
+
@google_client = nil
|
31
|
+
@application_name = application_name
|
32
|
+
@application_version = application_version
|
11
33
|
end
|
12
34
|
|
13
35
|
def for(access_token, refresh_token)
|
36
|
+
google_client.authorization.clear_credentials!
|
14
37
|
Borneo::AuthorizedProxy.new(self,access_token, refresh_token)
|
15
38
|
end
|
16
39
|
|
17
40
|
def google_client
|
18
|
-
Google::APIClient.new
|
41
|
+
@google_client ||= Google::APIClient.new(:application_name => @application_name, :application_version => @application_version)
|
42
|
+
end
|
43
|
+
|
44
|
+
def mocking_requests?
|
45
|
+
!!@@should_mock_requests
|
19
46
|
end
|
20
47
|
|
21
48
|
end
|
data/lib/borneo/method_proxy.rb
CHANGED
@@ -12,21 +12,29 @@ class Borneo::MethodProxy
|
|
12
12
|
@_components << name
|
13
13
|
end
|
14
14
|
|
15
|
+
def mocking_enabled?
|
16
|
+
@_service.proxy.client.mocking_requests?
|
17
|
+
end
|
18
|
+
|
15
19
|
def call(params = {})
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
if response.status == Borneo::ResponseStatus::STALE_ACCESS_TOKEN
|
25
|
-
_client.authorization.fetch_access_token!
|
20
|
+
unless mocking_enabled?
|
21
|
+
method_call = lambda do
|
22
|
+
_client.execute(
|
23
|
+
:api_method => _method,
|
24
|
+
:authorization => _authorization,
|
25
|
+
:parameters => params
|
26
|
+
)
|
27
|
+
end
|
26
28
|
response = method_call.call()
|
27
|
-
|
29
|
+
if response.status == Borneo::ResponseStatus::STALE_ACCESS_TOKEN
|
30
|
+
_client.authorization.fetch_access_token!
|
31
|
+
response = method_call.call()
|
32
|
+
end
|
28
33
|
|
29
|
-
|
34
|
+
data = response.data
|
35
|
+
else
|
36
|
+
data = mock_service.mock_response(@_components)
|
37
|
+
end
|
30
38
|
|
31
39
|
data
|
32
40
|
|
@@ -48,6 +56,10 @@ class Borneo::MethodProxy
|
|
48
56
|
m
|
49
57
|
end
|
50
58
|
|
59
|
+
def mock_service
|
60
|
+
Borneo::Client.stub_service(@_service.name, @_service.version)
|
61
|
+
end
|
62
|
+
|
51
63
|
def method_missing(name)
|
52
64
|
Borneo::MethodProxy.new(@_service, name, @_components)
|
53
65
|
end
|
data/lib/borneo/mock.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Borneo::Mock::Service
|
2
|
+
|
3
|
+
attr :client, :name, :version
|
4
|
+
|
5
|
+
def initialize(client, name, version)
|
6
|
+
@client = client
|
7
|
+
@name = name
|
8
|
+
@version = version
|
9
|
+
@methods = {}
|
10
|
+
@response_data = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_return(result)
|
14
|
+
@response_data = result
|
15
|
+
end
|
16
|
+
|
17
|
+
def mock_response(components)
|
18
|
+
if components.count == 0
|
19
|
+
@response_data
|
20
|
+
else
|
21
|
+
next_proxy = @methods[components[0]]
|
22
|
+
unless next_proxy.nil?
|
23
|
+
next_proxy.mock_response(components.drop(1))
|
24
|
+
else
|
25
|
+
raise "Request not stubbed for #{components}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing(method_name)
|
31
|
+
@methods[method_name] ||= Borneo::Mock::Service.new(@client, @name, @version)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/lib/borneo/rspec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'borneo'
|
2
|
+
|
3
|
+
require 'rspec/core'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
|
7
|
+
config.include Borneo::Mock::API
|
8
|
+
|
9
|
+
config.before(:each) do
|
10
|
+
Borneo::Client.enable_mocking!
|
11
|
+
end
|
12
|
+
|
13
|
+
config.after(:each) do
|
14
|
+
Borneo::Client.reset_mocks!
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/lib/borneo/version.rb
CHANGED
data/spec/core/client_spec.rb
CHANGED
@@ -5,6 +5,8 @@ describe Borneo::Client do
|
|
5
5
|
let(:client_id) { "SOME_CLIENT_ID" }
|
6
6
|
let(:client_secret) { "SOME_CLIENT_SECRET" }
|
7
7
|
let(:redirect_url) { "http://localhost" }
|
8
|
+
let(:app_name) { "SomeApplication" }
|
9
|
+
let(:app_version) { "1.0.1" }
|
8
10
|
|
9
11
|
describe "initialiser" do
|
10
12
|
it "takes a client ID, client secret and redirect URL parameters" do
|
@@ -13,6 +15,17 @@ describe Borneo::Client do
|
|
13
15
|
client.client_secret.should == client_secret
|
14
16
|
client.redirect_url.should == redirect_url
|
15
17
|
end
|
18
|
+
|
19
|
+
it "optionally takes an application name and version" do
|
20
|
+
client = Borneo::Client.new(client_id, client_secret, redirect_url, app_name, app_version)
|
21
|
+
client.application_name.should == app_name
|
22
|
+
client.application_version.should == app_version
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should cache the Google client value" do
|
27
|
+
client = Borneo::Client.new(client_id, client_secret, redirect_url)
|
28
|
+
client.google_client.should == client.google_client
|
16
29
|
end
|
17
30
|
|
18
31
|
describe "for" do
|
@@ -43,4 +56,32 @@ describe Borneo::Client do
|
|
43
56
|
|
44
57
|
end
|
45
58
|
|
59
|
+
describe "mocking" do
|
60
|
+
let(:client) { Borneo::Client.new(client_id, client_secret, redirect_url) }
|
61
|
+
|
62
|
+
it "defaults to not mocking" do
|
63
|
+
client.should_not be_mocking_requests
|
64
|
+
end
|
65
|
+
|
66
|
+
it "can be set globally to use a mock rather than calling external services" do
|
67
|
+
Borneo::Client.enable_mocking!
|
68
|
+
client.should be_mocking_requests
|
69
|
+
end
|
70
|
+
|
71
|
+
it "can be set globally to disable mocking" do
|
72
|
+
Borneo::Client.enable_mocking!
|
73
|
+
client.should be_mocking_requests
|
74
|
+
Borneo::Client.disable_mocking!
|
75
|
+
client.should_not be_mocking_requests
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "stub_service" do
|
79
|
+
it "should return a mock service" do
|
80
|
+
Borneo::Client.stub_service('plus', 'v1').should be_a(Borneo::Mock::Service)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
|
46
87
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../spec_helper.rb"
|
2
|
+
|
3
|
+
describe Borneo::Mock::Service do
|
4
|
+
|
5
|
+
let(:client_id) { "SOME_CLIENT_ID" }
|
6
|
+
let(:client_secret) { "SOME_CLIENT_SECRET" }
|
7
|
+
let(:redirect_url) { "http://localhost" }
|
8
|
+
let(:client) { Borneo::Client.new(client_id, client_secret, redirect_url) }
|
9
|
+
let(:service_name) { "oauth2" }
|
10
|
+
let(:service_version) { "v2" }
|
11
|
+
|
12
|
+
describe "initializer" do
|
13
|
+
it "should take the client, service name and service version as parameters" do
|
14
|
+
service = Borneo::Mock::Service.new(client, service_name, service_version)
|
15
|
+
service.client.should == client
|
16
|
+
service.name.should == service_name
|
17
|
+
service.version.should == service_version
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "stubbed responses" do
|
22
|
+
let(:client_id) { "SOME_CLIENT_ID" }
|
23
|
+
let(:client_secret) { "SOME_CLIENT_SECRET" }
|
24
|
+
let(:redirect_url) { "http://localhost" }
|
25
|
+
let(:client) { Borneo::Client.new(client_id, client_secret, redirect_url) }
|
26
|
+
let(:access_token) { "ACCESS_TOKEN" }
|
27
|
+
let(:refresh_token) { "REFRESH_TOKEN" }
|
28
|
+
let(:service_name) { "oauth2" }
|
29
|
+
let(:service_version) { "v2" }
|
30
|
+
let(:service) { Borneo::Mock::Service.new(client, service_name, service_version) }
|
31
|
+
let(:stubbed_response) { { :id => "0000", :email => "jsmith@example.com" } }
|
32
|
+
|
33
|
+
it "allows simple responses to be stubbed" do
|
34
|
+
service.userinfo.should respond_to(:to_return)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns stubbed response when query is made" do
|
38
|
+
Borneo::Client.enable_mocking!
|
39
|
+
Borneo::Client.stub_service(service_name, service_version).userinfo.to_return stubbed_response
|
40
|
+
client.for(access_token, refresh_token).service(service_name, service_version).userinfo.call.should == stubbed_response
|
41
|
+
end
|
42
|
+
|
43
|
+
it "raises an error when mocks aren't defined" do
|
44
|
+
Borneo::Client.enable_mocking!
|
45
|
+
lambda { client.for(access_token, refresh_token).service(service_name, service_version).userinfo.call }.should raise_exception(RuntimeError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "raises an error when mocks are reset" do
|
49
|
+
Borneo::Client.enable_mocking!
|
50
|
+
Borneo::Client.stub_service(service_name, service_version).userinfo.to_return stubbed_response
|
51
|
+
Borneo::Client.reset_mocks!
|
52
|
+
lambda { client.for(access_token, refresh_token).service(service_name, service_version).userinfo.call }.should raise_exception(RuntimeError)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "methods" do
|
57
|
+
let(:service) { Borneo::Mock::Service.new(client, service_name, service_version) }
|
58
|
+
|
59
|
+
it "should return another mock service for anything else" do
|
60
|
+
service.userinfo.should be_a(Borneo::Mock::Service)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: borneo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Geldart
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-api-client
|
@@ -125,6 +125,10 @@ files:
|
|
125
125
|
- lib/borneo/authorized_proxy.rb
|
126
126
|
- lib/borneo/client.rb
|
127
127
|
- lib/borneo/method_proxy.rb
|
128
|
+
- lib/borneo/mock.rb
|
129
|
+
- lib/borneo/mock/api.rb
|
130
|
+
- lib/borneo/mock/service.rb
|
131
|
+
- lib/borneo/rspec.rb
|
128
132
|
- lib/borneo/service.rb
|
129
133
|
- lib/borneo/version.rb
|
130
134
|
- spec/core/authorized_proxy_spec.rb
|
@@ -132,6 +136,7 @@ files:
|
|
132
136
|
- spec/core/method_proxy_spec.rb
|
133
137
|
- spec/core/service_spec.rb
|
134
138
|
- spec/discovery_document.json
|
139
|
+
- spec/mock/service_spec.rb
|
135
140
|
- spec/spec.opts
|
136
141
|
- spec/spec_helper.rb
|
137
142
|
homepage: ''
|
@@ -165,5 +170,6 @@ test_files:
|
|
165
170
|
- spec/core/method_proxy_spec.rb
|
166
171
|
- spec/core/service_spec.rb
|
167
172
|
- spec/discovery_document.json
|
173
|
+
- spec/mock/service_spec.rb
|
168
174
|
- spec/spec.opts
|
169
175
|
- spec/spec_helper.rb
|