sunstone 1.6.4 → 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 051c69bddb929215445466e39b7ab1195f980713
4
- data.tar.gz: 33d67611b87dacf74d886c91a4d3cba1ff6bf9e6
3
+ metadata.gz: a0acc52add951c41b1a9af3b1c243e009db98025
4
+ data.tar.gz: 1138d06d272162568ec1aa5ab2a3a3e35d476c52
5
5
  SHA512:
6
- metadata.gz: 7912c0cd5e1b05b8ed7b3ddf38084b712fbcb031f0cfd8e05ad5719266b637c022ad459389b9cf71b298771a1f270382a8d663a9d5117c0e6769fb4d945c2d3f
7
- data.tar.gz: 4c0e36266c7381930baf325aaa9ab944dbfe09d27f65243dfb8968e9702ac53cb3a2b037988a384204062e901215316c58f6aad0580d3884f12ac5948a1644c7
6
+ metadata.gz: cd4b7ea3b2b5a6a5cc5bd6a71668b9735b9075d6a8273054e20dccb073b4a1a0a4b8caaa2d0d16b609f178ea6bbf17498ef46f04586b1585b942f26a0a000610
7
+ data.tar.gz: b6b389e1bcfc1281988f3d45883962340cdfc6b18015a218c591ad5c300c7c33f4f8158a74c7934822579430b22127baee7897ee3a55fc0222237e706d5c5f0e
@@ -7,11 +7,11 @@ require 'net/https'
7
7
  # checking of responses.
8
8
  module Sunstone
9
9
  class Connection
10
-
10
+
11
11
  # Set the User-Agent of the client. Will be joined with other User-Agent info
12
12
  attr_writer :user_agent
13
13
  attr_accessor :api_key, :host, :port, :use_ssl
14
-
14
+
15
15
  # Initialize a connection a Sunstone API server.
16
16
  #
17
17
  # Options:
@@ -30,15 +30,15 @@ module Sunstone
30
30
  config[:port] ||= uri.port
31
31
  config[:use_ssl] ||= (uri.scheme == 'https')
32
32
  end
33
-
33
+
34
34
  [:api_key, :host, :port, :use_ssl, :user_agent].each do |key|
35
35
  self.send(:"#{key}=", config[key])
36
36
  end
37
-
37
+
38
38
  @connection = Net::HTTP.new(host, port)
39
39
  @connection.use_ssl = use_ssl
40
40
  end
41
-
41
+
42
42
  # Ping the Sunstone. If everything is configured and operating correctly
43
43
  # <tt>"pong"</tt> will be returned. Otherwise and Sunstone::Exception should be
44
44
  # thrown.
@@ -61,7 +61,7 @@ module Sunstone
61
61
  RUBY_PLATFORM
62
62
  ].compact.join(' ')
63
63
  end
64
-
64
+
65
65
  # Sends a Net::HTTPRequest to the server. The headers returned from
66
66
  # Sunestone#headers are automatically added to the request. The appropriate
67
67
  # error is raised if the response is not in the 200..299 range.
@@ -69,7 +69,7 @@ module Sunstone
69
69
  # Paramaters::
70
70
  #
71
71
  # * +request+ - A Net::HTTPRequest to send to the server
72
- # * +body+ - Optional, a String, IO Object, or a Ruby object which is
72
+ # * +body+ - Optional, a String, IO Object, or a Ruby object which is
73
73
  # converted into JSON and sent as the body
74
74
  # * +block+ - An optional block to call with the +Net::HTTPResponse+ object.
75
75
  #
@@ -100,11 +100,11 @@ module Sunstone
100
100
  def send_request(request, body=nil, &block)
101
101
  request_uri = "http#{use_ssl ? 's' : ''}://#{host}#{port != 80 ? (port == 443 && use_ssl ? '' : ":#{port}") : ''}#{request.path}"
102
102
  request_headers.each { |k, v| request[k] = v }
103
-
103
+
104
104
  if Thread.current[:sunstone_cookie_store]
105
105
  request['Cookie'] = Thread.current[:sunstone_cookie_store].cookie_header_for(request_uri)
106
106
  end
107
-
107
+
108
108
  if body.is_a?(IO)
109
109
  request['Transfer-Encoding'] = 'chunked'
110
110
  request.body_stream = body
@@ -128,14 +128,14 @@ module Sunstone
128
128
  Thread.current[:sunstone_cookie_store].set_cookie(request_uri, value)
