typhoeus 1.0.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +11 -8
  3. data/CHANGELOG.md +29 -1
  4. data/CONTRIBUTING.md +4 -0
  5. data/Gemfile +10 -3
  6. data/README.md +66 -41
  7. data/lib/typhoeus/adapters/faraday.rb +29 -9
  8. data/lib/typhoeus/cache/dalli.rb +28 -0
  9. data/lib/typhoeus/cache/rails.rb +28 -0
  10. data/lib/typhoeus/cache/redis.rb +35 -0
  11. data/lib/typhoeus/config.rb +8 -1
  12. data/lib/typhoeus/easy_factory.rb +7 -2
  13. data/lib/typhoeus/hydra/cacheable.rb +1 -1
  14. data/lib/typhoeus/request/actions.rb +7 -7
  15. data/lib/typhoeus/request/cacheable.rb +14 -3
  16. data/lib/typhoeus/request/callbacks.rb +21 -3
  17. data/lib/typhoeus/request/marshal.rb +2 -2
  18. data/lib/typhoeus/request/streamable.rb +1 -1
  19. data/lib/typhoeus/request.rb +2 -0
  20. data/lib/typhoeus/response/header.rb +12 -5
  21. data/lib/typhoeus/response/informations.rb +7 -3
  22. data/lib/typhoeus/response/status.rb +22 -2
  23. data/lib/typhoeus/response.rb +1 -1
  24. data/lib/typhoeus/version.rb +1 -1
  25. data/lib/typhoeus.rb +19 -3
  26. data/spec/typhoeus/adapters/faraday_spec.rb +237 -191
  27. data/spec/typhoeus/cache/dalli_spec.rb +41 -0
  28. data/spec/typhoeus/cache/redis_spec.rb +41 -0
  29. data/spec/typhoeus/config_spec.rb +1 -1
  30. data/spec/typhoeus/easy_factory_spec.rb +6 -0
  31. data/spec/typhoeus/hydra/cacheable_spec.rb +31 -1
  32. data/spec/typhoeus/pool_spec.rb +4 -2
  33. data/spec/typhoeus/request/cacheable_spec.rb +24 -0
  34. data/spec/typhoeus/request/callbacks_spec.rb +2 -2
  35. data/spec/typhoeus/request/marshal_spec.rb +1 -1
  36. data/spec/typhoeus/request_spec.rb +21 -3
  37. data/spec/typhoeus/response/header_spec.rb +51 -1
  38. data/spec/typhoeus/response/informations_spec.rb +12 -1
  39. data/spec/typhoeus/response/status_spec.rb +54 -0
  40. metadata +10 -3
@@ -10,7 +10,7 @@ module Typhoeus
10
10
  # Setting an on_body callback will cause the response body to be empty.
11
11
  #
12
12
  # @example Set on_body.
13
- # request.on_body { |response, body_chunk| puts "Got #{body_chunk.bytesize} bytes" }
13
+ # request.on_body { |body_chunk, response| puts "Got #{body_chunk.bytesize} bytes" }
14
14
  #
15
15
  # @param [ Block ] block The block to execute.
16
16
  #
@@ -212,8 +212,10 @@ module Typhoeus
212
212
  default_user_agent = Config.user_agent || Typhoeus::USER_AGENT
213
213
 
214
214
  options[:headers] = {'User-Agent' => default_user_agent}.merge(options[:headers] || {})
215
+ options[:headers]['Expect'] ||= ''
215
216
  options[:verbose] = Typhoeus::Config.verbose if options[:verbose].nil? && !Typhoeus::Config.verbose.nil?
216
217
  options[:maxredirs] ||= 50
218
+ options[:proxy] = Typhoeus::Config.proxy unless options.has_key?(:proxy) || Typhoeus::Config.proxy.nil?
217
219
  end
218
220
  end
219
221
  end
@@ -1,3 +1,5 @@
1
+ require 'delegate'
2
+
1
3
  module Typhoeus
2
4
  class Response
3
5
 
@@ -6,7 +8,7 @@ module Typhoeus
6
8
  # Values can be strings (normal case) or arrays of strings (for duplicates headers)
7
9
  #
8
10
  # @api private
9
- class Header < Hash
11
+ class Header < DelegateClass(Hash)
10
12
 
11
13
  # Create a new header.
12
14
  #
@@ -15,10 +17,14 @@ module Typhoeus
15
17
  #
16
18
  # @param [ String ] raw The raw header.
17
19
  def initialize(raw)
20
+ super({})
18
21
  @raw = raw
19
22
  @sanitized = {}
20
23
  parse
21
- set_default_proc_on(self, lambda { |h, k| @sanitized[k.to_s.downcase] })
24
+ end
25
+
26
+ def [](key)
27
+ fetch(key) { @sanitized[key.to_s.downcase] }
22
28
  end
23
29
 
24
30
  # Parses the raw header.
@@ -32,8 +38,9 @@ module Typhoeus
32
38
  process_pair(k, v)
33
39
  end
34
40
  when String
