web_client 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -4
- data/Gemfile +4 -4
- data/README.md +60 -30
- data/Rakefile +1 -1
- data/lib/web_client/connection.rb +74 -0
- data/lib/web_client/error.rb +16 -16
- data/lib/web_client/extensions/string.rb +15 -0
- data/lib/web_client/request.rb +36 -0
- data/lib/web_client/response.rb +33 -0
- data/lib/web_client/version.rb +3 -3
- data/lib/web_client.rb +22 -22
- data/spec/connection_spec.rb +164 -0
- data/spec/http_response_factory.rb +19 -0
- data/spec/request_spec.rb +72 -0
- data/spec/response_spec.rb +25 -0
- data/spec/spec_helper.rb +5 -4
- data/spec/string_spec.rb +17 -0
- data/web_client.gemspec +20 -22
- metadata +24 -18
- data/lib/web_client/base.rb +0 -44
- data/spec/base_spec.rb +0 -130
data/.gitignore
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
*.gem
|
2
|
-
.bundle
|
3
|
-
Gemfile.lock
|
4
|
-
pkg/*
|
1
|
+
*.gem
|
2
|
+
.bundle
|
3
|
+
Gemfile.lock
|
4
|
+
pkg/*
|
5
5
|
.idea
|
data/Gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source "http://rubygems.org"
|
2
|
-
|
3
|
-
# Specify your gem's dependencies in web_client.gemspec
|
4
|
-
gemspec
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in web_client.gemspec
|
4
|
+
gemspec
|
data/README.md
CHANGED
@@ -1,30 +1,60 @@
|
|
1
|
-
# WebClient
|
2
|
-
|
3
|
-
Net::HTTP wrapper easy to use
|
4
|
-
|
5
|
-
## Installation
|
6
|
-
|
7
|
-
Add this line to your application's Gemfile:
|
8
|
-
|
9
|
-
gem 'web_client'
|
10
|
-
|
11
|
-
And then execute:
|
12
|
-
|
13
|
-
$ bundle
|
14
|
-
|
15
|
-
Or install it yourself as:
|
16
|
-
|
17
|
-
$ gem install web_client
|
18
|
-
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
1
|
+
# WebClient
|
2
|
+
|
3
|
+
Net::HTTP wrapper easy to use
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'web_client'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install web_client
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Connection initialization
|
22
|
+
|
23
|
+
connection = WebClient::Connection.new 'localhost', 3000
|
24
|
+
#or
|
25
|
+
connection = WebClient::Connection.new host: 'localhost', port: 3000
|
26
|
+
|
27
|
+
### Request types
|
28
|
+
|
29
|
+
connection.get '/users'
|
30
|
+
connection.get '/users/123', headers: {content_type: 'application/json'}
|
31
|
+
connection.post '/users', body: '...'
|
32
|
+
connection.put '/users/123', body: '...'
|
33
|
+
connection.delete '/users/123'
|
34
|
+
|
35
|
+
### Raising request exceptions
|
36
|
+
|
37
|
+
connection.get url #=> nil
|
38
|
+
connection.get! url #=> raise WebClient::Error
|
39
|
+
|
40
|
+
### Response info
|
41
|
+
|
42
|
+
response = connection.get url
|
43
|
+
response.code #=> 200
|
44
|
+
response.body #=> '....'
|
45
|
+
response.success? #=> true
|
46
|
+
response.type #=> Net::HTTPOK
|
47
|
+
|
48
|
+
### Success block
|
49
|
+
|
50
|
+
json = connection.get url do |response|
|
51
|
+
JSON.parse response.body
|
52
|
+
end
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module WebClient
|
2
|
+
class Connection
|
3
|
+
|
4
|
+
include Net
|
5
|
+
|
6
|
+
def initialize(*args)
|
7
|
+
if args.first.is_a? Hash
|
8
|
+
options = args[0].inject({}) { |h, i| h[i[0].to_sym] = i[1]; h }
|
9
|
+
host = options[:host]
|
10
|
+
port = options[:port]
|
11
|
+
else
|
12
|
+
host = args[0]
|
13
|
+
port = args[1]
|
14
|
+
end
|
15
|
+
@http = HTTP.new(host, port)
|
16
|
+
end
|
17
|
+
|
18
|
+
def host
|
19
|
+
@http.address
|
20
|
+
end
|
21
|
+
|
22
|
+
def port
|
23
|
+
@http.port
|
24
|
+
end
|
25
|
+
|
26
|
+
def request!(request)
|
27
|
+
begin
|
28
|
+
WebClient.logger.debug "[WebClient] #{request.type.to_s.upcase} Url: http://#{host}#{(port != 80) ? ":#{port}" : ''}#{request.url} | Body: #{request.body} | Headers: #{request.headers}"
|
29
|
+
response = Response.new @http.request(request.to_http)
|
30
|
+
WebClient.logger.debug "[WebClient] RESPONSE Status: #{response.code} | Content: #{response.body}"
|
31
|
+
if block_given?
|
32
|
+
if response.success?
|
33
|
+
yield(response)
|
34
|
+
else
|
35
|
+
WebClient.logger.error "[WebClient] #{response.code} - Unexpected error\n#{response.body}"
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
else
|
39
|
+
response
|
40
|
+
end
|
41
|
+
rescue Timeout::Error,
|
42
|
+
Errno::EINVAL,
|
43
|
+
Errno::ECONNRESET,
|
44
|
+
EOFError,
|
45
|
+
HTTPBadResponse,
|
46
|
+
HTTPHeaderSyntaxError,
|
47
|
+
ProtocolError,
|
48
|
+
SocketError,
|
49
|
+
Errno::ECONNREFUSED => e
|
50
|
+
WebClient.logger.error "[WebClient] #{e.class}: #{e.message}"
|
51
|
+
raise Error, e
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def request(request, &block)
|
56
|
+
begin
|
57
|
+
request!(request, &block)
|
58
|
+
rescue WebClient::Error => e
|
59
|
+
nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
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))
|
66
|
+
end
|
67
|
+
|
68
|
+
define_method request_type do |url, options={}, &block|
|
69
|
+
request Request.new(options.merge(type: request_type, url: url)), &block
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/lib/web_client/error.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
module WebClient
|
2
|
-
|
3
|
-
class Error < StandardError
|
4
|
-
def initialize(inner_error)
|
5
|
-
@inner_error = inner_error
|
6
|
-
end
|
7
|
-
|
8
|
-
def type
|
9
|
-
@inner_error.class
|
10
|
-
end
|
11
|
-
|
12
|
-
def message
|
13
|
-
@inner_error.message
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
1
|
+
module WebClient
|
2
|
+
|
3
|
+
class Error < StandardError
|
4
|
+
def initialize(inner_error)
|
5
|
+
@inner_error = inner_error
|
6
|
+
end
|
7
|
+
|
8
|
+
def type
|
9
|
+
@inner_error.class
|
10
|
+
end
|
11
|
+
|
12
|
+
def message
|
13
|
+
@inner_error.message
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
17
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module WebClient
|
2
|
+
class Request
|
3
|
+
|
4
|
+
TYPES = [:get, :post, :put, :delete]
|
5
|
+
|
6
|
+
attr_accessor :type
|
7
|
+
attr_accessor :url
|
8
|
+
attr_accessor :body
|
9
|
+
attr_accessor :headers
|
10
|
+
|
11
|
+
def initialize(options={}, &block)
|
12
|
+
@type = options[:type] || :get
|
13
|
+
@url = options[:url]
|
14
|
+
@body = options[:body]
|
15
|
+
@headers = options[:headers] || {}
|
16
|
+
|
17
|
+
block.call(self) if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_http
|
21
|
+
klass = eval("Net::HTTP::#{type.to_s.titleize}")
|
22
|
+
request = klass.new url
|
23
|
+
request.set_form_data(body) if body.is_a? Hash
|
24
|
+
request.body = body if body.is_a? String
|
25
|
+
headers.each { |k, v| request.send("#{k}=", v) }
|
26
|
+
request
|
27
|
+
end
|
28
|
+
|
29
|
+
TYPES.each do |request_type|
|
30
|
+
define_singleton_method request_type do |options, &block|
|
31
|
+
new options.merge(type: request_type, &block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module WebClient
|
2
|
+
class Response
|
3
|
+
|
4
|
+
def initialize(response)
|
5
|
+
@response = response
|
6
|
+
end
|
7
|
+
|
8
|
+
def type
|
9
|
+
@response.class
|
10
|
+
end
|
11
|
+
|
12
|
+
def success?
|
13
|
+
@response.is_a? Net::HTTPSuccess
|
14
|
+
end
|
15
|
+
|
16
|
+
def method_missing(method, *args, &block)
|
17
|
+
if @response.respond_to? method
|
18
|
+
@response.send method, *args, &block
|
19
|
+
else
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def methods
|
25
|
+
(super | @response.methods).uniq
|
26
|
+
end
|
27
|
+
|
28
|
+
def respond_to?(method)
|
29
|
+
super || @response.respond_to?(method)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/web_client/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module WebClient
|
2
|
-
VERSION = '0.0.
|
3
|
-
end
|
1
|
+
module WebClient
|
2
|
+
VERSION = '0.0.4'
|
3
|
+
end
|
data/lib/web_client.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
require 'net/http'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
require 'web_client/
|
7
|
-
require 'web_client/
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
|
1
|
+
require 'net/http'
|
2
|
+
require 'logger'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
require 'web_client/version'
|
6
|
+
require 'web_client/extensions/string'
|
7
|
+
require 'web_client/request'
|
8
|
+
require 'web_client/response'
|
9
|
+
require 'web_client/connection'
|
10
|
+
require 'web_client/error'
|
11
|
+
|
12
|
+
module WebClient
|
13
|
+
|
14
|
+
def self.logger
|
15
|
+
@@logger ||= defined?(Rails) ? Rails.logger : Logger.new($stdout)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.logger=(logger)
|
19
|
+
@@logger = logger
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,164 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WebClient::Connection do
|
4
|
+
|
5
|
+
let(:connection) { WebClient::Connection.new(HOST) }
|
6
|
+
|
7
|
+
context 'Constructor' do
|
8
|
+
|
9
|
+
it 'with host' do
|
10
|
+
connection = WebClient::Connection.new 'localhost'
|
11
|
+
|
12
|
+
connection.host.should eq 'localhost'
|
13
|
+
connection.port.should eq 80
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'with host and port' do
|
17
|
+
connection = WebClient::Connection.new 'localhost', 1234
|
18
|
+
|
19
|
+
connection.host.should eq 'localhost'
|
20
|
+
connection.port.should eq 1234
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'with option host' do
|
24
|
+
connection = WebClient::Connection.new host: 'localhost'
|
25
|
+
|
26
|
+
connection.host.should eq 'localhost'
|
27
|
+
connection.port.should eq 80
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'with options host and port' do
|
31
|
+
connection = WebClient::Connection.new host: 'localhost', port: 1234
|
32
|
+
|
33
|
+
connection.host.should eq 'localhost'
|
34
|
+
connection.port.should eq 1234
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'Request type helpers' do
|
40
|
+
|
41
|
+
it 'get' do
|
42
|
+
stub_request(:get, "#{HOST}/get_stub").to_return(body: 'content')
|
43
|
+
response = connection.get('/get_stub')
|
44
|
+
|
45
|
+
response.type.should be Net::HTTPOK
|
46
|
+
response.body.should eq 'content'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'post' do
|
50
|
+
stub_request(:post, "#{HOST}/post_stub").with(body: 'form_data')
|
51
|
+
response = connection.post('/post_stub', body: 'form_data')
|
52
|
+
|
53
|
+
response.type.should be Net::HTTPOK
|
54
|
+
response.body.should be_nil
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'put' do
|
58
|
+
stub_request(:put, "#{HOST}/put_stub").with(body: 'form_data')
|
59
|
+
response = connection.put('/put_stub', body: 'form_data')
|
60
|
+
|
61
|
+
response.type.should be Net::HTTPOK
|
62
|
+
response.body.should be_nil
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'delete' do
|
66
|
+
stub_request(:delete, "#{HOST}/delete_stub").with(body: 'form_data')
|
67
|
+
response = connection.delete('/delete_stub', body: 'form_data')
|
68
|
+
|
69
|
+
response.type.should be Net::HTTPOK
|
70
|
+
response.body.should be_nil
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'Actions with headers' do
|
76
|
+
|
77
|
+
it 'get' do
|
78
|
+
stub_request(:get, "#{HOST}/get_stub").
|
79
|
+
with(headers: {content_type: 'application/json'}).
|
80
|
+
to_return(body: 'content')
|
81
|
+
response = connection.get('/get_stub', headers: {content_type: 'application/json'})
|
82
|
+
|
83
|
+
response.type.should be Net::HTTPOK
|
84
|
+
response.body.should eq 'content'
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'post' do
|
88
|
+
stub_request(:post, "#{HOST}/post_stub").
|
89
|
+
with(headers: {content_type: 'application/json'}).
|
90
|
+
with(body: 'form_data')
|
91
|
+
response = connection.post('/post_stub', body: 'form_data', headers: {content_type: 'application/json'})
|
92
|
+
|
93
|
+
response.type.should be Net::HTTPOK
|
94
|
+
response.body.should be_nil
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'put' do
|
98
|
+
stub_request(:put, "#{HOST}/put_stub").
|
99
|
+
with(headers: {content_type: 'application/json'}).
|
100
|
+
with(body: 'form_data')
|
101
|
+
response = connection.put('/put_stub', body: 'form_data', headers: {content_type: 'application/json'})
|
102
|
+
|
103
|
+
response.type.should be Net::HTTPOK
|
104
|
+
response.body.should be_nil
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'delete' do
|
108
|
+
stub_request(:delete, "#{HOST}/delete_stub").
|
109
|
+
with(headers: {content_type: 'application/json'}).
|
110
|
+
with(body: 'form_data')
|
111
|
+
response = connection.delete('/delete_stub', body: 'form_data', headers: {content_type: 'application/json'})
|
112
|
+
|
113
|
+
response.type.should be Net::HTTPOK
|
114
|
+
response.body.should be_nil
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'Errors handling' do
|
120
|
+
|
121
|
+
it 'Invalid host exception' do
|
122
|
+
stub_request(:get, /.*/).to_raise(SocketError.new('getaddrinfo: No such host is known.'))
|
123
|
+
lambda { connection.get! '/' }.should raise_error WebClient::Error
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'Timeout exception' do
|
127
|
+
stub_request(:get, /.*/).to_timeout
|
128
|
+
lambda { connection.get! '/' }.should raise_error WebClient::Error
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
context 'Safe mode' do
|
134
|
+
|
135
|
+
it 'Success response' do
|
136
|
+
stub_request(:get, "#{HOST}/get_stub").to_return(body: '{"id":1,"name":"John"}')
|
137
|
+
json = connection.get('/get_stub') do |response|
|
138
|
+
JSON.parse response.body
|
139
|
+
end
|
140
|
+
|
141
|
+
json.should eq 'id' => 1, 'name' => 'John'
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'Invalid response' do
|
145
|
+
stub_request(:get, "#{HOST}/get_stub").to_return(status: 404)
|
146
|
+
json = connection.get('/get_stub') do |response|
|
147
|
+
JSON.parse response.body
|
148
|
+
end
|
149
|
+
|
150
|
+
json.should be_nil
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'Request error' do
|
154
|
+
stub_request(:get, "#{HOST}/get_stub").to_timeout
|
155
|
+
json = connection.get('/get_stub') do |response|
|
156
|
+
JSON.parse response.body
|
157
|
+
end
|
158
|
+
|
159
|
+
json.should be_nil
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module HttpResponseFactory
|
2
|
+
|
3
|
+
def self.ok(body)
|
4
|
+
response = Net::HTTPOK.new '1.1', '200', 'nil'
|
5
|
+
response.body = body
|
6
|
+
response.content_type = 'text/html'
|
7
|
+
response.instance_variable_set '@read', true
|
8
|
+
response
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.not_found
|
12
|
+
response = Net::HTTPNotFound.new '1.1', '404', 'nil'
|
13
|
+
response.body = nil
|
14
|
+
response.content_type = 'text/html'
|
15
|
+
response.instance_variable_set '@read', true
|
16
|
+
response
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WebClient::Request do
|
4
|
+
|
5
|
+
context 'Initialization' do
|
6
|
+
|
7
|
+
it 'with url' do
|
8
|
+
request = WebClient::Request.new url: '/'
|
9
|
+
|
10
|
+
request.type.should eq :get
|
11
|
+
request.url.should eq '/'
|
12
|
+
request.body.should be_nil
|
13
|
+
request.headers.should eq Hash.new
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'with type' do
|
17
|
+
request = WebClient::Request.new url: '/', type: :post
|
18
|
+
|
19
|
+
request.type.should eq :post
|
20
|
+
request.url.should eq '/'
|
21
|
+
request.body.should be_nil
|
22
|
+
request.headers.should eq Hash.new
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'with body' do
|
26
|
+
request = WebClient::Request.new url: '/', body: 'content'
|
27
|
+
|
28
|
+
request.type.should eq :get
|
29
|
+
request.url.should eq '/'
|
30
|
+
request.body.should eq 'content'
|
31
|
+
request.headers.should eq Hash.new
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'with headers' do
|
35
|
+
request = WebClient::Request.new url: '/', headers: {content_type: 'application/json'}
|
36
|
+
|
37
|
+
request.type.should eq :get
|
38
|
+
request.url.should eq '/'
|
39
|
+
request.body.should be_nil
|
40
|
+
request.headers.should eq content_type: 'application/json'
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'with block' do
|
44
|
+
request = WebClient::Request.new do |r|
|
45
|
+
r.type = :post
|
46
|
+
r.url = '/'
|
47
|
+
end
|
48
|
+
|
49
|
+
request.type.should eq :post
|
50
|
+
request.url.should eq '/'
|
51
|
+
request.body.should be_nil
|
52
|
+
request.headers.should eq Hash.new
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'Constructors helpers' do
|
58
|
+
|
59
|
+
WebClient::Request::TYPES.each do |request_type|
|
60
|
+
it request_type do
|
61
|
+
request = WebClient::Request.send request_type, url: '/'
|
62
|
+
|
63
|
+
request.type.should eq request_type
|
64
|
+
request.url.should eq '/'
|
65
|
+
request.body.should be_nil
|
66
|
+
request.headers.should eq Hash.new
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WebClient::Response do
|
4
|
+
|
5
|
+
it 'Success' do
|
6
|
+
response = WebClient::Response.new HttpResponseFactory.ok('OK')
|
7
|
+
|
8
|
+
response.code.should eq '200'
|
9
|
+
response.body.should eq 'OK'
|
10
|
+
response.content_type.should eq 'text/html'
|
11
|
+
response.type.should be Net::HTTPOK
|
12
|
+
response.success?.should be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'Not found' do
|
16
|
+
response = WebClient::Response.new HttpResponseFactory.not_found
|
17
|
+
|
18
|
+
response.code.should eq '404'
|
19
|
+
response.body.should be_nil
|
20
|
+
response.content_type.should eq 'text/html'
|
21
|
+
response.type.should be Net::HTTPNotFound
|
22
|
+
response.success?.should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/string_spec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
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
|
data/web_client.gemspec
CHANGED
@@ -1,22 +1,20 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "web_client/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = 'web_client'
|
7
|
-
s.version = WebClient::VERSION
|
8
|
-
s.authors = ['Gabriel Naiman']
|
9
|
-
s.email = ['gabynaiman@gmail.com']
|
10
|
-
s.homepage = 'https://github.com/gabynaiman/web_client'
|
11
|
-
s.summary = %q{Net::HTTP wrapper easy to use}
|
12
|
-
s.description = %q{Net::HTTP wrapper easy to use}
|
13
|
-
s.files = `git ls-files`.split("\n")
|
14
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
|
18
|
-
s.
|
19
|
-
|
20
|
-
|
21
|
-
s.add_development_dependency 'webmock'
|
22
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "web_client/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'web_client'
|
7
|
+
s.version = WebClient::VERSION
|
8
|
+
s.authors = ['Gabriel Naiman']
|
9
|
+
s.email = ['gabynaiman@gmail.com']
|
10
|
+
s.homepage = 'https://github.com/gabynaiman/web_client'
|
11
|
+
s.summary = %q{Net::HTTP wrapper easy to use}
|
12
|
+
s.description = %q{Net::HTTP wrapper easy to use}
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
|
18
|
+
s.add_development_dependency 'rspec'
|
19
|
+
s.add_development_dependency 'webmock'
|
20
|
+
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
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,33 +9,27 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
16
|
-
requirement:
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
22
|
-
type: :
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rspec
|
27
|
-
requirement: &25412508 !ruby/object:Gem::Requirement
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
25
|
none: false
|
29
26
|
requirements:
|
30
27
|
- - ! '>='
|
31
28
|
- !ruby/object:Gem::Version
|
32
29
|
version: '0'
|
33
|
-
type: :development
|
34
|
-
prerelease: false
|
35
|
-
version_requirements: *25412508
|
36
30
|
- !ruby/object:Gem::Dependency
|
37
31
|
name: webmock
|
38
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
39
33
|
none: false
|
40
34
|
requirements:
|
41
35
|
- - ! '>='
|
@@ -43,7 +37,12 @@ dependencies:
|
|
43
37
|
version: '0'
|
44
38
|
type: :development
|
45
39
|
prerelease: false
|
46
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
47
46
|
description: Net::HTTP wrapper easy to use
|
48
47
|
email:
|
49
48
|
- gabynaiman@gmail.com
|
@@ -56,11 +55,18 @@ files:
|
|
56
55
|
- README.md
|
57
56
|
- Rakefile
|
58
57
|
- lib/web_client.rb
|
59
|
-
- lib/web_client/
|
58
|
+
- lib/web_client/connection.rb
|
60
59
|
- lib/web_client/error.rb
|
60
|
+
- lib/web_client/extensions/string.rb
|
61
|
+
- lib/web_client/request.rb
|
62
|
+
- lib/web_client/response.rb
|
61
63
|
- lib/web_client/version.rb
|
62
|
-
- spec/
|
64
|
+
- spec/connection_spec.rb
|
65
|
+
- spec/http_response_factory.rb
|
66
|
+
- spec/request_spec.rb
|
67
|
+
- spec/response_spec.rb
|
63
68
|
- spec/spec_helper.rb
|
69
|
+
- spec/string_spec.rb
|
64
70
|
- web_client.gemspec
|
65
71
|
homepage: https://github.com/gabynaiman/web_client
|
66
72
|
licenses: []
|
@@ -82,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
88
|
version: '0'
|
83
89
|
requirements: []
|
84
90
|
rubyforge_project:
|
85
|
-
rubygems_version: 1.8.
|
91
|
+
rubygems_version: 1.8.24
|
86
92
|
signing_key:
|
87
93
|
specification_version: 3
|
88
94
|
summary: Net::HTTP wrapper easy to use
|
data/lib/web_client/base.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
module WebClient
|
2
|
-
class Base
|
3
|
-
include Net
|
4
|
-
|
5
|
-
attr_reader :http
|
6
|
-
|
7
|
-
def initialize(*args)
|
8
|
-
if args.first.is_a? Hash
|
9
|
-
host = args[0]['host'] || args[0][:host]
|
10
|
-
port = args[0]['port'] || args[0][:port]
|
11
|
-
else
|
12
|
-
host = args[0]
|
13
|
-
port = args[1]
|
14
|
-
end
|
15
|
-
@http = HTTP.new(host, port)
|
16
|
-
end
|
17
|
-
|
18
|
-
WebClient::HTTP_METHODS.each do |http_method|
|
19
|
-
define_method http_method.to_s.demodulize.downcase do |path='/', data=nil, headers={}, &block|
|
20
|
-
request(http_method, path, data, headers, &block)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def request(method_class, path='/', data=nil, headers={})
|
27
|
-
begin
|
28
|
-
WebClient.logger.debug "[WebClient] #{method_class.to_s.demodulize.upcase} Url: http://#{@http.address}#{(@http.port != 80) ? ":#{@http.port}" : ''}#{path} | Params: #{data} | Headers: #{headers}"
|
29
|
-
request = method_class.new(path)
|
30
|
-
request.set_form_data(data) if data.is_a? Hash
|
31
|
-
request.body = data if data.is_a? String
|
32
|
-
headers.each { |k, v| request.send("#{k}=", v) }
|
33
|
-
yield(request, http) if block_given?
|
34
|
-
response = http.request(request)
|
35
|
-
WebClient.logger.debug "[WebClient] RESPONSE Status: #{response.code} | Content: #{response.body}"
|
36
|
-
response
|
37
|
-
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, HTTPBadResponse, HTTPHeaderSyntaxError, ProtocolError, SocketError, Errno::ECONNREFUSED => e
|
38
|
-
WebClient.logger.error "[WebClient] #{e.class}: #{e.message}"
|
39
|
-
raise Error, e
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
data/spec/base_spec.rb
DELETED
@@ -1,130 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe WebClient::Base do
|
4
|
-
|
5
|
-
let(:client) { WebClient::Base.new(HOST) }
|
6
|
-
|
7
|
-
context 'HTTP methods' do
|
8
|
-
|
9
|
-
it 'get' do
|
10
|
-
stub_request(:get, "#{HOST}/get_stub").to_return(body: 'content')
|
11
|
-
response = client.get('/get_stub')
|
12
|
-
response.should be_a Net::HTTPOK
|
13
|
-
response.body.should eq 'content'
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'post' do
|
17
|
-
stub_request(:post, "#{HOST}/post_stub").with(body: 'form_data')
|
18
|
-
response = client.post('/post_stub', 'form_data')
|
19
|
-
response.should be_a Net::HTTPOK
|
20
|
-
response.body.should be_nil
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'put' do
|
24
|
-
stub_request(:put, "#{HOST}/put_stub").with(body: 'form_data')
|
25
|
-
response = client.put('/put_stub', 'form_data')
|
26
|
-
response.should be_a Net::HTTPOK
|
27
|
-
response.body.should be_nil
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'delete' do
|
31
|
-
stub_request(:delete, "#{HOST}/delete_stub").with(body: 'form_data')
|
32
|
-
response = client.delete('/delete_stub', 'form_data')
|
33
|
-
response.should be_a Net::HTTPOK
|
34
|
-
response.body.should be_nil
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'Actions with headers' do
|
40
|
-
|
41
|
-
it 'get' do
|
42
|
-
stub_request(:get, "#{HOST}/get_stub").
|
43
|
-
with(headers: {content_type: 'application/json'}).
|
44
|
-
to_return(body: 'content')
|
45
|
-
response = client.get('/get_stub', nil, content_type: 'application/json')
|
46
|
-
response.should be_a Net::HTTPOK
|
47
|
-
response.body.should eq 'content'
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'post' do
|
51
|
-
stub_request(:post, "#{HOST}/post_stub").
|
52
|
-
with(headers: {content_type: 'application/json'}).
|
53
|
-
with(body: 'form_data')
|
54
|
-
response = client.post('/post_stub', 'form_data', content_type: 'application/json')
|
55
|
-
response.should be_a Net::HTTPOK
|
56
|
-
response.body.should be_nil
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'put' do
|
60
|
-
stub_request(:put, "#{HOST}/put_stub").
|
61
|
-
with(headers: {content_type: 'application/json'}).
|
62
|
-
with(body: 'form_data')
|
63
|
-
response = client.put('/put_stub', 'form_data', content_type: 'application/json')
|
64
|
-
response.should be_a Net::HTTPOK
|
65
|
-
response.body.should be_nil
|
66
|
-
end
|
67
|
-
|
68
|
-
it 'delete' do
|
69
|
-
stub_request(:delete, "#{HOST}/delete_stub").
|
70
|
-
with(headers: {content_type: 'application/json'}).
|
71
|
-
with(body: 'form_data')
|
72
|
-
response = client.delete('/delete_stub', 'form_data', content_type: 'application/json')
|
73
|
-
response.should be_a Net::HTTPOK
|
74
|
-
response.body.should be_nil
|
75
|
-
end
|
76
|
-
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'Actions with a block' do
|
81
|
-
|
82
|
-
it 'get' do
|
83
|
-
stub_request(:get, /.*/)
|
84
|
-
client.get('/get_stub') do |request, http|
|
85
|
-
request.path.should eq '/get_stub'
|
86
|
-
http.address.should eq HOST
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
it 'post' do
|
91
|
-
stub_request(:post, /.*/)
|
92
|
-
client.post('/post_stub', {}) do |request, http|
|
93
|
-
request.path.should eq '/post_stub'
|
94
|
-
http.address.should eq HOST
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'put' do
|
99
|
-
stub_request(:put, /.*/)
|
100
|
-
client.put('/put_stub', {}) do |request, http|
|
101
|
-
request.path.should eq '/put_stub'
|
102
|
-
http.address.should eq HOST
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'delete' do
|
107
|
-
stub_request(:delete, /.*/)
|
108
|
-
client.delete('/delete_stub') do |request, http|
|
109
|
-
request.path.should eq '/delete_stub'
|
110
|
-
http.address.should eq HOST
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
end
|
115
|
-
|
116
|
-
context 'Errors handling' do
|
117
|
-
|
118
|
-
it 'Invalid host exception' do
|
119
|
-
stub_request(:get, /.*/).to_raise(SocketError.new('getaddrinfo: No such host is known.'))
|
120
|
-
lambda { client.get }.should raise_error WebClient::Error
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'Timeout exception' do
|
124
|
-
stub_request(:get, /.*/).to_timeout
|
125
|
-
lambda { client.get }.should raise_error WebClient::Error
|
126
|
-
end
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
end
|