sagamore-client 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +4 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +6 -0
  5. data/.yardopts +1 -0
  6. data/Gemfile +14 -0
  7. data/Gemfile.lock +66 -0
  8. data/LICENSE +21 -0
  9. data/README.md +252 -0
  10. data/Rakefile +16 -0
  11. data/lib/sagamore-client.rb +1 -0
  12. data/lib/sagamore/client.rb +263 -0
  13. data/lib/sagamore/client/body.rb +12 -0
  14. data/lib/sagamore/client/collection.rb +108 -0
  15. data/lib/sagamore/client/errors.rb +17 -0
  16. data/lib/sagamore/client/resource.rb +205 -0
  17. data/lib/sagamore/client/response.rb +85 -0
  18. data/lib/sagamore/client/uri_ext.rb +51 -0
  19. data/sagamore-client.gemspec +20 -0
  20. data/spec/sagamore/client/body_spec.rb +32 -0
  21. data/spec/sagamore/client/resource_spec.rb +250 -0
  22. data/spec/sagamore/client/response_spec.rb +100 -0
  23. data/spec/sagamore/client/uri_ext_spec.rb +51 -0
  24. data/spec/sagamore/client_spec.rb +214 -0
  25. data/spec/shared_client_context.rb +5 -0
  26. data/spec/spec_helper.rb +12 -0
  27. data/vendor/cache/addressable-2.2.8.gem +0 -0
  28. data/vendor/cache/coderay-1.0.7.gem +0 -0
  29. data/vendor/cache/coveralls-0.6.9.gem +0 -0
  30. data/vendor/cache/crack-0.3.1.gem +0 -0
  31. data/vendor/cache/diff-lcs-1.1.3.gem +0 -0
  32. data/vendor/cache/hashie-1.2.0.gem +0 -0
  33. data/vendor/cache/json-1.7.4.gem +0 -0
  34. data/vendor/cache/method_source-0.8.gem +0 -0
  35. data/vendor/cache/mime-types-1.25.gem +0 -0
  36. data/vendor/cache/multi_json-1.8.0.gem +0 -0
  37. data/vendor/cache/patron-0.4.18.gem +0 -0
  38. data/vendor/cache/pry-0.9.10.gem +0 -0
  39. data/vendor/cache/rake-0.9.2.2.gem +0 -0
  40. data/vendor/cache/rest-client-1.6.7.gem +0 -0
  41. data/vendor/cache/rspec-2.11.0.gem +0 -0
  42. data/vendor/cache/rspec-core-2.11.1.gem +0 -0
  43. data/vendor/cache/rspec-expectations-2.11.2.gem +0 -0
  44. data/vendor/cache/rspec-mocks-2.11.1.gem +0 -0
  45. data/vendor/cache/simplecov-0.7.1.gem +0 -0
  46. data/vendor/cache/simplecov-html-0.7.1.gem +0 -0
  47. data/vendor/cache/slop-3.3.2.gem +0 -0
  48. data/vendor/cache/term-ansicolor-1.2.2.gem +0 -0
  49. data/vendor/cache/thor-0.18.1.gem +0 -0
  50. data/vendor/cache/tins-0.9.0.gem +0 -0
  51. data/vendor/cache/webmock-1.8.8.gem +0 -0
  52. metadata +148 -0
