http_wrapper 2.1.0 → 2.1.1

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: a2fa8e4065e61bf929e24793f65c42273fa98884
4
- data.tar.gz: 4e0efea520c0a5cca574430e3d8cef060004cd60
3
+ metadata.gz: fa85c2016386aa01baeab80596dc32c5fe9a8487
4
+ data.tar.gz: fd6379cb93042cc78a5cfc4efbea1042a7377fbe
5
5
  SHA512:
6
- metadata.gz: 380fa0e02f48cb4affca5d9c74fe54352965bbd6b4630909f509ffddced19f49af661595fce73aa6387e811989ea769d0f93a5ceb31855a60600b0f15b4b7180
7
- data.tar.gz: 833ada0ed80bd08c7d6313f6e2c07bdc4c9cdb3ee976aac359d8f44420756cbdb45bb2216e09ce8e128351fbbf687ddd625d29315f759fb561803253b3933f27
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
  [![Gem Version](https://badge.fury.io/rb/http_wrapper.png)](http://badge.fury.io/rb/http_wrapper)
6
- [![Build Status](https://travis-ci.org/Svyatov/http_wrapper.png)](https://travis-ci.org/Svyatov/http_wrapper)
7
- [![Dependency Status](https://gemnasium.com/Svyatov/http_wrapper.png)](https://gemnasium.com/Svyatov/http_wrapper)
8
- [![Code Climate](https://codeclimate.com/github/Svyatov/http_wrapper.png)](https://codeclimate.com/github/Svyatov/http_wrapper)
9
- [![Coverage Status](https://coveralls.io/repos/Svyatov/http_wrapper/badge.png)](https://coveralls.io/r/Svyatov/http_wrapper)
6
+ [![Build Status](https://travis-ci.org/svyatov/http_wrapper.png)](https://travis-ci.org/svyatov/http_wrapper)
7
+ [![Dependency Status](https://gemnasium.com/svyatov/http_wrapper.png)](https://gemnasium.com/svyatov/http_wrapper)
8
+ [![Code Climate](https://codeclimate.com/github/svyatov/http_wrapper.png)](https://codeclimate.com/github/svyatov/http_wrapper)
9
+ [![Coverage Status](https://coveralls.io/repos/svyatov/http_wrapper/badge.png)](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.0'
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 `UnknownParameterError` exception.
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 = 'Content-Type'.freeze
4
- USER_AGENT = 'User-Agent'.freeze
5
- COOKIE = 'Cookie'.freeze
6
- AJAX = 'X-Requested-With'.freeze
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
@@ -1,5 +1,5 @@
1
1
  class HTTPWrapper
2
2
  HTTPWrapperError = Class.new StandardError
3
3
  TooManyRedirectsError = Class.new HTTPWrapperError
4
- UnknownParameterError = Class.new HTTPWrapperError
4
+ UnknownKeyError = Class.new HTTPWrapperError
5
5
  end
@@ -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
- validate_options options
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 |method|
19
- define_method method do |url, params = {}|
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, method, params)
21
+ get_response Request.new(url, method_as_symbol, params)
22
22
  end
23
23
 
24
- %w(ajax json ajax_json).each do |header|
25
- define_method "#{method.to_s}_#{header}" do |url, params = {}|
26
- params[:headers] ||= {}
27
- params[:headers].merge! HTTPWrapper.const_get("#{header}_HEADER".upcase)
28
- __send__ method, url, params
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 "#{method.to_s}_json_ajax", "#{method.to_s}_ajax_json"
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.response['set-cookie']
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
@@ -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
- validate_parameters params
8
+ Utils.validate_hash_keys params, KNOWN_PARAMS_KEYS
9
9
 
10
- self.uri = url
10
+ self.uri = url
11
11
 
12
- @headers = params[:headers] || {}
13
- @query = params[:query] || {}
14
- @cookie = params[:cookie] || @headers[HEADER::COOKIE]
15
- @login = params[:auth] && params[:auth].fetch(:login)
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
- @method = method
19
- @method_class = Net::HTTP.const_get(method.to_s.capitalize)
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
- @body = params[:body]
22
- @user_agent = params[:user_agent]
23
- @content_type = params[:content_type]
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
- if url.is_a? URI
34
- @uri = url
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
- rebuild_uri_query_params
43
- convert_symbol_headers_to_string
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 validate_parameters(params)
50
- unknown_params = params.keys - KNOWN_PARAMS_KEYS
51
-
52
- if unknown_params.length > 0
53
- raise UnknownParameterError.new "Unknown parameters: #{unknown_params.join(', ')}"
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 initialize_headers
58
- @headers[HEADER::USER_AGENT] ||= @user_agent
59
- case @method
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 rebuild_uri_query_params
66
- return unless @query.size > 0
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 convert_symbol_headers_to_string
72
- @headers.keys.select{|key| key.is_a? Symbol}.each do |key|
73
- str_key = key.to_s.gsub(/_/, '-').capitalize
74
- @headers[str_key] = @headers.delete key
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 create_http_request
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 = @method_class.new uri, @headers
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.length > 0
91
- convert_body_to_multipart_data if @body
92
- @request.set_form @multipart_data, CONTENT_TYPE::MULTIPART
93
- elsif @body
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 set_basic_auth
99
- return unless @login
100
- @request.basic_auth @login, @password
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 set_cookies
104
- return unless @cookie
105
- @request['Cookie'] = @cookie
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 convert_body_to_multipart_data
109
- @body = query_to_hash(@body) unless @body.kind_of? Hash
110
- @body.each{|key, value| @multipart_data << [key.to_s, value.to_s]}
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 query_to_hash(query)
114
- Hash[URI.decode_www_form query]
115
- end
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
@@ -1,3 +1,3 @@
1
1
  class HTTPWrapper
2
- VERSION = '2.1.0'.freeze
2
+ VERSION = '2.1.1'.freeze
3
3
  end
data/lib/http_wrapper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'http_wrapper/version'
2
2
  require 'http_wrapper/constants'
3
3
  require 'http_wrapper/errors'
4
+ require 'http_wrapper/utils'
4
5
  require 'http_wrapper/request'
5
6
  require 'http_wrapper/http_wrapper'
@@ -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::UnknownParameterError, 'Unknown options: unknown_option, maybe_this_known'
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::UnknownParameterError, 'Unknown parameters: unknown_param_key, another_param_key'
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
- stub_get sample_url, { headers: {'Content-Type' => 'Some Content Type'} }
174
- subject.get sample_url, { headers: {content_type: 'Some Content Type'} }
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.0
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-18 00:00:00.000000000 Z
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/Svyatov/http_wrapper
91
+ homepage: http://github.com/svyatov/http_wrapper
91
92
  licenses:
92
93
  - MIT
93
94
  metadata: {}