129
129
  end
130
130
  end
131
-
131
+
132
132
  if block_given?
133
133
  return_value =yield(response)
134
134
  else
135
135
  return_value =response
136
136
  end
137
137
  end
138
-
138
+
139
139
  return_value
140
140
  end
141
141
 
@@ -170,10 +170,10 @@ module Sunstone
170
170
  def get(path, params='', &block)
171
171
  params ||= ''
172
172
  request = Net::HTTP::Get.new(path + '?' + params.to_param)
173
-
173
+
174
174
  send_request(request, nil, &block)
175
175
  end
176
-
176
+
177
177
  # Send a POST request to +path+ on the Sunstone Server via +Sunstone#send_request+.
178
178
  # See +Sunstone#send_request+ for more details on how the response is handled.
179
179
  #
@@ -205,10 +205,10 @@ module Sunstone
205
205
  # end
206
206
  def post(path, body=nil, &block)
207
207
  request = Net::HTTP::Post.new(path)
208
-
208
+
209
209
  send_request(request, body, &block)
210
210
  end
211
-
211
+
212
212
  # Send a PUT request to +path+ on the Sunstone Server via +Sunstone#send_request+.
213
213
  # See +Sunstone#send_request+ for more details on how the response is handled.
214
214
  #
@@ -240,10 +240,10 @@ module Sunstone
240
240
  # end
241
241
  def put(path, body=nil, *valid_response_codes, &block)
242
242
  request = Net::HTTP::Put.new(path)
243
-
243
+
244
244
  send_request(request, body, &block)
245
245
  end
246
-
246
+
247
247
  # Send a DELETE request to +path+ on the Sunstone Server via +Sunstone#send_request+.
248
248
  # See +Sunstone#send_request+ for more details on how the response is handled
249
249
  #
@@ -268,25 +268,25 @@ module Sunstone
268
268
  # end
269
269
  def delete(path, &block)
270
270
  request = Net::HTTP::Delete.new(path)
271
-
271
+
272
272
  send_request(request, nil, &block)
273
273
  end
274
274
 
275
275
  def server_config
276
276
  @server_config ||= Wankel.parse(get('/config').body, :symbolize_keys => true)
277
277
  end
278
-
278
+
279
279
  private
280
-
280
+
281
281
  def request_headers
282
282
  headers = {
283
283
  'Content-Type' => 'application/json',
284
284
  'User-Agent' => user_agent,
285
285
  'Api-Version' => '0.1.0'
286
286
  }
287
-
288
- headers['Api-Key'] = api_key if api_key
289
-
287
+ request_api_key = Thread.current[:sunstone_api_key] || api_key
288
+
289
+ headers['Api-Key'] = request_api_key if request_api_key
290
290
  headers
291
291
  end
292
292
 
@@ -311,7 +311,7 @@ module Sunstone
311
311
  # Sunstone.validate_response_code(<Net::HTTP::Response @code=500>) # => raises Sunstone::Exception
312
312
  def validate_response_code(response)
313
313
  code = response.code.to_i
314
-
314
+
315
315
  if !(200..299).include?(code)
316
316
  case code
317
317
  when 400
@@ -335,6 +335,6 @@ module Sunstone
335
335
  end
336
336
  end
337
337
  end
338
-
338
+
339
339
  end
340
340
  end
data/sunstone.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "sunstone"
3
- s.version = '1.6.4'
3
+ s.version = '1.6.5'
4
4
  s.authors = ["Jon Bracy"]
5
5
  s.email = ["jonbracy@gmail.com"]
6
6
  s.homepage = "http://sunstonerb.com"
@@ -5,49 +5,49 @@ class SunstoneTest < Minitest::Test
5
5
  def setup
6
6
  Sunstone.site = "http://test_api_key@testhost.com"
7
7
  end
8
-
8
+
9
9
  # Sunstone.site= ============================================================
10
-
10
+
11
11
  test "setting the site sets the api_key" do
12
12
  Sunstone.site = 'https://my_api_key@localhost'
13
13
  assert_equal('my_api_key', Sunstone.api_key)
14
14
  end
15
-
15
+
16
16
  test "setting the site sets the host" do
17
17
  Sunstone.site = 'https://my_api_key@example.com'
18
18
  assert_equal('example.com', Sunstone.host)
19
19
  end
20
-
20
+
21
21
  test "setting the site sets the port" do
22
22
  Sunstone.site = 'http://my_api_key@example.com'