35
- raw.lines.each do |header|
36
- next if header.empty? || header.start_with?( 'HTTP/1.' )
41
+ raw.split(/\r?\n(?!\s)/).each do |header|
42
+ header.strip!
43
+ next if header.empty? || header.start_with?( 'HTTP/' )
37
44
  process_line(header)
38
45
  end
39
46
  end
@@ -46,7 +53,7 @@ module Typhoeus
46
53
  # @return [ void ]
47
54
  def process_line(header)
48
55
  key, value = header.split(':', 2)
49
- process_pair(key.strip, value.strip)
56
+ process_pair(key.strip, (value ? value.strip.gsub(/\r?\n\s*/, ' ') : ''))
50
57
  end
51
58
 
52
59
  # Sets key value pair for self and @sanitized.
@@ -47,9 +47,13 @@ module Typhoeus
47
47
  def response_headers
48
48
  return options[:response_headers] if options[:response_headers]
49
49
  if mock? && h = options[:headers]
50
- h.map{ |k,v| [k, v.respond_to?(:join) ? v.join : v] }.
51
- map{ |e| "#{e.first}: #{e.last}" }.
52
- join("\r\n")
50
+ status_code = return_code || "200"
51
+ reason_phrase = status_code == "200" ? "OK" : "Mock Reason Phrase"
52
+ status_line = "HTTP/1.1 #{status_code} #{reason_phrase}"
53
+ actual_headers = h.map{ |k,v| [k, v.respond_to?(:join) ? v.join(',') : v] }.
54
+ map{ |e| "#{e.first}: #{e.last}" }
55
+
56
+ [status_line, *actual_headers].join("\r\n")
53
57
  end
54
58
  end
55
59
 
@@ -39,14 +39,24 @@ module Typhoeus
39
39
  @http_version ||= first_header_line ? first_header_line[/HTTP\/(\S+)/, 1] : nil
40
40
  end
41
41
 
42
- # Return wether the response is a success.
42
+ # Return whether the response is a success.
43
43
  #
44
44
  # @example Return if the response was successful.
45
45
  # response.success?
46
46
  #
47
47
  # @return [ Boolean ] Return true if successful, false else.
48
48
  def success?
49
- (mock || return_code == :ok) && response_code && response_code >= 200 && response_code < 300
49
+ (mock || return_code == :ok) && response_code && has_good_response_code?
50
+ end
51
+
52
+ # Return whether the response is a failure.
53
+ #
54
+ # @example Return if the response was failed.
55
+ # response.failure?
56
+ #
57
+ # @return [ Boolean ] Return true if failure, false else.
58
+ def failure?
59
+ (mock || return_code == :internal_server_error) && response_code && has_bad_response_code?
50
60
  end
51
61
 
52
62
  # Return wether the response is modified.
@@ -81,6 +91,16 @@ module Typhoeus
81
91
  end
82
92
  end
83
93
  end
94
+
95
+ # :nodoc:
96
+ def has_good_response_code?
97
+ response_code >= 200 && response_code < 300
98
+ end
99
+
100
+ # :nodoc:
101
+ def has_bad_response_code?
102
+ !has_good_response_code?
103
+ end
84
104
  end
85
105
  end
86
106
  end
@@ -14,7 +14,7 @@ module Typhoeus
14
14
  # Remembers the corresponding request.
15
15
  #
16
16
  # @example Get request.
17
- # request = Typhoeus::Request.get("www.example.com")
17
+ # request = Typhoeus::Request.new("www.example.com")
18
18
  # response = request.run
19
19
  # request == response.request
20
20
  # #=> true
@@ -1,5 +1,5 @@
1
1
  module Typhoeus
2
2
 
3
3
  # The current Typhoeus version.
4
- VERSION = '1.0.2'
4
+ VERSION = '1.4.0'
5
5
  end
data/lib/typhoeus.rb CHANGED
@@ -17,6 +17,21 @@ if defined?(Rack)
17
17
  require "rack/typhoeus"
18
18
  end
19
19
 
20
+ # If the Redis gem is available, load the redis cache adapter
21
+ if defined?(Redis)
22
+ require "typhoeus/cache/redis"
23
+ end
24
+
25
+ # If the Dalli gem is available, load the Dalli cache adapter
26
+ if defined?(Dalli)
27
+ require "typhoeus/cache/dalli"
28
+ end
29
+
30
+ # If we are using Rails, load the Rails cache adapter
31
+ if defined?(Rails)
32
+ require "typhoeus/cache/rails"
33
+ end
34
+
20
35
  # If we are using Rails, then we will include the Typhoeus railtie.
21
36
  # if defined?(Rails)
22
37
  # require "typhoeus/railtie"
@@ -112,11 +127,12 @@ module Typhoeus
112
127
  # #=> :ok
113
128
  # end
114
129
  #
115
- # @param [ Block ] block The block to execute.
116
- #
130
+ # @yield Yields control to the block after disabling block_connection.
131
+ # Afterwards, the block_connection is set to its original
132
+ # value.
117
133
  # @return [ Object ] Returns the return value of the block.
118
134
  #
119
- # @see Typhoeus::Config#block_connection
135
+ # @see Typhoeus::Config.block_connection
120
136
  def self.with_connection
121
137
  old = Config.block_connection
122
138
  Config.block_connection = false