@@ -0,0 +1,214 @@
1
+ require 'spec_helper'
2
+
3
+ describe Sagamore::Client do
4
+ include_context "client"
5
+
6
+ describe "session" do
7
+ it "should be a Patron::Session" do
8
+ client.session.should be_a Patron::Session
9
+ end
10
+ end
11
+
12
+ describe "auth" do
13
+ it "should attempt to authenticate with the given username and password" do
14
+ request_stub = stub_request(:post, "#{base_url}/auth/identity/callback").with \
15
+ :body => "auth_key=coco&password=boggle",
16
+ :headers => {'Content-Type' => 'application/x-www-form-urlencoded'}
17
+ client.auth(:username => 'coco', :password => 'boggle')
18
+ request_stub.should have_been_requested
19
+ end
20
+
21
+ it "should raise an exception if called without username or password" do
22
+ lambda { client.auth }.should raise_error("Must specify :username and :password")
23
+ lambda { client.auth(:username => 'x') }.should raise_error("Must specify :username and :password")
24
+ lambda { client.auth(:password => 'y') }.should raise_error("Must specify :username and :password")
25
+ end
26
+
27
+ it "should return true if auth succeeds" do
28
+ stub_request(:post, "#{base_url}/auth/identity/callback").to_return(:status => 200)
29
+ lambda { client.auth(:username => 'someone', :password => 'right') }.should be_true
30
+ end
31
+
32
+ it "should raise an AuthFailed if auth fails" do
33
+ stub_request(:post, "#{base_url}/auth/identity/callback").to_return(:status => 401)
34
+ lambda { client.auth(:username => 'someone', :password => 'wrong') }.should \
35
+ raise_error(Sagamore::Client::AuthFailed, "Sagamore auth failed")
36
+ end
37
+ end
38
+
39
+ describe "initialize" do
40
+ it "should call configure_session" do
41
+ Sagamore::Client.any_instance.should_receive(:configure_session).with(base_url, {:x => 'y'})
42
+ Sagamore::Client.new(base_url, :x => 'y')
43
+ end
44
+ end
45
+
46
+ describe "configure_session" do
47
+ it "should set the session's base_url" do
48
+ session.should_receive(:base_url=).with(base_url)
49
+ client.__send__(:configure_session, base_url, :x => 'y')
50
+ end
51
+
52
+ it "should enable cookies" do
53
+ session.should_receive(:handle_cookies)
54
+ client.__send__(:configure_session, base_url, :x => 'y')
55
+ end
56
+
57
+ it "should allow setting insecure on the session" do
58
+ session.should_receive(:insecure=).with(true)
59
+ client.__send__(:configure_session, base_url, :insecure => true)
60
+ end
61
+
62
+ it "set the default timeout" do
63
+ client.__send__(:configure_session, base_url, {})
64
+ client.session.timeout.should == Sagamore::Client::DEFAULT_TIMEOUT
65
+ end
66
+
67
+ it "set the default connect timeout" do
68
+ client.__send__(:configure_session, base_url, {})
69
+ client.session.connect_timeout.should == Sagamore::Client::DEFAULT_CONNECT_TIMEOUT
70
+ end
71
+ end
72
+
73
+ describe "[]" do
74
+ it "should return a resource object with the given path and client" do
75
+ client["path"].should be_a Sagamore::Client::Resource
76
+ end
77
+ end
78
+
79
+
80
+ [:get, :head, :delete].each do |method|
81
+ bang_method = "#{method}!"
82
+ describe method do
83
+ it "should call session's #{method}" do
84
+ session.should_receive(method).with('/relative/path')
85
+ client.__send__(method, '/relative/path')
86
+ end
87
+
88
+ it "should return a Sagamore::Client::Response" do
89
+ request_stub = stub_request(method, "#{base_url}/relative/path")
90
+ response = client.__send__(method, '/relative/path')
91
+ response.should be_a Sagamore::Client::Response
92
+ end
93
+
94
+ it "should remove redundant base path prefix from URL if present" do
95
+ request_stub = stub_request(method, "#{base_url}/relative/path")
96
+ response = client.__send__(method, '/api/relative/path')
97
+ response.should be_a Sagamore::Client::Response
98
+ end
99
+ end
100
+
101
+ describe bang_method do
102
+ it "should call #{method}" do
103
+ response = mock(Sagamore::Client::Response)
104
+ response.should_receive(:success?).and_return(true)
105
+ client.should_receive(method).with('/path', false).and_return(response)
106
+ client.__send__(bang_method, '/path').should === response
107
+ end
108
+
109
+ it "should raise an exception on failure" do
110
+ response = mock(Sagamore::Client::Response)
111
+ response.should_receive(:success?).and_return(false)
112
+ response.should_receive(:status_line).and_return('404 Not Found')
113
+ client.should_receive(method).with('/path', false).and_return(response)
114
+ lambda { client.send(bang_method, '/path') }.should raise_error(Sagamore::Client::RequestFailed)
115
+ end
116
+ end
117
+ end
118
+
119
+
120
+ [:put, :post].each do |method|
121
+ bang_method = "#{method}!"
122
+ describe method do
123
+ it "should call session's #{method}" do
124
+ session.should_receive(method).with('/relative/path', 'body')
125
+ client.__send__(method, '/relative/path', 'body')
126
+ end
127
+
128
+ it "should return a Sagamore::Client::Response" do
129
+ request_stub = stub_request(method, "#{base_url}/relative/path")
130
+ response = client.__send__(method, '/relative/path', 'body')
131
+ response.should be_a Sagamore::Client::Response
132
+ end
133
+
134
+ it "should serialize the request body as JSON if it is a hash" do
135
+ body_hash = {:key1 => 'val1', :key2 => 'val2'}
136
+ session.should_receive(method).with('/path', body_hash.to_json)
137
+ client.__send__(method, '/path', body_hash)
138
+ end
139
+
140
+ it "should set the Content-Type header to application/json if not specified" do
141
+ request_stub = stub_request(method, "#{base_url}/my/resource").
142
+ with(:headers => {'Content-Type' => 'application/json'})
143
+ client.__send__(method, '/my/resource', :key1 => 'val1')
144
+ request_stub.should have_been_requested
145
+ end
146
+
147
+ it "should set the Content-Type header to specified value if specified" do
148
+ request_stub = stub_request(method, "#{base_url}/my/resource").
149
+ with(:headers => {'Content-Type' => 'application/pdf'})
150
+ client.__send__(method, '/my/resource', {:key1 => 'val1'}, 'Content-Type' => 'application/pdf')
151
+ request_stub.should have_been_requested
152
+ end
153
+ end
154
+
155
+ describe bang_method do
156
+ it "should call #{method}" do
157
+ response = mock(Sagamore::Client::Response)
158
+ response.should_receive(:success?).and_return(true)
159
+ client.should_receive(method).with('/path', 'body', false).and_return(response)
160
+ client.__send__(bang_method, '/path', 'body').should === response
161
+ end
162
+
163
+ it "should raise an exception on failure" do
164
+ response = mock(Sagamore::Client::Response)
165
+ response.should_receive(:success?).and_return(false)
166
+ response.should_receive(:status_line).and_return('404 Not Found')
167
+ client.should_receive(method).with('/path', 'body', false).and_return(response)
168
+ expect { client.send(bang_method, '/path', 'body') }.to raise_error { |error|
169
+ error.should be_a(Sagamore::Client::RequestFailed)
170
+ error.response.should === response
171
+ }
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "each_page" do
177
+ it "should request each page of the collection and yield each response to the block" do
178
+ responses = (1..3).map do |p|
179
+ response = mock(Sagamore::Client::Response)
180
+ response.stub(:[]).with('pages').and_return(3)
181
+
182
+ client.should_receive(:get!).with("/things?page=#{p}&per_page=20".to_uri).and_return(response)
183
+
184
+ response
185
+ end
186
+
187
+ expect do |block|
188
+ client.each_page('/things', &block)
189
+ end.to yield_successive_args(*responses)
190
+ end
191
+ end
192
+
193
+ describe "each" do
194
+ it "should request each page of the collection and yield each individual result to the block" do
195
+ all_results = (1..3).inject([]) do |results, p|
196
+ response = mock(Sagamore::Client::Response)
197
+ response.stub(:[]).with('pages').and_return(3)
198
+
199
+ page_results = 20.times.map {|i| "Page #{p} result #{i+1}"}
200
+ results += page_results
201
+
202
+ response.stub(:[]).with('results').and_return(page_results)
203
+
204
+ client.should_receive(:get!).with("/things?page=#{p}&per_page=20".to_uri).and_return(response)
205
+
206
+ results
207
+ end
208
+
209
+ expect do |block|
210
+ client.each('/things', &block)
211
+ end.to yield_successive_args(*all_results)
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,5 @@
1
+ shared_context "client" do
2
+ let(:base_url) { "http://bozo.com/api" }
3
+ let(:client) { Sagamore::Client.new(base_url) }
4
+ let(:session) { client.session }
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'sagamore-client'
5
+ require 'webmock/rspec'
6
+ require 'shared_client_context'
7
+
8
+ class String
9
+ def to_uri
10
+ Addressable::URI.parse(self)
11
+ end
12
+ end
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sagamore-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 3.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sagamore
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: patron
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.18
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.18
27
+ - !ruby/object:Gem::Dependency
28
+ name: addressable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.8
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.8
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.7.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.7.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: hashie
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - .gitignore
76
+ - .rspec
77
+ - .travis.yml
78
+ - .yardopts
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE
82
+ - README.md
83
+ - Rakefile
84
+ - lib/sagamore-client.rb
85
+ - lib/sagamore/client.rb
86
+ - lib/sagamore/client/body.rb
87
+ - lib/sagamore/client/collection.rb
88
+ - lib/sagamore/client/errors.rb
89
+ - lib/sagamore/client/resource.rb
90
+ - lib/sagamore/client/response.rb
91
+ - lib/sagamore/client/uri_ext.rb
92
+ - sagamore-client.gemspec
93
+ - spec/sagamore/client/body_spec.rb
94
+ - spec/sagamore/client/resource_spec.rb
95
+ - spec/sagamore/client/response_spec.rb
96
+ - spec/sagamore/client/uri_ext_spec.rb
97
+ - spec/sagamore/client_spec.rb
98
+ - spec/shared_client_context.rb
99
+ - spec/spec_helper.rb
100
+ - vendor/cache/addressable-2.2.8.gem
101
+ - vendor/cache/coderay-1.0.7.gem
102
+ - vendor/cache/coveralls-0.6.9.gem
103
+ - vendor/cache/crack-0.3.1.gem
104
+ - vendor/cache/diff-lcs-1.1.3.gem
105
+ - vendor/cache/hashie-1.2.0.gem
106
+ - vendor/cache/json-1.7.4.gem
107
+ - vendor/cache/method_source-0.8.gem
108
+ - vendor/cache/mime-types-1.25.gem
109
+ - vendor/cache/multi_json-1.8.0.gem
110
+ - vendor/cache/patron-0.4.18.gem
111
+ - vendor/cache/pry-0.9.10.gem
112
+ - vendor/cache/rake-0.9.2.2.gem
113
+ - vendor/cache/rest-client-1.6.7.gem
114
+ - vendor/cache/rspec-2.11.0.gem
115
+ - vendor/cache/rspec-core-2.11.1.gem
116
+ - vendor/cache/rspec-expectations-2.11.2.gem
117
+ - vendor/cache/rspec-mocks-2.11.1.gem
118
+ - vendor/cache/simplecov-0.7.1.gem
119
+ - vendor/cache/simplecov-html-0.7.1.gem
120
+ - vendor/cache/slop-3.3.2.gem
121
+ - vendor/cache/term-ansicolor-1.2.2.gem
122
+ - vendor/cache/thor-0.18.1.gem
123
+ - vendor/cache/tins-0.9.0.gem
124
+ - vendor/cache/webmock-1.8.8.gem
125
+ homepage:
126
+ licenses: []
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: 1.3.6
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.1.4
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Sagamore client library
148
+ test_files: []