23
23
  assert_equal(80, Sunstone.port)
24
-
24
+
25
25
  Sunstone.site = 'https://my_api_key@example.com'
26
26
  assert_equal(443, Sunstone.port)
27
-
27
+
28
28
  Sunstone.site = 'https://my_api_key@example.com:4321'
29
29
  assert_equal(4321, Sunstone.port)
30
30
  end
31
-
31
+
32
32
  test "setting the site sets the use_ssl option" do
33
33
  Sunstone.site = 'http://my_api_key@example.com'
34
34
  assert_equal(false, Sunstone.use_ssl)
35
-
35
+
36
36
  Sunstone.site = 'https://my_api_key@example.com'
37
37
  assert_equal(true, Sunstone.use_ssl)
38
38
  end
39
-
39
+
40
40
  # Sunstone.user_agent= ======================================================
41
41
  test "setting the user_agent appends it to the User-Agent" do
42
42
  assert_equal("Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", Sunstone.user_agent)
43
-
43
+
44
44
  Sunstone.user_agent = "MyGem/3.14"
45
45
  assert_equal("MyGem/3.14 Sunstone/#{Sunstone::VERSION} Ruby/#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} #{RUBY_PLATFORM}", Sunstone.user_agent)
46
46
  Sunstone.user_agent = nil
47
47
  end
48
-
48
+
49
49
  # Sunstone.with_cookie_store ================================================
50
-
50
+
51
51
  test '#with_cookie_store(store, &block) sets the cookie-store' do
52
52
  assert_nil Thread.current[:sunstone_cookie_store]
53
53
  Sunstone.with_cookie_store('my_store') do
@@ -56,20 +56,30 @@ class SunstoneTest < Minitest::Test
56
56
  assert_nil Thread.current[:sunstone_cookie_store]
57
57
  end
58
58
 
59
+ # Sunstone.with_api_key ================================================
60
+
61
+ test '#with_api_key(key, &block) sets the Api-Key' do
62
+ assert_nil Thread.current[:sunstone_api_key]
63
+ Sunstone.with_api_key('my_api_key') do
64
+ assert_equal 'my_api_key', Thread.current[:sunstone_api_key]
65
+ end
66
+ assert_nil Thread.current[:sunstone_api_key]
67
+ end
68
+
59
69
  # Sunstone.send_request =====================================================
60
-
70
+
61
71
  test '#send_request(#<Net::HTTPRequest>)' do
62
72
  stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
63
-
73
+
64
74
  assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
65
75
  end
66
-
76
+
67
77
  test '#send_request(#<Net::HTTPRequest>, body) with string body' do
68
78
  stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
69
-
79
+
70
80
  assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), '{"key":"value"}').body)
71
81
  end
72
-
82
+
73
83
  test '#send_request(#<Net::HTTPRequest>, body) with IO body' do
74
84
  stub_request(:post, "http://testhost.com/test").with { |request|
75
85
  request.headers['Transfer-Encoding'] == "chunked" && request.body == '{"key":"value"}'
@@ -81,14 +91,14 @@ class SunstoneTest < Minitest::Test
81
91
 
82
92
  assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), rd).body)
83
93
  end
84
-
94
+
85
95
  test '#send_request(#<Net::HTTPRequest>, body) with Ruby Object body' do
86
96
  stub_request(:post, "http://testhost.com/test").with(:body => '{"key":"value"}').to_return(:body => "post")
87
-
97
+
88
98
  assert_equal('post', Sunstone.send_request(Net::HTTP::Post.new('/test'), {:key => 'value'}).body)
89
99
  end
90
100
 
91
- test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
101
+ test '#send_request(#<Net::HTTPRequest>) raises Sunstone::Exceptions on non-200 responses' do
92
102
  stub_request(:get, "http://testhost.com/400").to_return(:status => 400)
93
103
  stub_request(:get, "http://testhost.com/401").to_return(:status => 401)
94
104
  stub_request(:get, "http://testhost.com/404").to_return(:status => 404)
@@ -97,7 +107,7 @@ class SunstoneTest < Minitest::Test
97
107
  stub_request(:get, "http://testhost.com/450").to_return(:status => 450)
98
108
  stub_request(:get, "http://testhost.com/503").to_return(:status => 503)
99
109
  stub_request(:get, "http://testhost.com/550").to_return(:status => 550)
