typhoeus 0.4.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +26 -0
  5. data/CHANGELOG.md +341 -28
  6. data/CONTRIBUTING.md +20 -0
  7. data/Gemfile +31 -2
  8. data/Guardfile +9 -0
  9. data/LICENSE +1 -1
  10. data/README.md +486 -357
  11. data/Rakefile +21 -12
  12. data/UPGRADE.md +55 -0
  13. data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +76 -0
  14. data/lib/rack/typhoeus/middleware/params_decoder.rb +57 -0
  15. data/lib/rack/typhoeus.rb +1 -0
  16. data/lib/typhoeus/adapters/faraday.rb +180 -0
  17. data/lib/typhoeus/cache/dalli.rb +28 -0
  18. data/lib/typhoeus/cache/rails.rb +28 -0
  19. data/lib/typhoeus/cache/redis.rb +35 -0
  20. data/lib/typhoeus/config.rb +69 -0
  21. data/lib/typhoeus/easy_factory.rb +180 -0
  22. data/lib/typhoeus/errors/no_stub.rb +12 -0
  23. data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
  24. data/lib/typhoeus/errors.rb +9 -0
  25. data/lib/typhoeus/expectation.rb +217 -0
  26. data/lib/typhoeus/hydra/addable.rb +23 -0
  27. data/lib/typhoeus/hydra/before.rb +31 -0
  28. data/lib/typhoeus/hydra/block_connection.rb +35 -0
  29. data/lib/typhoeus/hydra/cacheable.rb +15 -0
  30. data/lib/typhoeus/hydra/memoizable.rb +56 -0
  31. data/lib/typhoeus/hydra/queueable.rb +83 -0
  32. data/lib/typhoeus/hydra/runnable.rb +19 -0
  33. data/lib/typhoeus/hydra/stubbable.rb +28 -0
  34. data/lib/typhoeus/hydra.rb +84 -236
  35. data/lib/typhoeus/pool.rb +70 -0
  36. data/lib/typhoeus/railtie.rb +12 -0
  37. data/lib/typhoeus/request/actions.rb +125 -0
  38. data/lib/typhoeus/request/before.rb +30 -0
  39. data/lib/typhoeus/request/block_connection.rb +52 -0
  40. data/lib/typhoeus/request/cacheable.rb +38 -0
  41. data/lib/typhoeus/request/callbacks.rb +151 -0
  42. data/lib/typhoeus/request/marshal.rb +22 -0
  43. data/lib/typhoeus/request/memoizable.rb +38 -0
  44. data/lib/typhoeus/request/operations.rb +40 -0
  45. data/lib/typhoeus/request/responseable.rb +29 -0
  46. data/lib/typhoeus/request/streamable.rb +34 -0
  47. data/lib/typhoeus/request/stubbable.rb +30 -0
  48. data/lib/typhoeus/request.rb +186 -231
  49. data/lib/typhoeus/response/cacheable.rb +14 -0
  50. data/lib/typhoeus/response/header.rb +105 -0
  51. data/lib/typhoeus/response/informations.rb +248 -0
  52. data/lib/typhoeus/response/status.rb +106 -0
  53. data/lib/typhoeus/response.rb +60 -115
  54. data/lib/typhoeus/version.rb +3 -1
  55. data/lib/typhoeus.rb +126 -39
  56. data/perf/profile.rb +14 -0
  57. data/perf/vs_nethttp.rb +64 -0
  58. data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +156 -0
  59. data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
  60. data/spec/spec_helper.rb +29 -0
  61. data/spec/support/localhost_server.rb +94 -0
  62. data/spec/support/memory_cache.rb +15 -0
  63. data/spec/support/server.rb +116 -0
  64. data/spec/typhoeus/adapters/faraday_spec.rb +339 -0
  65. data/spec/typhoeus/cache/dalli_spec.rb +41 -0
  66. data/spec/typhoeus/cache/redis_spec.rb +41 -0
  67. data/spec/typhoeus/config_spec.rb +15 -0
  68. data/spec/typhoeus/easy_factory_spec.rb +143 -0
  69. data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
  70. data/spec/typhoeus/expectation_spec.rb +280 -0
  71. data/spec/typhoeus/hydra/addable_spec.rb +22 -0
  72. data/spec/typhoeus/hydra/before_spec.rb +98 -0
  73. data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
  74. data/spec/typhoeus/hydra/cacheable_spec.rb +88 -0
  75. data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
  76. data/spec/typhoeus/hydra/queueable_spec.rb +98 -0
  77. data/spec/typhoeus/hydra/runnable_spec.rb +137 -0
  78. data/spec/typhoeus/hydra/stubbable_spec.rb +48 -0
  79. data/spec/typhoeus/hydra_spec.rb +22 -0
  80. data/spec/typhoeus/pool_spec.rb +137 -0
  81. data/spec/typhoeus/request/actions_spec.rb +19 -0
  82. data/spec/typhoeus/request/before_spec.rb +93 -0
  83. data/spec/typhoeus/request/block_connection_spec.rb +75 -0
  84. data/spec/typhoeus/request/cacheable_spec.rb +94 -0
  85. data/spec/typhoeus/request/callbacks_spec.rb +91 -0
  86. data/spec/typhoeus/request/marshal_spec.rb +60 -0
  87. data/spec/typhoeus/request/memoizable_spec.rb +34 -0
  88. data/spec/typhoeus/request/operations_spec.rb +101 -0
  89. data/spec/typhoeus/request/responseable_spec.rb +13 -0
  90. data/spec/typhoeus/request/stubbable_spec.rb +45 -0
  91. data/spec/typhoeus/request_spec.rb +232 -0
  92. data/spec/typhoeus/response/header_spec.rb +147 -0
  93. data/spec/typhoeus/response/informations_spec.rb +283 -0
  94. data/spec/typhoeus/response/status_spec.rb +256 -0
  95. data/spec/typhoeus/response_spec.rb +100 -0
  96. data/spec/typhoeus_spec.rb +105 -0
  97. data/typhoeus.gemspec +25 -0
  98. metadata +146 -158
  99. data/lib/typhoeus/curl.rb +0 -453
  100. data/lib/typhoeus/easy/auth.rb +0 -14
  101. data/lib/typhoeus/easy/callbacks.rb +0 -33
  102. data/lib/typhoeus/easy/ffi_helper.rb +0 -61
  103. data/lib/typhoeus/easy/infos.rb +0 -90
  104. data/lib/typhoeus/easy/options.rb +0 -115
  105. data/lib/typhoeus/easy/proxy.rb +0 -20
  106. data/lib/typhoeus/easy/ssl.rb +0 -82
  107. data/lib/typhoeus/easy.rb +0 -115
  108. data/lib/typhoeus/filter.rb +0 -28
  109. data/lib/typhoeus/form.rb +0 -61
  110. data/lib/typhoeus/header.rb +0 -54
  111. data/lib/typhoeus/hydra/callbacks.rb +0 -24
  112. data/lib/typhoeus/hydra/connect_options.rb +0 -61
  113. data/lib/typhoeus/hydra/stubbing.rb +0 -68
  114. data/lib/typhoeus/hydra_mock.rb +0 -131
  115. data/lib/typhoeus/multi.rb +0 -146
  116. data/lib/typhoeus/param_processor.rb +0 -43
  117. data/lib/typhoeus/remote.rb +0 -306
  118. data/lib/typhoeus/remote_method.rb +0 -108
  119. data/lib/typhoeus/remote_proxy_object.rb +0 -50
  120. data/lib/typhoeus/utils.rb +0 -50
