typhoeus 0.4.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +4 -0
- data/.travis.yml +26 -0
- data/CHANGELOG.md +341 -28
- data/CONTRIBUTING.md +20 -0
- data/Gemfile +31 -2
- data/Guardfile +9 -0
- data/LICENSE +1 -1
- data/README.md +486 -357
- data/Rakefile +21 -12
- data/UPGRADE.md +55 -0
- data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +76 -0
- data/lib/rack/typhoeus/middleware/params_decoder.rb +57 -0
- data/lib/rack/typhoeus.rb +1 -0
- data/lib/typhoeus/adapters/faraday.rb +180 -0
- data/lib/typhoeus/cache/dalli.rb +28 -0
- data/lib/typhoeus/cache/rails.rb +28 -0
- data/lib/typhoeus/cache/redis.rb +35 -0
- data/lib/typhoeus/config.rb +69 -0
- data/lib/typhoeus/easy_factory.rb +180 -0
- data/lib/typhoeus/errors/no_stub.rb +12 -0
- data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
- data/lib/typhoeus/errors.rb +9 -0
- data/lib/typhoeus/expectation.rb +217 -0
- data/lib/typhoeus/hydra/addable.rb +23 -0
- data/lib/typhoeus/hydra/before.rb +31 -0
- data/lib/typhoeus/hydra/block_connection.rb +35 -0
- data/lib/typhoeus/hydra/cacheable.rb +15 -0
- data/lib/typhoeus/hydra/memoizable.rb +56 -0
- data/lib/typhoeus/hydra/queueable.rb +83 -0
- data/lib/typhoeus/hydra/runnable.rb +19 -0
- data/lib/typhoeus/hydra/stubbable.rb +28 -0
- data/lib/typhoeus/hydra.rb +84 -236
- data/lib/typhoeus/pool.rb +70 -0
- data/lib/typhoeus/railtie.rb +12 -0
- data/lib/typhoeus/request/actions.rb +125 -0
- data/lib/typhoeus/request/before.rb +30 -0
- data/lib/typhoeus/request/block_connection.rb +52 -0
- data/lib/typhoeus/request/cacheable.rb +38 -0
- data/lib/typhoeus/request/callbacks.rb +151 -0
- data/lib/typhoeus/request/marshal.rb +22 -0
- data/lib/typhoeus/request/memoizable.rb +38 -0
- data/lib/typhoeus/request/operations.rb +40 -0
- data/lib/typhoeus/request/responseable.rb +29 -0
- data/lib/typhoeus/request/streamable.rb +34 -0
- data/lib/typhoeus/request/stubbable.rb +30 -0
- data/lib/typhoeus/request.rb +186 -231
- data/lib/typhoeus/response/cacheable.rb +14 -0
- data/lib/typhoeus/response/header.rb +105 -0
- data/lib/typhoeus/response/informations.rb +248 -0
- data/lib/typhoeus/response/status.rb +106 -0
- data/lib/typhoeus/response.rb +60 -115
- data/lib/typhoeus/version.rb +3 -1
- data/lib/typhoeus.rb +126 -39
- data/perf/profile.rb +14 -0
- data/perf/vs_nethttp.rb +64 -0
- data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +156 -0
- data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/localhost_server.rb +94 -0
- data/spec/support/memory_cache.rb +15 -0
- data/spec/support/server.rb +116 -0
- data/spec/typhoeus/adapters/faraday_spec.rb +339 -0
- data/spec/typhoeus/cache/dalli_spec.rb +41 -0
- data/spec/typhoeus/cache/redis_spec.rb +41 -0
- data/spec/typhoeus/config_spec.rb +15 -0
- data/spec/typhoeus/easy_factory_spec.rb +143 -0
- data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
- data/spec/typhoeus/expectation_spec.rb +280 -0
- data/spec/typhoeus/hydra/addable_spec.rb +22 -0
- data/spec/typhoeus/hydra/before_spec.rb +98 -0
- data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
- data/spec/typhoeus/hydra/cacheable_spec.rb +88 -0
- data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
- data/spec/typhoeus/hydra/queueable_spec.rb +98 -0
- data/spec/typhoeus/hydra/runnable_spec.rb +137 -0
- data/spec/typhoeus/hydra/stubbable_spec.rb +48 -0
- data/spec/typhoeus/hydra_spec.rb +22 -0
- data/spec/typhoeus/pool_spec.rb +137 -0
- data/spec/typhoeus/request/actions_spec.rb +19 -0
- data/spec/typhoeus/request/before_spec.rb +93 -0
- data/spec/typhoeus/request/block_connection_spec.rb +75 -0
- data/spec/typhoeus/request/cacheable_spec.rb +94 -0
- data/spec/typhoeus/request/callbacks_spec.rb +91 -0
- data/spec/typhoeus/request/marshal_spec.rb +60 -0
- data/spec/typhoeus/request/memoizable_spec.rb +34 -0
- data/spec/typhoeus/request/operations_spec.rb +101 -0
- data/spec/typhoeus/request/responseable_spec.rb +13 -0
- data/spec/typhoeus/request/stubbable_spec.rb +45 -0
- data/spec/typhoeus/request_spec.rb +232 -0
- data/spec/typhoeus/response/header_spec.rb +147 -0
- data/spec/typhoeus/response/informations_spec.rb +283 -0
- data/spec/typhoeus/response/status_spec.rb +256 -0
- data/spec/typhoeus/response_spec.rb +100 -0
- data/spec/typhoeus_spec.rb +105 -0
- data/typhoeus.gemspec +25 -0
- metadata +146 -158
- data/lib/typhoeus/curl.rb +0 -453
- data/lib/typhoeus/easy/auth.rb +0 -14
- data/lib/typhoeus/easy/callbacks.rb +0 -33
- data/lib/typhoeus/easy/ffi_helper.rb +0 -61
- data/lib/typhoeus/easy/infos.rb +0 -90
- data/lib/typhoeus/easy/options.rb +0 -115
- data/lib/typhoeus/easy/proxy.rb +0 -20
- data/lib/typhoeus/easy/ssl.rb +0 -82
- data/lib/typhoeus/easy.rb +0 -115
- data/lib/typhoeus/filter.rb +0 -28
- data/lib/typhoeus/form.rb +0 -61
- data/lib/typhoeus/header.rb +0 -54
- data/lib/typhoeus/hydra/callbacks.rb +0 -24
- data/lib/typhoeus/hydra/connect_options.rb +0 -61
- data/lib/typhoeus/hydra/stubbing.rb +0 -68
- data/lib/typhoeus/hydra_mock.rb +0 -131
- data/lib/typhoeus/multi.rb +0 -146
- data/lib/typhoeus/param_processor.rb +0 -43
- data/lib/typhoeus/remote.rb +0 -306
- data/lib/typhoeus/remote_method.rb +0 -108
- data/lib/typhoeus/remote_proxy_object.rb +0 -50
- data/lib/typhoeus/utils.rb +0 -50
@@ -1,108 +0,0 @@
|
|
1
|
-
module Typhoeus
|
2
|
-
class RemoteMethod
|
3
|
-
attr_accessor :http_method, :options, :base_uri, :path, :on_success, :on_failure, :cache_ttl
|
4
|
-
|
5
|
-
def initialize(options = {})
|
6
|
-
@http_method = options.delete(:method) || :get
|
7
|
-
@options = options
|
8
|
-
@base_uri = options.delete(:base_uri)
|
9
|
-
@path = options.delete(:path)
|
10
|
-
@on_success = options[:on_success]
|
11
|
-
@on_failure = options[:on_failure]
|
12
|
-
@cache_responses = options.delete(:cache_responses)
|
13
|
-
@memoize_responses = options.delete(:memoize_responses) || @cache_responses
|
14
|
-
@cache_ttl = @cache_responses == true ? 0 : @cache_responses
|
15
|
-
@keys = nil
|
16
|
-
|
17
|
-
clear_cache
|
18
|
-
end
|
19
|
-
|
20
|
-
def cache_responses?
|
21
|
-
@cache_responses
|
22
|
-
end
|
23
|
-
|
24
|
-
def memoize_responses?
|
25
|
-
@memoize_responses
|
26
|
-
end
|
27
|
-
|
28
|
-
def args_options_key(args, options)
|
29
|
-
"#{args.to_s}+#{options.to_s}"
|
30
|
-
end
|
31
|
-
|
32
|
-
def calling(args, options)
|
33
|
-
@called_methods[args_options_key(args, options)] = true
|
34
|
-
end
|
35
|
-
|
36
|
-
def already_called?(args, options)
|
37
|
-
@called_methods.has_key? args_options_key(args, options)
|
38
|
-
end
|
39
|
-
|
40
|
-
def add_response_block(block, args, options)
|
41
|
-
@response_blocks[args_options_key(args, options)] << block
|
42
|
-
end
|
43
|
-
|
44
|
-
def call_response_blocks(result, args, options)
|
45
|
-
key = args_options_key(args, options)
|
46
|
-
@response_blocks[key].each {|block| block.call(result)}
|
47
|
-
@response_blocks.delete(key)
|
48
|
-
@called_methods.delete(key)
|
49
|
-
end
|
50
|
-
|
51
|
-
def clear_cache
|
52
|
-
@response_blocks = Hash.new {|h, k| h[k] = []}
|
53
|
-
@called_methods = {}
|
54
|
-
end
|
55
|
-
|
56
|
-
def merge_options(new_options)
|
57
|
-
merged = options.merge(new_options)
|
58
|
-
if options.has_key?(:params) && new_options.has_key?(:params)
|
59
|
-
merged[:params] = options[:params].merge(new_options[:params])
|
60
|
-
end
|
61
|
-
argument_names.each {|a| merged.delete(a)}
|
62
|
-
merged.delete(:on_success) if merged[:on_success].nil?
|
63
|
-
merged.delete(:on_failure) if merged[:on_failure].nil?
|
64
|
-
merged
|
65
|
-
end
|
66
|
-
|
67
|
-
def interpolate_path_with_arguments(args)
|
68
|
-
interpolated_path = @path
|
69
|
-
argument_names.each do |arg|
|
70
|
-
interpolated_path = interpolated_path.gsub(":#{arg}", args[arg].to_s)
|
71
|
-
end
|
72
|
-
interpolated_path
|
73
|
-
end
|
74
|
-
|
75
|
-
def argument_names
|
76
|
-
return @keys if @keys
|
77
|
-
pattern, keys = compile(@path)
|
78
|
-
@keys = keys.collect {|k| k.to_sym}
|
79
|
-
end
|
80
|
-
|
81
|
-
# rippped from Sinatra. clean up stuff we don't need later
|
82
|
-
def compile(path)
|
83
|
-
path ||= ""
|
84
|
-
keys = []
|
85
|
-
if path.respond_to? :to_str
|
86
|
-
special_chars = %w{. + ( )}
|
87
|
-
pattern =
|
88
|
-
path.gsub(/((:\w+)|[\*#{special_chars.join}])/) do |match|
|
89
|
-
case match
|
90
|
-
when "*"
|
91
|
-
keys << 'splat'
|
92
|
-
"(.*?)"
|
93
|
-
when *special_chars
|
94
|
-
Regexp.escape(match)
|
95
|
-
else
|
96
|
-
keys << $2[1..-1]
|
97
|
-
"([^/?&#]+)"
|
98
|
-
end
|
99
|
-
end
|
100
|
-
[/^#{pattern}$/, keys]
|
101
|
-
elsif path.respond_to? :match
|
102
|
-
[path, keys]
|
103
|
-
else
|
104
|
-
raise TypeError, path
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module Typhoeus
|
2
|
-
class RemoteProxyObject
|
3
|
-
instance_methods.each { |m| undef_method m unless m =~ /^__|object_id/ }
|
4
|
-
|
5
|
-
def initialize(clear_memoized_store_proc, easy, options = {})
|
6
|
-
@clear_memoized_store_proc = clear_memoized_store_proc
|
7
|
-
@easy = easy
|
8
|
-
@success = options[:on_success]
|
9
|
-
@failure = options[:on_failure]
|
10
|
-
@cache = options.delete(:cache)
|
11
|
-
@cache_key = options.delete(:cache_key)
|
12
|
-
@timeout = options.delete(:cache_timeout)
|
13
|
-
Typhoeus.add_easy_request(@easy)
|
14
|
-
end
|
15
|
-
|
16
|
-
def method_missing(sym, *args, &block)
|
17
|
-
unless @proxied_object
|
18
|
-
if @cache && @cache_key
|
19
|
-
@proxied_object = @cache.get(@cache_key) rescue nil
|
20
|
-
end
|
21
|
-
|
22
|
-
unless @proxied_object
|
23
|
-
Typhoeus.perform_easy_requests
|
24
|
-
response = Response.new(:code => @easy.response_code,
|
25
|
-
:curl_return_code => @easy.curl_return_code,
|
26
|
-
:curl_error_message => @easy.curl_error_message,
|
27
|
-
:headers => @easy.response_header,
|
28
|
-
:body => @easy.response_body,
|
29
|
-
:time => @easy.total_time_taken,
|
30
|
-
:requested_url => @easy.url,
|
31
|
-
:requested_http_method => @easy.method,
|
32
|
-
:start_time => @easy.start_time)
|
33
|
-
if @easy.response_code >= 200 && @easy.response_code < 300
|
34
|
-
Typhoeus.release_easy_object(@easy)
|
35
|
-
@proxied_object = @success.nil? ? response : @success.call(response)
|
36
|
-
|
37
|
-
if @cache && @cache_key
|
38
|
-
@cache.set(@cache_key, @proxied_object, @timeout)
|
39
|
-
end
|
40
|
-
else
|
41
|
-
@proxied_object = @failure.nil? ? response : @failure.call(response)
|
42
|
-
end
|
43
|
-
@clear_memoized_store_proc.call
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
@proxied_object.__send__(sym, *args, &block)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
data/lib/typhoeus/utils.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
module Typhoeus
|
2
|
-
module Utils
|
3
|
-
# Taken from Rack::Utils, 1.2.1 to remove Rack dependency.
|
4
|
-
def escape(s)
|
5
|
-
s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/u) {
|
6
|
-
'%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
|
7
|
-
}.tr(' ', '+')
|
8
|
-
end
|
9
|
-
module_function :escape
|
10
|
-
|
11
|
-
# Params are NOT escaped.
|
12
|
-
def traverse_params_hash(hash, result = nil, current_key = nil)
|
13
|
-
result = ParamProcessor.traverse_params_hash hash, result, current_key
|
14
|
-
end
|
15
|
-
module_function :traverse_params_hash
|
16
|
-
|
17
|
-
def traversal_to_param_string(traversal, escape = true)
|
18
|
-
traversal[:params].collect { |param|
|
19
|
-
escape ? "#{Typhoeus::Utils.escape(param[0])}=#{Typhoeus::Utils.escape(param[1])}" : "#{param[0]}=#{param[1]}"
|
20
|
-
}.join('&')
|
21
|
-
end
|
22
|
-
module_function :traversal_to_param_string
|
23
|
-
|
24
|
-
# Return the bytesize of String; uses String#size under Ruby 1.8 and
|
25
|
-
# String#bytesize under 1.9.
|
26
|
-
if ''.respond_to?(:bytesize)
|
27
|
-
def bytesize(string)
|
28
|
-
string.bytesize
|
29
|
-
end
|
30
|
-
else
|
31
|
-
def bytesize(string)
|
32
|
-
string.size
|
33
|
-
end
|
34
|
-
end
|
35
|
-
module_function :bytesize
|
36
|
-
|
37
|
-
# Return a byteslice from a string; uses String#[] under Ruby 1.8 and
|
38
|
-
# String#byteslice under 1.9.
|
39
|
-
if ''.respond_to?(:byteslice)
|
40
|
-
def byteslice(string, *args)
|
41
|
-
string.byteslice(*args)
|
42
|
-
end
|
43
|
-
else
|
44
|
-
def byteslice(string, *args)
|
45
|
-
string[*args]
|
46
|
-
end
|
47
|
-
end
|
48
|
-
module_function :byteslice
|
49
|
-
end
|
50
|
-
end
|