100
-
110
+
101
111
  assert_raises(Sunstone::Exception::BadRequest) { Sunstone.send_request(Net::HTTP::Get.new('/400')) }
102
112
  assert_raises(Sunstone::Exception::Unauthorized) { Sunstone.send_request(Net::HTTP::Get.new('/401')) }
103
113
  assert_raises(Sunstone::Exception::NotFound) { Sunstone.send_request(Net::HTTP::Get.new('/404')) }
@@ -107,27 +117,27 @@ class SunstoneTest < Minitest::Test
107
117
  assert_raises(Sunstone::Exception::ServiceUnavailable) { Sunstone.send_request(Net::HTTP::Get.new('/503')) }
108
118
  assert_raises(Sunstone::Exception) { Sunstone.send_request(Net::HTTP::Get.new('/550')) }
109
119
  end
110
-
120
+
111
121
  test '#send_request(#<Net::HTTPRequest>, &block) returns value returned from &block' do
112
122
  stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
113
-
123
+
114
124
  value = Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
115
125
  3215
116
126
  end
117
-
127
+
118
128
  assert_equal 3215, value
119
129
  end
120
-
130
+
121
131
  test '#send_request(#<Net::HTTPRequest>, &block)' do
122
132
  stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
123
-
133
+
124
134
  Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
125
135
  assert_equal 'get', response.body
126
136
  end
127
137
 
128
138
  # make sure block is not called when not in valid_response_codes
129
139
  stub_request(:get, "http://testhost.com/test").to_return(:status => 401, :body => 'get')
