typhoeus 0.5.3 → 0.5.4
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/CHANGELOG.md +19 -1
- data/lib/rack/typhoeus.rb +1 -0
- data/lib/rack/typhoeus/middleware/params_decoder.rb +57 -0
- data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +72 -0
- data/lib/typhoeus.rb +17 -6
- data/lib/typhoeus/expectation.rb +10 -10
- data/lib/typhoeus/hydra/easy_factory.rb +1 -1
- data/lib/typhoeus/railtie.rb +12 -0
- data/lib/typhoeus/request.rb +12 -8
- data/lib/typhoeus/request/actions.rb +15 -15
- data/lib/typhoeus/request/callbacks.rb +3 -1
- data/lib/typhoeus/request/operations.rb +13 -12
- data/lib/typhoeus/response.rb +6 -1
- data/lib/typhoeus/response/header.rb +44 -14
- data/lib/typhoeus/response/informations.rb +1 -1
- data/lib/typhoeus/version.rb +1 -1
- metadata +9 -5
data/CHANGELOG.md
CHANGED
@@ -2,7 +2,25 @@
|
|
2
2
|
|
3
3
|
## Master
|
4
4
|
|
5
|
-
[Full Changelog](http://github.com/typhoeus/typhoeus/compare/v0.5.
|
5
|
+
[Full Changelog](http://github.com/typhoeus/typhoeus/compare/v0.5.4...master)
|
6
|
+
|
7
|
+
## 0.5.4
|
8
|
+
|
9
|
+
[Full Changelog](http://github.com/typhoeus/typhoeus/compare/v0.5.3...v0.5.4)
|
10
|
+
|
11
|
+
Enhancements:
|
12
|
+
|
13
|
+
* Make sure response_code is an integer.
|
14
|
+
* When setting an header through vcr or webmock it becomes a `Typhoeus::Response::Header`.
|
15
|
+
* Provide a Rack middleware to decode nested Typhoeus arrays properly.
|
16
|
+
([Dwayne Macgowan](https://github.com/dwaynemac), [\#224](https://github.com/typhoeus/typhoeus/issues/224))
|
17
|
+
* Handled response is available again.
|
18
|
+
* Rename parameter `url` to `base_url`. See discussion here: [\#250](https://github.com/typhoeus/typhoeus/issues/250).
|
19
|
+
([bkimble](https://github.com/bkimble), [\#256](https://github.com/typhoeus/typhoeus/pull/256))
|
20
|
+
* Provide O(1) header access.
|
21
|
+
* Work around nuby 1.8.7 limitations.
|
22
|
+
( [](https://github.com/sshingler), [\#176](https://github.com/typhoeus/typhoeus/pull/176) )
|
23
|
+
* Provide symbol access.
|
6
24
|
|
7
25
|
## 0.5.3
|
8
26
|
|
@@ -0,0 +1 @@
|
|
1
|
+
require "rack/typhoeus/middleware/params_decoder"
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rack/typhoeus/middleware/params_decoder/helper'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
module Typhoeus
|
5
|
+
module Middleware
|
6
|
+
|
7
|
+
# This Rack middleware takes care of the proper deserialization of
|
8
|
+
# the nested params encoded by Typhoeus.
|
9
|
+
#
|
10
|
+
# @example Require the railtie when using Rails.
|
11
|
+
# require 'typhoeus/railtie'
|
12
|
+
#
|
13
|
+
# @example Include the middleware for Rack based applications.
|
14
|
+
# use Rack::Typhoeus::Middleware::ParamsDecoder
|
15
|
+
#
|
16
|
+
# @example Use the helper directly. Not recommended as b/c the interface might change.
|
17
|
+
# require 'rack/typhoeus/middleware/params_decoder/helper'
|
18
|
+
# include Rack::Typhoeus::Middleware::ParamsDecoder::Helper
|
19
|
+
# decode!(params)
|
20
|
+
#
|
21
|
+
# @author Dwayne Macgowan
|
22
|
+
# @since 0.5.4
|
23
|
+
class ParamsDecoder
|
24
|
+
include ParamsDecoder::Helper
|
25
|
+
|
26
|
+
def initialize(app)
|
27
|
+
@app = app
|
28
|
+
end
|
29
|
+
|
30
|
+
def call(env)
|
31
|
+
req = Rack::Request.new(env)
|
32
|
+
decode(req.params).each_pair { |k, v| update_params req, k, v }
|
33
|
+
@app.call(env)
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# Persist params change in environment. Extracted from:
|
39
|
+
# https://github.com/rack/rack/blob/master/lib/rack/request.rb#L233
|
40
|
+
def update_params(req, k, v)
|
41
|
+
found = false
|
42
|
+
if req.GET.has_key?(k)
|
43
|
+
found = true
|
44
|
+
req.GET[k] = v
|
45
|
+
end
|
46
|
+
if req.POST.has_key?(k)
|
47
|
+
found = true
|
48
|
+
req.POST[k] = v
|
49
|
+
end
|
50
|
+
unless found
|
51
|
+
req.GET[k] = v
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Rack
|
2
|
+
module Typhoeus
|
3
|
+
module Middleware
|
4
|
+
class ParamsDecoder
|
5
|
+
module Helper
|
6
|
+
|
7
|
+
# Recursively decodes Typhoeus encoded arrays in given Hash.
|
8
|
+
#
|
9
|
+
# @example Use directly in a Rails controller.
|
10
|
+
# class ApplicationController
|
11
|
+
# before_filter :decode_typhoeus_arrays
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# @author Dwayne Macgowan
|
15
|
+
#
|
16
|
+
def decode_typhoeus_arrays
|
17
|
+
decode!(params)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Recursively decodes Typhoeus encoded arrays in given Hash.
|
21
|
+
#
|
22
|
+
# @param hash [Hash]. This Hash will be modified!
|
23
|
+
#
|
24
|
+
# @return [Hash] Hash with properly decoded nested arrays.
|
25
|
+
def decode!(hash)
|
26
|
+
return hash unless hash.is_a?(Hash)
|
27
|
+
hash.each_pair do |key,value|
|
28
|
+
if value.is_a?(Hash)
|
29
|
+
decode!(value)
|
30
|
+
hash[key] = convert(value)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
hash
|
34
|
+
end
|
35
|
+
|
36
|
+
def decode(hash)
|
37
|
+
decode!(hash.dup)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Checks if Hash is an Array encoded as a Hash.
|
43
|
+
# Specifically will check for the Hash to have this
|
44
|
+
# form: {'0' => v0, '1' => v1, .., 'n' => vN }
|
45
|
+
#
|
46
|
+
# @param hash [Hash]
|
47
|
+
#
|
48
|
+
# @return [Boolean] True if its a encoded Array, else false.
|
49
|
+
def encoded?(hash)
|
50
|
+
return false if hash.empty?
|
51
|
+
keys = hash.keys.map{|i| i.to_i if i.respond_to?(:to_i)}.sort
|
52
|
+
keys == hash.keys.size.times.to_a
|
53
|
+
end
|
54
|
+
|
55
|
+
# If the Hash is an array encoded by typhoeus an array is returned
|
56
|
+
# else the self is returned
|
57
|
+
#
|
58
|
+
# @param hash [Hash] The Hash to convert into an Array.
|
59
|
+
#
|
60
|
+
# @return [Arraya/Hash]
|
61
|
+
def convert(hash)
|
62
|
+
if encoded?(hash)
|
63
|
+
Hash[hash.sort].values
|
64
|
+
else
|
65
|
+
hash
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/typhoeus.rb
CHANGED
@@ -9,8 +9,19 @@ require 'typhoeus/request'
|
|
9
9
|
require 'typhoeus/response'
|
10
10
|
require 'typhoeus/version'
|
11
11
|
|
12
|
+
# If we are using any Rack based application then we need the Typhoeus rack
|
13
|
+
# # middleware to ensure our app is running properly.
|
14
|
+
if defined?(Rack)
|
15
|
+
require "rack/typhoeus"
|
16
|
+
end
|
17
|
+
|
18
|
+
# If we are using Rails then we will include the Typhoeus railtie.
|
19
|
+
# if defined?(Rails)
|
20
|
+
# require "typhoeus/railtie"
|
21
|
+
# end
|
22
|
+
|
12
23
|
# Typhoeus is a http client library based on Ethon which
|
13
|
-
# wraps libcurl. Sitting on top of libcurl
|
24
|
+
# wraps libcurl. Sitting on top of libcurl makes Typhoeus
|
14
25
|
# very reliable and fast.
|
15
26
|
#
|
16
27
|
# There are some gems using Typhoeus like
|
@@ -55,17 +66,17 @@ module Typhoeus
|
|
55
66
|
#
|
56
67
|
# @example (see Typhoeus::Expectation)
|
57
68
|
#
|
58
|
-
# @param [ String ]
|
69
|
+
# @param [ String ] base_url The url to stub out.
|
59
70
|
# @param [ Hash ] options The options to stub out.
|
60
71
|
#
|
61
72
|
# @return [ Typhoeus::Expectation ] The expecatation.
|
62
73
|
#
|
63
74
|
# @see Typhoeus::Expectation
|
64
|
-
def stub(
|
65
|
-
expectation = Expectation.all.find{ |e| e.
|
75
|
+
def stub(base_url, options = {})
|
76
|
+
expectation = Expectation.all.find{ |e| e.base_url == base_url && e.options == options }
|
66
77
|
return expectation if expectation
|
67
78
|
|
68
|
-
Expectation.new(
|
79
|
+
Expectation.new(base_url, options).tap do |new_expectation|
|
69
80
|
Expectation.all << new_expectation
|
70
81
|
end
|
71
82
|
end
|
@@ -73,7 +84,7 @@ module Typhoeus
|
|
73
84
|
# Add before callbacks.
|
74
85
|
#
|
75
86
|
# @example Add before callback.
|
76
|
-
# Typhoeus.before { |request| p request.
|
87
|
+
# Typhoeus.before { |request| p request.base_url }
|
77
88
|
#
|
78
89
|
# @param [ Block ] block The callback.
|
79
90
|
#
|
data/lib/typhoeus/expectation.rb
CHANGED
@@ -17,7 +17,7 @@ module Typhoeus
|
|
17
17
|
class Expectation
|
18
18
|
|
19
19
|
# @api private
|
20
|
-
attr_reader :
|
20
|
+
attr_reader :base_url
|
21
21
|
|
22
22
|
# @api private
|
23
23
|
attr_reader :options
|
@@ -66,13 +66,13 @@ module Typhoeus
|
|
66
66
|
# Creates an expactation.
|
67
67
|
#
|
68
68
|
# @example Create expactation.
|
69
|
-
# Typhoeus::Expectation.new(
|
69
|
+
# Typhoeus::Expectation.new(base_url)
|
70
70
|
#
|
71
71
|
# @return [ Expectation ] The created expactation.
|
72
72
|
#
|
73
73
|
# @api private
|
74
|
-
def initialize(
|
75
|
-
@
|
74
|
+
def initialize(base_url, options = {})
|
75
|
+
@base_url = base_url
|
76
76
|
@options = options
|
77
77
|
@response_counter = 0
|
78
78
|
@from = nil
|
@@ -117,7 +117,7 @@ module Typhoeus
|
|
117
117
|
#
|
118
118
|
# @api private
|
119
119
|
def matches?(request)
|
120
|
-
url_match?(request.
|
120
|
+
url_match?(request.base_url) && options_match?(request)
|
121
121
|
end
|
122
122
|
|
123
123
|
# Return canned responses.
|
@@ -157,18 +157,18 @@ module Typhoeus
|
|
157
157
|
(options ? options.all?{ |k,v| request.original_options[k] == v || request.options[k] == v } : true)
|
158
158
|
end
|
159
159
|
|
160
|
-
# Check wether the
|
161
|
-
# The
|
160
|
+
# Check wether the base_url matches the request url.
|
161
|
+
# The base_url can be a string, regex or nil. String and
|
162
162
|
# regexp were checked, nil is always true. Else false.
|
163
163
|
#
|
164
164
|
# Nil serves as a placeholder in case you want to match
|
165
165
|
# all urls.
|
166
166
|
def url_match?(request_url)
|
167
|
-
case
|
167
|
+
case base_url
|
168
168
|
when String
|
169
|
-
|
169
|
+
base_url == request_url
|
170
170
|
when Regexp
|
171
|
-
!!request_url.match(
|
171
|
+
!!request_url.match(base_url)
|
172
172
|
when nil
|
173
173
|
true
|
174
174
|
else
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "typhoeus"
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Typhoeus
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
# Need to include the Typhoeus middleware.
|
7
|
+
initializer "include the identity map" do |app|
|
8
|
+
app.config.middleware.use "Rack::Typhoeus::Middleware::ParamsDecoder"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/typhoeus/request.rb
CHANGED
@@ -30,10 +30,10 @@ module Typhoeus
|
|
30
30
|
include Request::Stubbable
|
31
31
|
include Request::Before
|
32
32
|
|
33
|
-
# Returns the provided url.
|
33
|
+
# Returns the provided base url.
|
34
34
|
#
|
35
35
|
# @return [ String ]
|
36
|
-
attr_accessor :
|
36
|
+
attr_accessor :base_url
|
37
37
|
|
38
38
|
# Returns options, which includes default parameters.
|
39
39
|
#
|
@@ -89,7 +89,7 @@ module Typhoeus
|
|
89
89
|
# followlocation: true
|
90
90
|
# ).run
|
91
91
|
#
|
92
|
-
# @param [ String ]
|
92
|
+
# @param [ String ] base_url The url to request.
|
93
93
|
# @param [ options ] options The options.
|
94
94
|
#
|
95
95
|
# @option options [ Hash ] :params Translated
|
@@ -99,18 +99,22 @@ module Typhoeus
|
|
99
99
|
#
|
100
100
|
# @return [ Typhoeus::Request ] The request.
|
101
101
|
#
|
102
|
-
# @note See {http://rubydoc.info/github/typhoeus/ethon/Ethon/Easy
|
102
|
+
# @note See {http://rubydoc.info/github/typhoeus/ethon/Ethon/Easy/Options Ethon::Easy::Options} for more options.
|
103
103
|
#
|
104
104
|
# @see Typhoeus::Hydra
|
105
105
|
# @see Typhoeus::Response
|
106
106
|
# @see Typhoeus::Request::Actions
|
107
|
-
def initialize(
|
108
|
-
@
|
107
|
+
def initialize(base_url, options = {})
|
108
|
+
@base_url = base_url
|
109
109
|
@original_options = options
|
110
110
|
@options = options.dup
|
111
111
|
|
112
112
|
set_defaults
|
113
113
|
end
|
114
|
+
|
115
|
+
def url
|
116
|
+
base_url
|
117
|
+
end
|
114
118
|
|
115
119
|
# Returns wether other is equal to self.
|
116
120
|
#
|
@@ -124,7 +128,7 @@ module Typhoeus
|
|
124
128
|
# @api private
|
125
129
|
def eql?(other)
|
126
130
|
self.class == other.class &&
|
127
|
-
self.
|
131
|
+
self.base_url == other.base_url &&
|
128
132
|
fuzzy_hash_eql?(self.options, other.options)
|
129
133
|
end
|
130
134
|
|
@@ -134,7 +138,7 @@ module Typhoeus
|
|
134
138
|
#
|
135
139
|
# @api private
|
136
140
|
def hash
|
137
|
-
[ self.class, self.
|
141
|
+
[ self.class, self.base_url, self.options ].hash
|
138
142
|
end
|
139
143
|
|
140
144
|
private
|
@@ -18,8 +18,8 @@ module Typhoeus
|
|
18
18
|
# @return (see Typhoeus::Request#initialize)
|
19
19
|
#
|
20
20
|
# @note (see Typhoeus::Request#initialize)
|
21
|
-
def get(
|
22
|
-
Request.new(
|
21
|
+
def get(base_url, options = {})
|
22
|
+
Request.new(base_url, options.merge(:method => :get)).run
|
23
23
|
end
|
24
24
|
|
25
25
|
# Make a post request.
|
@@ -34,8 +34,8 @@ module Typhoeus
|
|
34
34
|
# @return (see Typhoeus::Request#initialize)
|
35
35
|
#
|
36
36
|
# @note (see Typhoeus::Request#initialize)
|
37
|
-
def post(
|
38
|
-
Request.new(
|
37
|
+
def post(base_url, options = {})
|
38
|
+
Request.new(base_url, options.merge(:method => :post)).run
|
39
39
|
end
|
40
40
|
|
41
41
|
# Make a put request.
|
@@ -46,15 +46,15 @@ module Typhoeus
|
|
46
46
|
# @param (see Typhoeus::Request#initialize)
|
47
47
|
#
|
48
48
|
# @option options :params [ Hash ] Params hash which
|
49
|
-
# is attached to the
|
49
|
+
# is attached to the base_url.
|
50
50
|
# @option options :body [ Hash ] Body hash which
|
51
51
|
# becomes a PUT request body.
|
52
52
|
#
|
53
53
|
# @return (see Typhoeus::Request#initialize)
|
54
54
|
#
|
55
55
|
# @note (see Typhoeus::Request#initialize)
|
56
|
-
def put(
|
57
|
-
Request.new(
|
56
|
+
def put(base_url, options = {})
|
57
|
+
Request.new(base_url, options.merge(:method => :put)).run
|
58
58
|
end
|
59
59
|
|
60
60
|
# Make a delete request.
|
@@ -69,8 +69,8 @@ module Typhoeus
|
|
69
69
|
# @return (see Typhoeus::Request#initialize)
|
70
70
|
#
|
71
71
|
# @note (see Typhoeus::Request#initialize)
|
72
|
-
def delete(
|
73
|
-
Request.new(
|
72
|
+
def delete(base_url, options = {})
|
73
|
+
Request.new(base_url, options.merge(:method => :delete)).run
|
74
74
|
end
|
75
75
|
|
76
76
|
# Make a head request.
|
@@ -85,8 +85,8 @@ module Typhoeus
|
|
85
85
|
# @return (see Typhoeus::Request#initialize)
|
86
86
|
#
|
87
87
|
# @note (see Typhoeus::Request#initialize)
|
88
|
-
def head(
|
89
|
-
Request.new(
|
88
|
+
def head(base_url, options = {})
|
89
|
+
Request.new(base_url, options.merge(:method => :head)).run
|
90
90
|
end
|
91
91
|
|
92
92
|
# Make a patch request.
|
@@ -101,8 +101,8 @@ module Typhoeus
|
|
101
101
|
# @return (see Typhoeus::Request#initialize)
|
102
102
|
#
|
103
103
|
# @note (see Typhoeus::Request#initialize)
|
104
|
-
def patch(
|
105
|
-
Request.new(
|
104
|
+
def patch(base_url, options = {})
|
105
|
+
Request.new(base_url, options.merge(:method => :patch)).run
|
106
106
|
end
|
107
107
|
|
108
108
|
# Make a options request.
|
@@ -117,8 +117,8 @@ module Typhoeus
|
|
117
117
|
# @return (see Typhoeus::Request#initialize)
|
118
118
|
#
|
119
119
|
# @note (see Typhoeus::Request#initialize)
|
120
|
-
def options(
|
121
|
-
Request.new(
|
120
|
+
def options(base_url, options = {})
|
121
|
+
Request.new(base_url, options.merge(:method => :options)).run
|
122
122
|
end
|
123
123
|
end
|
124
124
|
end
|
@@ -95,7 +95,9 @@ module Typhoeus
|
|
95
95
|
callbacks += Typhoeus.on_failure + on_failure
|
96
96
|
end
|
97
97
|
|
98
|
-
callbacks.map
|
98
|
+
callbacks.map do |callback|
|
99
|
+
self.response.handled_response = callback.call(self.response)
|
100
|
+
end
|
99
101
|
end
|
100
102
|
end
|
101
103
|
end
|
@@ -15,7 +15,7 @@ module Typhoeus
|
|
15
15
|
easy = Typhoeus.get_easy
|
16
16
|
begin
|
17
17
|
easy.http_request(
|
18
|
-
|
18
|
+
base_url,
|
19
19
|
options.fetch(:method, :get),
|
20
20
|
options.reject{|k,_| k==:method}
|
21
21
|
)
|
@@ -52,26 +52,27 @@ module Typhoeus
|
|
52
52
|
|
53
53
|
def provide_help(option)
|
54
54
|
renamed = {
|
55
|
+
:auth_method => :httpauth,
|
55
56
|
:connect_timeout => :connecttimeout,
|
57
|
+
:disable_ssl_host_verification => :ssl_verifyhost,
|
58
|
+
:disable_ssl_peer_verification => :ssl_verifypeer,
|
59
|
+
:encoding => :accept_encoding,
|
56
60
|
:follow_location => :followlocation,
|
57
61
|
:max_redirects => :maxredirs,
|
58
|
-
:
|
62
|
+
:password => :userpwd,
|
63
|
+
:proxy_auth_method => :proxyauth,
|
59
64
|
:proxy_password => :proxyuserpwd,
|
60
|
-
:
|
61
|
-
:
|
65
|
+
:proxy_type => :proxytype,
|
66
|
+
:proxy_username => :proxyuserpwd,
|
67
|
+
:ssl_cacert => :cainfo,
|
68
|
+
:ssl_capath => :capath,
|
62
69
|
:ssl_cert => :sslcert,
|
63
70
|
:ssl_cert_type => :sslcerttype,
|
64
71
|
:ssl_key => :sslkey,
|
65
|
-
:ssl_key_type => :sslkeytype,
|
66
72
|
:ssl_key_password => :keypasswd,
|
67
|
-
:
|
68
|
-
:ssl_capath => :capath,
|
73
|
+
:ssl_key_type => :sslkeytype,
|
69
74
|
:ssl_version => :sslversion,
|
70
|
-
:username => :userpwd
|
71
|
-
:password => :userpwd,
|
72
|
-
:auth_method => :httpauth,
|
73
|
-
:proxy_auth_method => :proxyauth,
|
74
|
-
:proxy_type => :proxytype
|
75
|
+
:username => :userpwd
|
75
76
|
}
|
76
77
|
removed = [:cache_key_basis, :cache_timout, :user_agent]
|
77
78
|
if new_option = renamed[option.to_sym]
|
data/lib/typhoeus/response.rb
CHANGED
@@ -26,6 +26,11 @@ module Typhoeus
|
|
26
26
|
# @return [ Hash ]
|
27
27
|
attr_accessor :options
|
28
28
|
|
29
|
+
# The handled response.
|
30
|
+
#
|
31
|
+
# @return [ String ]
|
32
|
+
attr_accessor :handled_response
|
33
|
+
|
29
34
|
# @api private
|
30
35
|
attr_writer :mock
|
31
36
|
|
@@ -39,7 +44,7 @@ module Typhoeus
|
|
39
44
|
# @return [ Response ] The new response.
|
40
45
|
def initialize(options = {})
|
41
46
|
@options = options
|
42
|
-
@headers = options[:headers]
|
47
|
+
@headers = Header.new(options[:headers]) if options[:headers]
|
43
48
|
end
|
44
49
|
|
45
50
|
# Returns wether this request is mocked
|
@@ -6,6 +6,7 @@ module Typhoeus
|
|
6
6
|
#
|
7
7
|
# @api private
|
8
8
|
class Header < Hash
|
9
|
+
|
9
10
|
# Create a new header.
|
10
11
|
#
|
11
12
|
# @example Create new header.
|
@@ -14,7 +15,9 @@ module Typhoeus
|
|
14
15
|
# @param [ String ] raw The raw header.
|
15
16
|
def initialize(raw)
|
16
17
|
@raw = raw
|
18
|
+
@sanitized = {}
|
17
19
|
parse
|
20
|
+
set_default_proc_on(self, lambda { |h, k| @sanitized[k.to_s.downcase] })
|
18
21
|
end
|
19
22
|
|
20
23
|
# Parses the raw header.
|
@@ -22,15 +25,16 @@ module Typhoeus
|
|
22
25
|
# @example Parse header.
|
23
26
|
# header.parse
|
24
27
|
def parse
|
25
|
-
raw
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
28
|
+
case @raw
|
29
|
+
when Hash
|
30
|
+
raw.each do |k, v|
|
31
|
+
process_pair(k, v)
|
32
|
+
end
|
33
|
+
when String
|
34
|
+
raw.lines.each do |header|
|
35
|
+
next if header.empty? || header =~ /^HTTP\/1.[01]/
|
36
|
+
process_line(header)
|
37
|
+
end
|
34
38
|
end
|
35
39
|
end
|
36
40
|
|
@@ -41,11 +45,26 @@ module Typhoeus
|
|
41
45
|
# @return [ void ]
|
42
46
|
def process_line(header)
|
43
47
|
key, value = header.split(':', 2).map(&:strip)
|
44
|
-
|
45
|
-
|
46
|
-
|
48
|
+
process_pair(key, value)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Sets key value pair for self and @sanitized.
|
52
|
+
#
|
53
|
+
# @return [ void ]
|
54
|
+
def process_pair(key, value)
|
55
|
+
set_value(key, value, self)
|
56
|
+
set_value(key.downcase, value, @sanitized)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Sets value for key in specified hash
|
60
|
+
#
|
61
|
+
# @return [ void ]
|
62
|
+
def set_value(key, value, hash)
|
63
|
+
if hash.has_key?(key)
|
64
|
+
hash[key] = [hash[key]] unless hash[key].is_a? Array
|
65
|
+
hash[key].push(value)
|
47
66
|
else
|
48
|
-
|
67
|
+
hash[key] = value
|
49
68
|
end
|
50
69
|
end
|
51
70
|
|
@@ -56,7 +75,18 @@ module Typhoeus
|
|
56
75
|
#
|
57
76
|
# @return [ String ] The raw header.
|
58
77
|
def raw
|
59
|
-
@raw
|
78
|
+
@raw || ''
|
79
|
+
end
|
80
|
+
|
81
|
+
# Sets the default proc for the specified hash independent of the Ruby version.
|
82
|
+
#
|
83
|
+
# @return [ void ]
|
84
|
+
def set_default_proc_on(hash, default_proc)
|
85
|
+
if hash.respond_to?(:default_proc=)
|
86
|
+
hash.default_proc = default_proc
|
87
|
+
else
|
88
|
+
hash.replace(Hash.new(&default_proc).merge(hash))
|
89
|
+
end
|
60
90
|
end
|
61
91
|
end
|
62
92
|
end
|
data/lib/typhoeus/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: typhoeus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2013-01-11 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: ethon
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.5.
|
23
|
+
version: 0.5.7
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -28,7 +28,7 @@ dependencies:
|
|
28
28
|
requirements:
|
29
29
|
- - ~>
|
30
30
|
- !ruby/object:Gem::Version
|
31
|
-
version: 0.5.
|
31
|
+
version: 0.5.7
|
32
32
|
description: Like a modern code version of the mythical beast with 100 serpent heads,
|
33
33
|
Typhoeus runs HTTP requests in parallel while cleanly encapsulating handling logic.
|
34
34
|
email:
|
@@ -37,6 +37,9 @@ executables: []
|
|
37
37
|
extensions: []
|
38
38
|
extra_rdoc_files: []
|
39
39
|
files:
|
40
|
+
- lib/rack/typhoeus/middleware/params_decoder/helper.rb
|
41
|
+
- lib/rack/typhoeus/middleware/params_decoder.rb
|
42
|
+
- lib/rack/typhoeus.rb
|
40
43
|
- lib/typhoeus/adapters/faraday.rb
|
41
44
|
- lib/typhoeus/config.rb
|
42
45
|
- lib/typhoeus/errors/no_stub.rb
|
@@ -53,6 +56,7 @@ files:
|
|
53
56
|
- lib/typhoeus/hydra/runnable.rb
|
54
57
|
- lib/typhoeus/hydra/stubbable.rb
|
55
58
|
- lib/typhoeus/hydra.rb
|
59
|
+
- lib/typhoeus/railtie.rb
|
56
60
|
- lib/typhoeus/request/actions.rb
|
57
61
|
- lib/typhoeus/request/before.rb
|
58
62
|
- lib/typhoeus/request/block_connection.rb
|
@@ -88,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
92
|
version: '0'
|
89
93
|
segments:
|
90
94
|
- 0
|
91
|
-
hash:
|
95
|
+
hash: -4558354049498247324
|
92
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
97
|
none: false
|
94
98
|
requirements:
|