typhoeus 0.4.2 → 0.5.0.alpha

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.
Files changed (56) hide show
  1. data/CHANGELOG.md +86 -28
  2. data/Gemfile +17 -1
  3. data/README.md +20 -422
  4. data/Rakefile +21 -12
  5. data/lib/typhoeus.rb +58 -41
  6. data/lib/typhoeus/config.rb +14 -0
  7. data/lib/typhoeus/errors.rb +9 -0
  8. data/lib/typhoeus/errors/no_stub.rb +12 -0
  9. data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
  10. data/lib/typhoeus/expectation.rb +126 -0
  11. data/lib/typhoeus/hydra.rb +31 -236
  12. data/lib/typhoeus/hydras/block_connection.rb +33 -0
  13. data/lib/typhoeus/hydras/easy_factory.rb +67 -0
  14. data/lib/typhoeus/hydras/easy_pool.rb +40 -0
  15. data/lib/typhoeus/hydras/memoizable.rb +53 -0
  16. data/lib/typhoeus/hydras/queueable.rb +46 -0
  17. data/lib/typhoeus/hydras/runnable.rb +18 -0
  18. data/lib/typhoeus/hydras/stubbable.rb +27 -0
  19. data/lib/typhoeus/request.rb +68 -243
  20. data/lib/typhoeus/requests/actions.rb +101 -0
  21. data/lib/typhoeus/requests/block_connection.rb +31 -0
  22. data/lib/typhoeus/requests/callbacks.rb +82 -0
  23. data/lib/typhoeus/requests/marshal.rb +21 -0
  24. data/lib/typhoeus/requests/memoizable.rb +36 -0
  25. data/lib/typhoeus/requests/operations.rb +52 -0
  26. data/lib/typhoeus/requests/responseable.rb +29 -0
  27. data/lib/typhoeus/requests/stubbable.rb +29 -0
  28. data/lib/typhoeus/response.rb +24 -118
  29. data/lib/typhoeus/responses/header.rb +50 -0
  30. data/lib/typhoeus/responses/informations.rb +43 -0
  31. data/lib/typhoeus/responses/legacy.rb +27 -0
  32. data/lib/typhoeus/responses/status.rb +78 -0
  33. data/lib/typhoeus/version.rb +3 -1
  34. metadata +34 -141
  35. data/lib/typhoeus/curl.rb +0 -453
  36. data/lib/typhoeus/easy.rb +0 -115
  37. data/lib/typhoeus/easy/auth.rb +0 -14
  38. data/lib/typhoeus/easy/callbacks.rb +0 -33
  39. data/lib/typhoeus/easy/ffi_helper.rb +0 -61
  40. data/lib/typhoeus/easy/infos.rb +0 -90
  41. data/lib/typhoeus/easy/options.rb +0 -115
  42. data/lib/typhoeus/easy/proxy.rb +0 -20
  43. data/lib/typhoeus/easy/ssl.rb +0 -82
  44. data/lib/typhoeus/filter.rb +0 -28
  45. data/lib/typhoeus/form.rb +0 -61
  46. data/lib/typhoeus/header.rb +0 -54
  47. data/lib/typhoeus/hydra/callbacks.rb +0 -24
  48. data/lib/typhoeus/hydra/connect_options.rb +0 -61
  49. data/lib/typhoeus/hydra/stubbing.rb +0 -68
  50. data/lib/typhoeus/hydra_mock.rb +0 -131
  51. data/lib/typhoeus/multi.rb +0 -146
  52. data/lib/typhoeus/param_processor.rb +0 -43
  53. data/lib/typhoeus/remote.rb +0 -306
  54. data/lib/typhoeus/remote_method.rb +0 -108
  55. data/lib/typhoeus/remote_proxy_object.rb +0 -50
  56. 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
@@ -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