130
-
140
+
131
141
  assert_raises(Sunstone::Exception::Unauthorized) {
132
142
  Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
133
143
  raise Sunstone::Exception, 'Should not get here'
@@ -141,7 +151,7 @@ class SunstoneTest < Minitest::Test
141
151
  wr.write(<<-DATA.gsub(/^ +/, '').gsub(/\n/, "\r\n"))
142
152
  HTTP/1.1 200 OK
143
153
  Content-Length: 5
144
-
154
+
145
155
  hello
146
156
  DATA
147
157
 
@@ -149,7 +159,7 @@ class SunstoneTest < Minitest::Test
149
159
  connection = mock('connection')
150
160
  connection.stubs(:request).yields(res)
151
161
  Sunstone.stubs(:with_connection).yields(connection)
152
-
162
+
153
163
  res.reading_body(rd, true) do
154
164
  Sunstone.send_request(Net::HTTP::Get.new('/test')) do |response|
155
165
  response.read_body do |chunk|
@@ -158,21 +168,21 @@ class SunstoneTest < Minitest::Test
158
168
  end
159
169
  end
160
170
  end
161
-
171
+
162
172
  test '#send_request(#<Net::HTTPRequest) adds cookies to the cookie store if present' do
163
173
  store = CookieStore::HashStore.new
164
174
  stub_request(:get, "http://testhost.com/test").to_return(:body => 'get', :headers => {'Set-Cookie' => 'foo=bar; Max-Age=3600'})
165
-
175
+
166
176
  Sunstone.with_cookie_store(store) do
167
177
  Sunstone.send_request(Net::HTTP::Get.new('/test'))
168
178
  end
169
-
179
+
170
180
  assert_equal 1, store.instance_variable_get(:@domains).size
171
181
  assert_equal 1, store.instance_variable_get(:@domains)['testhost.com'].size
172
182
  assert_equal 1, store.instance_variable_get(:@domains)['testhost.com']['/test'].size
173
183
  assert_equal 'bar', store.instance_variable_get(:@domains)['testhost.com']['/test']['foo'].value
174
184
  end
175
-
185
+
176
186
  test '#send_request(#<Net::HTTPRequest>) includes the headers' do
177
187
  stub_request(:get, "http://testhost.com/test").with(:headers => {
178
188
  'Api-Key' => 'test_api_key',
@@ -181,7 +191,7 @@ class SunstoneTest < Minitest::Test
181
191
  }).to_return(:body => "get")
182
192
 
183
193
  assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
184
-
194
+
185
195
  # Test without api key
186
196
  Sunstone.site = "http://testhost.com"
187
197
  stub_request(:get, "http://testhost.com/test").with(:headers => {
@@ -191,12 +201,12 @@ class SunstoneTest < Minitest::Test
191
201
 
192
202
  assert_equal('get', Sunstone.send_request(Net::HTTP::Get.new('/test')).body)
193
203
  end
194
-
204
+
195
205
  # Sunstone.get ==============================================================
196
-
206
+
197
207
  test '#get(path)' do
198
208
  stub_request(:get, "http://testhost.com/test").to_return(:body => "get")
199
-
209
+
200
210
  assert_equal('get', Sunstone.get('/test').body)
201
211
  end
202
212
 
@@ -205,16 +215,16 @@ class SunstoneTest < Minitest::Test
205
215
 
206
216
  assert_equal 'get', Sunstone.get('/test', 'key=value').body
207
217
  end
208
-
218
+
209
219
  test '#get(path, params) with params as hash' do
210
220
  stub_request(:get, "http://testhost.com/test").with(:query => {'key' => 'value'}).to_return(:body => "get")
211
221
 
212
222
  assert_equal 'get', Sunstone.get('/test', {:key => 'value'}).body
213
223
  end
214
-
224
+
215
225
  test '#get(path, &block)' do
216
226
  stub_request(:get, "http://testhost.com/test").to_return(:body => 'get')
217
-
227
+
218
228
  Sunstone.get('/test') do |response|
219
229
  assert_equal 'get', response.body
220
230
  end
@@ -224,41 +234,41 @@ class SunstoneTest < Minitest::Test
224
234
 
225
235
  test '#post(path)' do
226
236
  stub_request(:post, "http://testhost.com/test").to_return(:body => "post")
227
-
237
+
228
238
  assert_equal('post', Sunstone.post('/test').body)
229
239
  end
230
-
240
+
231
241
  test '#post(path, body)' do
232
242
  stub_request(:post, "http://testhost.com/test").with(:body => 'body').to_return(:body => "post")
233
-
243
+
234
244
  assert_equal('post', Sunstone.post('/test', 'body').body)
235
245
  end
236
-
246
+
237
247
  test '#post(path, &block)' do
238
248
  stub_request(:post, "http://testhost.com/test").to_return(:body => 'post')
239
-
249
+
240
250
  Sunstone.post('/test') do |response|
241
251
  assert_equal 'post', response.body
242
252
  end
243
253
  end
244
-
254
+
245
255
  # Sunstone.put ==============================================================
246
256
 
247
257
  test '#put(path)' do
248
258
  stub_request(:put, "http://testhost.com/test").to_return(:body => "put")
249
-
259
+
250
260
  assert_equal('put', Sunstone.put('/test').body)
251
261
  end
252
-
262
+
253
263
  test '#put(path, body)' do
254
264
  stub_request(:put, "http://testhost.com/test").with(:body => 'body').to_return(:body => "put")
255
-
265
+
256
266
  assert_equal('put', Sunstone.put('/test', 'body').body)
257
267
  end
258
-
268
+
259
269
  test '#put(path, &block)' do
260
270
  stub_request(:put, "http://testhost.com/test").to_return(:body => 'put')
261
-
271
+
262
272
  Sunstone.put('/test') do |response|
263
273
  assert_equal 'put', response.body
264
274
  end
@@ -268,35 +278,35 @@ class SunstoneTest < Minitest::Test
268
278
 
269
279
  test '#delete' do
270
280
  stub_request(:delete, "http://testhost.com/test").to_return(:body => "delete")
271
-
281
+
272
282
  assert_equal('delete', Sunstone.delete('/test').body)
273
283
  end
274
-
284
+
275
285
  test '#delete(path, &block)' do
276
286
  stub_request(:delete, "http://testhost.com/test").to_return(:body => 'delete')
277
-
287
+
278
288
  Sunstone.delete('/test') do |response|
279
289
  assert_equal 'delete', response.body
280
290
  end
281
291
  end
282
-
292
+
283
293
  # Sunstone.ping =============================================================
284
-
294
+
285
295
  test '#ping' do
286
296
  stub_request(:get, "http://testhost.com/ping").to_return(:body => 'pong')
287
-
297
+
288
298
  assert_equal( 'pong', Sunstone.ping )
289
299
  end
290
-
300
+
291
301
  # Sunstone.config ===========================================================
292
-
302
+
293
303
  test '#config' do
294
304
  stub_request(:get, "http://testhost.com/config").to_return(:body => '{"server": "configs"}')
295
-
305
+
296
306
  assert_equal( {:server => "configs"}, Sunstone.config )
297
307
  end
298
308
 
299
309
 
300
310
 
301
311
 
302
- end
312
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunstone
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.4
4
+ version: 1.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-03 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake