http_wrapper 2.1.0 → 2.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +6 -6
- data/lib/http_wrapper/constants.rb +4 -4
- data/lib/http_wrapper/errors.rb +1 -1
- data/lib/http_wrapper/http_wrapper.rb +16 -19
- data/lib/http_wrapper/request.rb +65 -65
- data/lib/http_wrapper/utils.rb +18 -0
- data/lib/http_wrapper/version.rb +1 -1
- data/lib/http_wrapper.rb +1 -0
- data/spec/http_wrapper_spec.rb +11 -4
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa85c2016386aa01baeab80596dc32c5fe9a8487
|
4
|
+
data.tar.gz: fd6379cb93042cc78a5cfc4efbea1042a7377fbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8209c18ab5f5934bfe4eb8792346d9407706345e0a091a23cedc90dde41e4ef761f3c5f59d41168e5e4470d4fbc224d305ddcf4d09e9583f7dea74044088138c
|
7
|
+
data.tar.gz: b4dd32e4c65b49634447e29773332ea37628099e247cf2cb454d3059c7c1ea207be6f449baded1681b8fb678437a5363daf0817a3f2ef4a69f1296a24a5c7c90
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## v2.1.1
|
4
|
+
|
5
|
+
* code refactoring
|
6
|
+
* fixed `post_and_get_cookie` method (warning: HTTPResponse#response is obsolete)
|
7
|
+
* `UnknownParameterError` renamed to `UnknownKeyError`
|
8
|
+
* removed options and parameters validation code duplication
|
9
|
+
|
3
10
|
## v2.1.0
|
4
11
|
|
5
12
|
* added ability to perform custom `Net::HTTP` requests
|
data/README.md
CHANGED
@@ -3,10 +3,10 @@
|
|
3
3
|
Simple wrapper around standard Net::HTTP library
|
4
4
|
|
5
5
|
[](http://badge.fury.io/rb/http_wrapper)
|
6
|
-
[](https://travis-ci.org/svyatov/http_wrapper)
|
7
|
+
[](https://gemnasium.com/svyatov/http_wrapper)
|
8
|
+
[](https://codeclimate.com/github/svyatov/http_wrapper)
|
9
|
+
[](https://coveralls.io/r/svyatov/http_wrapper)
|
10
10
|
|
11
11
|
---
|
12
12
|
|
@@ -15,7 +15,7 @@ Simple wrapper around standard Net::HTTP library
|
|
15
15
|
Add this line to your Gemfile:
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
gem 'http_wrapper', '~> 2.1.
|
18
|
+
gem 'http_wrapper', '~> 2.1.1'
|
19
19
|
```
|
20
20
|
|
21
21
|
And then execute:
|
@@ -312,7 +312,7 @@ http.execute request, uri
|
|
312
312
|
```
|
313
313
|
|
314
314
|
Don't worry if you mistype root parameters key. `http_wrapper` checks root parameters keys and instantiation options keys.
|
315
|
-
If any unknown options or parameters found, it raises the `
|
315
|
+
If any unknown options or parameters found, it raises the `UnknownKeyError` exception.
|
316
316
|
|
317
317
|
## Contributing
|
318
318
|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
class HTTPWrapper
|
2
2
|
module HEADER
|
3
|
-
CONTENT_TYPE = '
|
4
|
-
USER_AGENT = '
|
5
|
-
COOKIE = '
|
6
|
-
AJAX = '
|
3
|
+
CONTENT_TYPE = 'content-type'.freeze
|
4
|
+
USER_AGENT = 'user-agent'.freeze
|
5
|
+
COOKIE = 'cookie'.freeze
|
6
|
+
AJAX = 'x-requested-with'.freeze
|
7
7
|
end
|
8
8
|
|
9
9
|
module CONTENT_TYPE
|
data/lib/http_wrapper/errors.rb
CHANGED
@@ -6,7 +6,7 @@ class HTTPWrapper
|
|
6
6
|
attr_accessor :timeout, :verify_cert, :logger, :max_redirects, :user_agent
|
7
7
|
|
8
8
|
def initialize(options = {})
|
9
|
-
|
9
|
+
Utils.validate_hash_keys options, KNOWN_OPTIONS_KEYS
|
10
10
|
|
11
11
|
@timeout = options.fetch(:timeout) { 10 }
|
12
12
|
@verify_cert = options.fetch(:verify_cert) { true }
|
@@ -15,26 +15,27 @@ class HTTPWrapper
|
|
15
15
|
@user_agent = options.fetch(:user_agent) { USER_AGENT }
|
16
16
|
end
|
17
17
|
|
18
|
-
[:get, :post, :put, :delete].each do |
|
19
|
-
define_method
|
18
|
+
[:get, :post, :put, :delete].each do |method_as_symbol|
|
19
|
+
define_method method_as_symbol do |url, params = {}|
|
20
20
|
params[:user_agent] ||= @user_agent
|
21
|
-
get_response Request.new(url,
|
21
|
+
get_response Request.new(url, method_as_symbol, params)
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
method_as_string = method_as_symbol.to_s
|
25
|
+
|
26
|
+
%w(ajax json ajax_json).each do |request_type|
|
27
|
+
define_method "#{method_as_string}_#{request_type}" do |url, params = {}|
|
28
|
+
(params[:headers] ||= {}).merge!(headers_specific_for request_type)
|
29
|
+
public_send method_as_symbol, url, params
|
29
30
|
end
|
30
31
|
end
|
31
32
|
|
32
|
-
alias_method "#{
|
33
|
+
alias_method "#{method_as_string}_json_ajax", "#{method_as_string}_ajax_json"
|
33
34
|
end
|
34
35
|
|
35
36
|
def post_and_get_cookie(url, params = {})
|
36
37
|
response = post url, params
|
37
|
-
response
|
38
|
+
response['set-cookie']
|
38
39
|
end
|
39
40
|
|
40
41
|
def execute(request, uri)
|
@@ -44,14 +45,6 @@ class HTTPWrapper
|
|
44
45
|
|
45
46
|
private
|
46
47
|
|
47
|
-
def validate_options(options)
|
48
|
-
unknown_options = options.keys - KNOWN_OPTIONS_KEYS
|
49
|
-
|
50
|
-
if unknown_options.length > 0
|
51
|
-
raise UnknownParameterError.new "Unknown options: #{unknown_options.join(', ')}"
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
48
|
def get_response(request, redirects_limit = @max_redirects)
|
56
49
|
raise TooManyRedirectsError.new 'Too many redirects!' if redirects_limit == 0
|
57
50
|
|
@@ -65,6 +58,10 @@ class HTTPWrapper
|
|
65
58
|
response
|
66
59
|
end
|
67
60
|
|
61
|
+
def headers_specific_for(request_type)
|
62
|
+
self.class.const_get "#{request_type.upcase}_HEADER"
|
63
|
+
end
|
64
|
+
|
68
65
|
def create_connection(uri)
|
69
66
|
connection = Net::HTTP.new uri.host, uri.port
|
70
67
|
connection.read_timeout = @timeout
|
data/lib/http_wrapper/request.rb
CHANGED
@@ -5,24 +5,24 @@ class HTTPWrapper
|
|
5
5
|
KNOWN_PARAMS_KEYS = [:headers, :query, :cookie, :auth, :body, :user_agent, :content_type, :multipart].freeze
|
6
6
|
|
7
7
|
def initialize(url, method, params = {})
|
8
|
-
|
8
|
+
Utils.validate_hash_keys params, KNOWN_PARAMS_KEYS
|
9
9
|
|
10
|
-
self.uri
|
10
|
+
self.uri = url
|
11
11
|
|
12
|
-
@
|
13
|
-
@
|
14
|
-
@
|
15
|
-
@
|
16
|
-
@password = params[:auth] && params[:auth].fetch(:password)
|
12
|
+
@query = params[:query] || {}
|
13
|
+
@headers = normalize_headers params[:headers]
|
14
|
+
@method = http_method_class_for method
|
15
|
+
@cookie = params[:cookie]
|
17
16
|
|
18
|
-
@
|
19
|
-
@
|
17
|
+
@body_data = params[:body]
|
18
|
+
@multipart_data = params[:multipart]
|
19
|
+
@user_agent = params[:user_agent]
|
20
|
+
@content_type = params[:content_type] || default_content_type_for(method)
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
@multipart_data = params[:multipart] || []
|
22
|
+
if params[:auth]
|
23
|
+
@login = params[:auth].fetch(:login)
|
24
|
+
@password = params[:auth].fetch(:password)
|
25
|
+
end
|
26
26
|
|
27
27
|
initialize_headers
|
28
28
|
end
|
@@ -30,56 +30,60 @@ class HTTPWrapper
|
|
30
30
|
attr_reader :uri
|
31
31
|
|
32
32
|
def uri=(url)
|
33
|
-
|
34
|
-
|
35
|
-
else
|
36
|
-
url = "http://#{url}" unless url =~ /\Ahttps?:\/\//
|
37
|
-
@uri = URI.parse url
|
38
|
-
end
|
33
|
+
url = "http://#{url}" unless url =~ /\Ahttps?:\/\//
|
34
|
+
@uri = URI.parse url
|
39
35
|
end
|
40
36
|
|
41
37
|
def create
|
42
|
-
|
43
|
-
|
44
|
-
create_http_request
|
38
|
+
merge_uri_query
|
39
|
+
create_method_specific_request
|
45
40
|
end
|
46
41
|
|
47
42
|
private
|
48
43
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
44
|
+
def normalize_headers(headers)
|
45
|
+
normal_headers = {}
|
46
|
+
if headers
|
47
|
+
headers.each do |header, value|
|
48
|
+
normal_headers[normalize_header header] = value
|
49
|
+
end
|
54
50
|
end
|
51
|
+
normal_headers
|
55
52
|
end
|
56
53
|
|
57
|
-
def
|
58
|
-
|
59
|
-
|
60
|
-
when :post, :put then @headers[HEADER::CONTENT_TYPE] ||= (@content_type || CONTENT_TYPE::POST)
|
61
|
-
else @headers[HEADER::CONTENT_TYPE] ||= (@content_type || CONTENT_TYPE::DEFAULT)
|
62
|
-
end
|
54
|
+
def normalize_header(header)
|
55
|
+
header = header.to_s.gsub(/_/, '-') if header.is_a? Symbol
|
56
|
+
header.downcase
|
63
57
|
end
|
64
58
|
|
65
|
-
def
|
66
|
-
|
67
|
-
query = @uri.query ? query_to_hash(@uri.query).merge(@query) : @query
|
68
|
-
@uri.query = URI.encode_www_form query
|
59
|
+
def http_method_class_for(method)
|
60
|
+
Net::HTTP.const_get method.to_s.capitalize
|
69
61
|
end
|
70
62
|
|
71
|
-
def
|
72
|
-
|
73
|
-
|
74
|
-
|
63
|
+
def default_content_type_for(method)
|
64
|
+
case method
|
65
|
+
when :post, :put then CONTENT_TYPE::POST
|
66
|
+
else CONTENT_TYPE::DEFAULT
|
75
67
|
end
|
76
68
|
end
|
77
69
|
|
78
|
-
def
|
70
|
+
def initialize_headers
|
71
|
+
@headers[HEADER::USER_AGENT] ||= @user_agent
|
72
|
+
@headers[HEADER::CONTENT_TYPE] ||= @content_type
|
73
|
+
@headers[HEADER::COOKIE] ||= @cookie if @cookie
|
74
|
+
end
|
75
|
+
|
76
|
+
def merge_uri_query
|
77
|
+
return unless @query.size > 0
|
78
|
+
original_query = @uri.query ? Utils.query_to_hash(@uri.query) : {}
|
79
|
+
merged_query = original_query.merge @query
|
80
|
+
@uri.query = Utils.hash_to_query merged_query
|
81
|
+
end
|
82
|
+
|
83
|
+
def create_method_specific_request
|
79
84
|
# Ruby v1.9.3 doesn't understand full URI object, it needs just path :(
|
80
85
|
uri = RUBY_VERSION =~ /\A2/ ? @uri : @uri.request_uri
|
81
|
-
@request = @
|
82
|
-
set_cookies
|
86
|
+
@request = @method.new uri, @headers
|
83
87
|
set_body
|
84
88
|
set_basic_auth
|
85
89
|
@request
|
@@ -87,35 +91,31 @@ class HTTPWrapper
|
|
87
91
|
|
88
92
|
def set_body
|
89
93
|
return unless @request.request_body_permitted?
|
90
|
-
if @multipart_data
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
@request.body = @body.is_a?(Hash) ? hash_to_query(@body) : @body
|
94
|
+
if @multipart_data
|
95
|
+
set_body_from_multipart_data
|
96
|
+
else
|
97
|
+
set_body_from_body_data
|
95
98
|
end
|
96
99
|
end
|
97
100
|
|
98
|
-
def
|
99
|
-
|
100
|
-
@request.
|
101
|
+
def set_body_from_multipart_data
|
102
|
+
convert_body_data_to_multipart_data if @body_data
|
103
|
+
@request.set_form @multipart_data, CONTENT_TYPE::MULTIPART
|
101
104
|
end
|
102
105
|
|
103
|
-
def
|
104
|
-
|
105
|
-
@
|
106
|
+
def convert_body_data_to_multipart_data
|
107
|
+
@body_data = Utils.query_to_hash(@body_data) unless @body_data.kind_of? Hash
|
108
|
+
@body_data.each{|key, value| @multipart_data << [key.to_s, value.to_s]}
|
106
109
|
end
|
107
110
|
|
108
|
-
def
|
109
|
-
|
110
|
-
@body
|
111
|
+
def set_body_from_body_data
|
112
|
+
return unless @body_data
|
113
|
+
@request.body = @body_data.is_a?(Hash) ? Utils.hash_to_query(@body_data) : @body_data
|
111
114
|
end
|
112
115
|
|
113
|
-
def
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
def hash_to_query(hash)
|
118
|
-
URI.encode_www_form hash
|
116
|
+
def set_basic_auth
|
117
|
+
return unless @login
|
118
|
+
@request.basic_auth @login, @password
|
119
119
|
end
|
120
120
|
end
|
121
121
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class HTTPWrapper
|
2
|
+
module Utils
|
3
|
+
def self.validate_hash_keys(hash_to_check, known_keys_array)
|
4
|
+
unknown_keys = hash_to_check.keys - known_keys_array
|
5
|
+
if unknown_keys.length > 0
|
6
|
+
raise UnknownKeyError.new "Unknown keys: #{unknown_keys.join(', ')}"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.query_to_hash(query)
|
11
|
+
Hash[URI.decode_www_form query]
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.hash_to_query(hash)
|
15
|
+
URI.encode_www_form hash
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/http_wrapper/version.rb
CHANGED
data/lib/http_wrapper.rb
CHANGED
data/spec/http_wrapper_spec.rb
CHANGED
@@ -24,13 +24,13 @@ describe HTTPWrapper do
|
|
24
24
|
it 'should raise UnknownParameterError if initial options key is unknown' do
|
25
25
|
expect do
|
26
26
|
HTTPWrapper.new unknown_option: 'test', maybe_this_known: '?'
|
27
|
-
end.to raise_error HTTPWrapper::
|
27
|
+
end.to raise_error HTTPWrapper::UnknownKeyError, 'Unknown keys: unknown_option, maybe_this_known'
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should raise UnknownParameterError if params key is unknown' do
|
31
31
|
expect do
|
32
32
|
subject.get sample_url, unknown_param_key: 'test', another_param_key: 'wow'
|
33
|
-
end.to raise_error HTTPWrapper::
|
33
|
+
end.to raise_error HTTPWrapper::UnknownKeyError, 'Unknown keys: unknown_param_key, another_param_key'
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'should follow redirects no more then 10 times by default' do
|
@@ -159,6 +159,12 @@ describe HTTPWrapper do
|
|
159
159
|
subject.get sample_url, params
|
160
160
|
end
|
161
161
|
|
162
|
+
it 'should use headers cookie if both (headers and parameters) cookies provided' do
|
163
|
+
params = { headers: {'Cookie' => 'Custom cookie'} }
|
164
|
+
stub_get sample_url, params
|
165
|
+
subject.get sample_url, params.merge({cookie: 'should not use this one'})
|
166
|
+
end
|
167
|
+
|
162
168
|
it 'should hit provided url with basic auth' do
|
163
169
|
stub_get sample_url_with_basic_auth
|
164
170
|
subject.get sample_url, auth: {login: basic_auth_login, password: basic_auth_password}
|
@@ -170,8 +176,9 @@ describe HTTPWrapper do
|
|
170
176
|
end
|
171
177
|
|
172
178
|
it 'should equally treat header as string and header as symbol' do
|
173
|
-
|
174
|
-
|
179
|
+
custom_content_type = 'Some Content Type'
|
180
|
+
stub_get sample_url, { headers: {'Content-Type' => custom_content_type} }
|
181
|
+
subject.get sample_url, { headers: {content_type: custom_content_type} }
|
175
182
|
end
|
176
183
|
end
|
177
184
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_wrapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leonid Svyatov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-22 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -83,11 +83,12 @@ files:
|
|
83
83
|
- lib/http_wrapper/errors.rb
|
84
84
|
- lib/http_wrapper/http_wrapper.rb
|
85
85
|
- lib/http_wrapper/request.rb
|
86
|
+
- lib/http_wrapper/utils.rb
|
86
87
|
- lib/http_wrapper/version.rb
|
87
88
|
- lib/http_wrapper.rb
|
88
89
|
- spec/http_wrapper_spec.rb
|
89
90
|
- spec/spec_helper.rb
|
90
|
-
homepage: http://github.com/
|
91
|
+
homepage: http://github.com/svyatov/http_wrapper
|
91
92
|
licenses:
|
92
93
|
- MIT
|
93
94
|
metadata: {}
|