web_client 0.0.4 → 0.0.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.
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - 2.0
5
+ - jruby
6
+ script: bundle exec rspec spec
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # WebClient
2
2
 
3
+ [![Build Status](https://travis-ci.org/gabynaiman/web_client.png?branch=master)](https://travis-ci.org/gabynaiman/web_client)
4
+
3
5
  Net::HTTP wrapper easy to use
4
6
 
5
7
  ## Installation
@@ -39,6 +39,7 @@ module WebClient
39
39
  response
40
40
  end
41
41
  rescue Timeout::Error,
42
+ Errno::EHOSTUNREACH,
42
43
  Errno::EINVAL,
43
44
  Errno::ECONNRESET,
44
45
  EOFError,
@@ -47,7 +48,7 @@ module WebClient
47
48
  ProtocolError,
48
49
  SocketError,
49
50
  Errno::ECONNREFUSED => e
50
- WebClient.logger.error "[WebClient] #{e.class}: #{e.message}"
51
+ WebClient.logger.error "[WebClient] #{e.class}: #{e.message}\nServer: #{host}:#{port}\nRequest: #{request.to_json}"
51
52
  raise Error, e
52
53
  end
53
54
  end
@@ -61,8 +62,8 @@ module WebClient
61
62
  end
62
63
 
63
64
  Request::TYPES.each do |request_type|
64
- define_method "#{request_type}!" do |url, options={}|
65
- request! Request.new(options.merge(type: request_type, url: url))
65
+ define_method "#{request_type}!" do |url, options={}, &block|
66
+ request! Request.new(options.merge(type: request_type, url: url)), &block
66
67
  end
67
68
 
68
69
  define_method request_type do |url, options={}, &block|
@@ -10,7 +10,11 @@ module WebClient
10
10
  end
11
11
 
12
12
  def message
13
- @inner_error.message
13
+ "#{type}: #{@inner_error.message}"
14
+ end
15
+
16
+ def to_s
17
+ message
14
18
  end
15
19
  end
16
20
 
@@ -18,7 +18,7 @@ module WebClient
18
18
  end
19
19
 
20
20
  def to_http
21
- klass = eval("Net::HTTP::#{type.to_s.titleize}")
21
+ klass = eval("Net::HTTP::#{type.to_s.capitalize}")
22
22
  request = klass.new url
23
23
  request.set_form_data(body) if body.is_a? Hash
24
24
  request.body = body if body.is_a? String
@@ -1,3 +1,3 @@
1
1
  module WebClient
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
data/lib/web_client.rb CHANGED
@@ -3,7 +3,6 @@ require 'logger'
3
3
  require 'json'
4
4
 
5
5
  require 'web_client/version'
6
- require 'web_client/extensions/string'
7
6
  require 'web_client/request'
8
7
  require 'web_client/response'
9
8
  require 'web_client/connection'
@@ -120,12 +120,12 @@ describe WebClient::Connection do
120
120
 
121
121
  it 'Invalid host exception' do
122
122
  stub_request(:get, /.*/).to_raise(SocketError.new('getaddrinfo: No such host is known.'))
123
- lambda { connection.get! '/' }.should raise_error WebClient::Error
123
+ expect { connection.get! '/' }.to raise_error WebClient::Error
124
124
  end
125
125
 
126
126
  it 'Timeout exception' do
127
127
  stub_request(:get, /.*/).to_timeout
128
- lambda { connection.get! '/' }.should raise_error WebClient::Error
128
+ expect { connection.get! '/' }.to raise_error WebClient::Error
129
129
  end
130
130
 
131
131
  end
@@ -161,4 +161,31 @@ describe WebClient::Connection do
161
161
 
162
162
  end
163
163
 
164
+ context 'Unsafe mode' do
165
+
166
+ it 'Success response' do
167
+ stub_request(:get, "#{HOST}/get_stub").to_return(body: '{"id":1,"name":"John"}')
168
+ json = connection.get!('/get_stub') do |response|
169
+ JSON.parse response.body
170
+ end
171
+
172
+ json.should eq 'id' => 1, 'name' => 'John'
173
+ end
174
+
175
+ it 'Invalid response' do
176
+ stub_request(:get, "#{HOST}/get_stub").to_return(status: 404)
177
+ json = connection.get!('/get_stub') do |response|
178
+ JSON.parse response.body
179
+ end
180
+
181
+ json.should be_nil
182
+ end
183
+
184
+ it 'Request error' do
185
+ stub_request(:get, "#{HOST}/get_stub").to_timeout
186
+ expect { connection.get!('/get_stub') { |r| JSON.parse r.body } }.to raise_error WebClient::Error
187
+ end
188
+
189
+ end
190
+
164
191
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-11 00:00:00.000000000 Z
12
+ date: 2013-04-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -51,13 +51,13 @@ extensions: []
51
51
  extra_rdoc_files: []
52
52
  files:
53
53
  - .gitignore
54
+ - .travis.yml
54
55
  - Gemfile
55
56
  - README.md
56
57
  - Rakefile
57
58
  - lib/web_client.rb
58
59
  - lib/web_client/connection.rb
59
60
  - lib/web_client/error.rb
60
- - lib/web_client/extensions/string.rb
61
61
  - lib/web_client/request.rb
62
62
  - lib/web_client/response.rb
63
63
  - lib/web_client/version.rb
@@ -66,7 +66,6 @@ files:
66
66
  - spec/request_spec.rb
67
67
  - spec/response_spec.rb
68
68
  - spec/spec_helper.rb
69
- - spec/string_spec.rb
70
69
  - web_client.gemspec
71
70
  homepage: https://github.com/gabynaiman/web_client
72
71
  licenses: []
@@ -93,3 +92,4 @@ signing_key:
93
92
  specification_version: 3
94
93
  summary: Net::HTTP wrapper easy to use
95
94
  test_files: []
95
+ has_rdoc:
@@ -1,15 +0,0 @@
1
- class String
2
-
3
- def demodulize
4
- if i = self.rindex('::')
5
- self[(i+2)..-1]
6
- else
7
- self
8
- end
9
- end
10
-
11
- def titleize
12
- "#{self.to_s[0].upcase}#{self[1..-1]}"
13
- end
14
-
15
- end
data/spec/string_spec.rb DELETED
@@ -1,17 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe String do
4
-
5
- it 'Titleize single word' do
6
- 'hello'.titleize.should eq 'Hello'
7
- end
8
-
9
- it 'Titleize phrase' do
10
- 'hello world'.titleize.should eq 'Hello world'
11
- end
12
-
13
- it 'Titleize titleized word' do
14
- 'Hello'.titleize.should eq 'Hello'
15
- end
16
-
17
- end