data/Rakefile CHANGED
@@ -1,13 +1,18 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
1
+ require "bundler"
2
+ Bundler.setup
2
3
 
3
- require 'rspec/core/rake_task'
4
- RSpec::Core::RakeTask.new do |t|
4
+ require "rake"
5
+ require "rspec/core/rake_task"
6
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
7
+ require "typhoeus/version"
8
+
9
+ task :gem => :build
10
+ task :build do
11
+ system "gem build typhoeus.gemspec"
5
12
  end
6
13
 
7
- task :install do
8
- rm_rf "*.gem"
9
- puts `gem build typhoeus.gemspec`
10
- puts `gem install typhoeus-*.gem`
14
+ task :install => :build do
15
+ system "gem install typhoeus-#{Typhoeus::VERSION}.gem"
11
16
  end
12
17
 
13
18
  task :release => :build do
@@ -16,14 +21,18 @@ task :release => :build do
16
21
  system "gem push typhoeus-#{Typhoeus::VERSION}.gem"
17
22
  end
18
23
 
24
+ RSpec::Core::RakeTask.new(:spec) do |t|
25
+ t.verbose = false
26
+ t.ruby_opts = "-W -I./spec -rspec_helper"
27
+ end
28
+
19
29
  desc "Start up the test servers"
20
- task :start_test_servers do
21
- require 'spec/support/typhoeus_localhost_server'
30
+ task :start do
31
+ require_relative 'spec/support/boot'
22
32
  begin
23
- TyphoeusLocalhostServer.start_servers!(:rake)
33
+ Boot.start_servers(:rake)
24
34
  rescue Exception
25
35
  end
26
36
  end
27
37
 
28
- desc "Build Typhoeus and run all the tests."
29
- task :default => [:spec]
38
+ task :default => :spec
data/UPGRADE.md ADDED
@@ -0,0 +1,55 @@
1
+ # Upgrade guide
2
+
3
+ ## 0.5
4
+
5
+ ### Options
6
+
7
+ Fix the option names, because some were renamed. The errors should point you in the right direction:
8
+
9
+ ```ruby
10
+ Typhoeus.get("www.example.com", follow_location: true)
11
+ # Ethon::Errors::InvalidOption: The option: follow_location is invalid.
12
+ # Please try followlocation instead of follow_location.
13
+ # ... [Backtrace]
14
+
15
+ Typhoeus.get("www.example.com", followlocation: true).code
16
+ #=> 200
17
+ ```
18
+
19
+ ### Headers
20
+
21
+ `Response#headers` returns a hash now and replaces `Response#headers_hash`, use `Response#response_headers` for the raw string:
22
+
23
+ ```ruby
24
+ Typhoeus.get("www.example.com", followlocation: true).headers
25
+ #=> {
26
+ # "date"=>"Tue, 06 Nov 2012 09:07:27 GMT",
27
+ # "server"=>"Apache/2.2.3 (CentOS)",
28
+ # "last-modified"=>"Wed, 09 Feb 2011 17:13:15 GMT",
29
+ # "vary"=>"Accept-Encoding",
30
+ # "connection"=>"close",
31
+ # "content-type"=>"text/html; charset=UTF-8"
32
+ # }
33
+
34
+ Typhoeus.get("www.example.com", followlocation: true).response_headers
35
+ #=> "HTTP/1.0 302 Found\r\nLocation: http://www.iana.org/domains/example/ [...]"
36
+ ```
37
+
38
+ ### Params vs body
39
+
40
+ Make sure every request sends proper params and body (especially POST/PUT). `:params` becomes url parameter and `:body` request body. Before params for POST was smashed into the body.
41
+
42
+ ### Configuration
43
+
44
+ Create a global configuration in case you want to turn on verbose, memoize or block_connection:
45
+
46
+ ```ruby
47
+ Typhoeus.configure do |config|
48
+ config.verbose = true
49
+ config.memoize = true
50
+ end
51
+ ```
52
+
53
+ ### Docs
54
+
55
+ When in doubt, read the [docs](http://rubydoc.info/github/typhoeus/typhoeus/frames/Typhoeus) or the [code](https://www.github.com/typhoeus).
@@ -0,0 +1,76 @@
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
+ if hash.keys.size > 1
52
+ keys = hash.keys.map{|i| i.to_i if i.respond_to?(:to_i)}.sort
53
+ keys == hash.keys.size.times.to_a
54
+ else
55
+ hash.keys.first =~ /0/
56
+ end
57
+ end
58
+
59
+ # If the Hash is an array encoded by typhoeus an array is returned
60
+ # else the self is returned
61
+ #
62
+ # @param hash [Hash] The Hash to convert into an Array.
63
+ #
64
+ # @return [Arraya/Hash]
65
+ def convert(hash)
66
+ if encoded?(hash)
67
+ hash.sort{ |a, b| a[0].to_i <=> b[0].to_i }.map{ |key, value| value }
68
+ else
69
+ hash
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -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#L243
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 @@
1
+ require "rack/typhoeus/middleware/params_decoder"
@@ -0,0 +1,180 @@
1
+ require 'faraday'
2
+
3
+ module Faraday # :nodoc:
4
+ class Adapter # :nodoc:
5
+
6
+ # Adapter to use Faraday with Typhoeus.
7
+ #
8
+ # @example Use Typhoeus.
9
+ # require 'faraday'
10
+ # require 'typhoeus'
11
+ # require 'typhoeus/adapters/faraday'
12
+ #
13
+ # conn = Faraday.new(url: "www.example.com") do |faraday|
14
+ # faraday.adapter :typhoeus
15
+ #
16
+ # # You can include Typhoeus options to be used for every request
17
+ # # faraday.adapter :typhoeus, forbid_reuse: true, maxredirs: 1
18
+ # end
19
+ #
20
+ # response = conn.get("/")
21
+ class Typhoeus < Faraday::Adapter
22
+ self.supports_parallel = true
23
+
24
+ (class << self; self; end).instance_eval do
25
+ remove_method :setup_parallel_manager if method_defined? :setup_parallel_manager
26
+ end
27
+
28
+ remove_method :call if method_defined? :call
29
+ remove_method :perform_request if method_defined? :perform_request
30
+ remove_method :request if method_defined? :request
31
+ remove_method :read_body if method_defined? :read_body
32
+ remove_method :configure_ssl if method_defined? :configure_ssl
33
+ remove_method :configure_proxy if method_defined? :configure_proxy
34
+ remove_method :configure_timeout if method_defined? :configure_timeout
35
+ remove_method :configure_socket if method_defined? :configure_socket
36
+ remove_method :parallel? if method_defined? :parallel?
37
+
38
+ # Initialize the Typhoeus adapter
39
+ #
40
+ # @param [ App ] app Farday app
41
+ # @option [ Hash ] adapter_options Typhoeus options
42
+ #
43
+ # @return [ void ]
44
+ def initialize(app, adapter_options = {})
45
+ super(app)
46
+ @adapter_options = adapter_options
47
+ end
48
+
49
+ # Setup Hydra with provided options.
50
+ #
51
+ # @example Setup Hydra.
52
+ # Faraday::Adapter::Typhoeus.setup_parallel_manager
53
+ # #=> #<Typhoeus::Hydra ... >
54
+ #
55
+ # @param (see Typhoeus::Hydra#initialize)
56
+ # @option (see Typhoeus::Hydra#initialize)
57
+ #
58
+ # @return [ Typhoeus::Hydra ] The hydra.
59
+ def self.setup_parallel_manager(options = {})
60
+ ::Typhoeus::Hydra.new(options)
61
+ end
62
+
63
+ dependency 'typhoeus'
64
+
65
+ # Hook into Faraday and perform the request with Typhoeus.
66
+ #
67
+ # @param [ Hash ] env The environment.
68
+ #
69
+ # @return [ void ]
70
+ def call(env)
71
+ super
72
+ perform_request env
73
+ @app.call env
74
+ end
75
+
76
+ private
77
+
78
+ def perform_request(env)
79
+ if parallel?(env)
80
+ env[:parallel_manager].queue request(env)
81
+ else
82
+ request(env).run
83
+ end
84
+ end
85
+
86
+ def request(env)
87
+ read_body env
88
+
89
+ req = typhoeus_request(env)
90
+
91
+ configure_ssl req, env
92
+ configure_proxy req, env
93
+ configure_timeout req, env
94
+ configure_socket req, env
95
+
96
+ req.on_complete do |resp|
97
+ if resp.timed_out?
98
+ env[:typhoeus_timed_out] = true
99
+ unless parallel?(env)
100
+ raise Faraday::TimeoutError, "request timed out"
101
+ end
102
+ elsif (resp.response_code == 0) || ((resp.return_code != :ok) && !resp.mock?)
103
+ env[:typhoeus_connection_failed] = true
104
+ env[:typhoeus_return_message] = resp.return_message
105
+ unless parallel?(env)
106
+ raise Faraday::ConnectionFailed, resp.return_message
107
+ end
108
+ end
109
+
110
+ save_response(env, resp.code, resp.body) do |response_headers|
111
+ response_headers.parse resp.response_headers
112
+ end
113
+ # in async mode, :response is initialized at this point
114
+ env[:response].finish(env) if parallel?(env)
115
+ end
116
+
117
+ req
118
+ end
119
+
120
+ def typhoeus_request(env)
121
+ opts = {
122
+ :method => env[:method],
123
+ :body => env[:body],
124
+ :headers => env[:request_headers]
125
+ }.merge(@adapter_options)
126
+
127
+ ::Typhoeus::Request.new(env[:url].to_s, opts)
128
+ end
129
+
130
+ def read_body(env)
131
+ env[:body] = env[:body].read if env[:body].respond_to? :read
132
+ end
133
+
134
+ def configure_ssl(req, env)
135
+ ssl = env[:ssl]
136
+
137
+ verify_p = (ssl && ssl.fetch(:verify, true))
138
+
139
+ ssl_verifyhost = verify_p ? 2 : 0
140
+ req.options[:ssl_verifyhost] = ssl_verifyhost
141
+ req.options[:ssl_verifypeer] = verify_p
142
+ req.options[:sslversion] = ssl[:version] if ssl[:version]
143
+ req.options[:sslcert] = ssl[:client_cert] if ssl[:client_cert]
144
+ req.options[:sslkey] = ssl[:client_key] if ssl[:client_key]
145
+ req.options[:cainfo] = ssl[:ca_file] if ssl[:ca_file]
146
+ req.options[:capath] = ssl[:ca_path] if ssl[:ca_path]
147
+ client_cert_passwd_key = [:client_cert_passwd, :client_certificate_password].detect { |name| ssl.key?(name) }
148
+ req.options[:keypasswd] = ssl[client_cert_passwd_key] if client_cert_passwd_key
149
+ end
150
+
151
+ def configure_proxy(req, env)
152
+ proxy = env[:request][:proxy]
153
+ return unless proxy
154
+
155
+ req.options[:proxy] = "#{proxy[:uri].scheme}://#{proxy[:uri].host}:#{proxy[:uri].port}"
156
+
157
+ if proxy[:user] && proxy[:password]
158
+ req.options[:proxyauth] = :any
159
+ req.options[:proxyuserpwd] = "#{proxy[:user]}:#{proxy[:password]}"
160
+ end
161
+ end
162
+
163
+ def configure_timeout(req, env)
164
+ env_req = env[:request]
165
+ req.options[:timeout_ms] = (env_req[:timeout] * 1000).to_i if env_req[:timeout]
166
+ req.options[:connecttimeout_ms] = (env_req[:open_timeout] * 1000).to_i if env_req[:open_timeout]
167
+ end
168
+
169
+ def configure_socket(req, env)
170
+ if bind = env[:request][:bind]
171
+ req.options[:interface] = bind[:host]
172
+ end
173
+ end
174
+
175
+ def parallel?(env)
176
+ !!env[:parallel_manager]
177
+ end
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,28 @@
1
+ module Typhoeus
2
+ module Cache
3
+ # This module provides a simple way to cache HTTP responses using Dalli.
4
+ class Dalli
5
+ # @example Set Dalli as the Typhoeus cache backend
6
+ # Typhoeus::Config.cache = Typhoeus::Cache::Dalli.new
7
+ #
8
+ # @param [ Dalli::Client ] client
9
+ # A connection to the cache server. Defaults to `Dalli::Client.new`
10
+ # @param [ Hash ] options
11
+ # Options
12
+ # @option options [ Integer ] :default_ttl
13
+ # The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
14
+ def initialize(client = ::Dalli::Client.new, options = {})
15
+ @client = client
16
+ @default_ttl = options[:default_ttl]
17
+ end
18
+
19
+ def get(request)
20
+ @client.get(request.cache_key)
21
+ end
22
+
23
+ def set(request, response)
24
+ @client.set(request.cache_key, response, request.cache_ttl || @default_ttl)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ module Typhoeus
2
+ module Cache
3
+ # This module provides a simple way to cache HTTP responses in using the Rails cache.
4
+ class Rails
5
+ # @example Use the Rails cache setup to cache Typhoeus responses.
6
+ # Typhoeus::Config.cache = Typhoeus::Cache::Rails.new
7
+ #
8
+ # @param [ ActiveSupport::Cache::Store ] cache
9
+ # A Rails cache backend. Defaults to Rails.cache.
10
+ # @param [ Hash ] options
11
+ # Options
12
+ # @option options [ Integer ] :default_ttl
13
+ # The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
14
+ def initialize(cache = ::Rails.cache, options = {})
15
+ @cache = cache
16
+ @default_ttl = options[:default_ttl]
17
+ end
18
+
19
+ def get(request)
20
+ @cache.read(request)
21
+ end
22
+
23
+ def set(request, response)
24
+ @cache.write(request.cache_key, response, :expires_in => request.cache_ttl || @default_ttl)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,35 @@
1
+ module Typhoeus
2
+ module Cache
3
+ # This module provides a simple way to cache HTTP responses in Redis.
4
+ class Redis
5
+ # @example Set Redis as the Typhoeus cache backend
6
+ # Typhoeus::Config.cache = Typhoeus::Cache::Redis.new
7
+ #
8
+ # @param [ Redis ] redis
9
+ # A connection to Redis. Defaults to `Redis.new`, which uses the
10
+ # `REDIS_URL` environment variable to connect
11
+ # @param [ Hash ] options
12
+ # Options
13
+ # @option options [ Integer ] :default_ttl
14
+ # The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
15
+ def initialize(redis = ::Redis.new, options = {})
16
+ @redis = redis
17
+ @default_ttl = options[:default_ttl]
18
+ end
19
+
20
+ def get(request)
21
+ serialized_response = @redis.get(request.cache_key)
22
+ return unless serialized_response
23
+ Marshal.load(serialized_response)
24
+ end
25
+
26
+ def set(request, response)
27
+ ttl = request.cache_ttl || @default_ttl
28
+ key = request.cache_key
29
+ serialized_response = Marshal.dump(response)
30
+ @redis.set(key, serialized_response)
31
+ @redis.expire(key, ttl) if ttl
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,69 @@
1
+ module Typhoeus
2
+
3
+ # The Typhoeus configuration used to set global
4
+ # options.
5
+ # @example Set the configuration options within a block.
6
+ # Typhoeus.configure do |config|
7
+ # config.verbose = true
8
+ # end
9
+ #
10
+ # @example Set the configuration directly.
11
+ # Typhoeus::Config.verbose = true
12
+ module Config
13
+ extend self
14
+
15
+ # Defines whether the connection is blocked.
16
+ # Defaults to false. When set to true, only
17
+ # stubbed requests are allowed. A
18
+ # {Typhoeus::Errors::NoStub} error is raised,
19
+ # when trying to do a real request. It's possible
20
+ # to work around inside
21
+ # {Typhoeus.with_connection}.
22
+ #
23
+ # @return [ Boolean ]
24
+ #
25
+ # @see Typhoeus::Request::BlockConnection
26
+ # @see Typhoeus::Hydra::BlockConnection
27
+ # @see Typhoeus#with_connection
28
+ # @see Typhoeus::Errors::NoStub
29
+ attr_accessor :block_connection
30
+
31
+ # Defines whether GET requests are memoized when using the {Typhoeus::Hydra}.
32
+ #
33
+ # @return [ Boolean ]
34
+ #
35
+ # @see Typhoeus::Hydra
36
+ # @see Typhoeus::Hydra::Memoizable
37
+ attr_accessor :memoize
38
+
39
+ # Defines whether curls debug output is shown.
40
+ # Unfortunately it prints to stderr.
41
+ #
42
+ # @return [ Boolean ]
43
+ #
44
+ # @see http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTVERBOSE
45
+ attr_accessor :verbose
46
+
47
+ # Defines whether requests are cached.
48
+ #
49
+ # @return [ Object ]
50
+ #
51
+ # @see Typhoeus::Hydra::Cacheable
52
+ # @see Typhoeus::Request::Cacheable
53
+ attr_accessor :cache
54
+
55
+ # Defines whether to use a default user agent.
56
+ #
57
+ # @return [ String ]
58
+ #
59
+ # @see Typhoeus::Request#set_defaults
60
+ attr_accessor :user_agent
61
+
62
+ # Defines wether to use a proxy server for every request.
63
+ #
64
+ # @return [ String ]
65
+ #
66
+ # @see Typhoeus::Request#set_defaults
67
+ attr_accessor :proxy
68
+ end
69
+ end