typhoeus 0.5.0.pre → 0.5.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/CHANGELOG.md +12 -0
  2. data/Gemfile +17 -1
  3. data/README.md +2 -2
  4. data/lib/typhoeus.rb +78 -16
  5. data/lib/typhoeus/config.rb +40 -4
  6. data/lib/typhoeus/errors.rb +9 -0
  7. data/lib/typhoeus/errors/no_stub.rb +12 -0
  8. data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
  9. data/lib/typhoeus/expectation.rb +174 -0
  10. data/lib/typhoeus/hydra.rb +71 -14
  11. data/lib/typhoeus/hydra/before.rb +30 -0
  12. data/lib/typhoeus/hydra/block_connection.rb +35 -0
  13. data/lib/typhoeus/{hydras → hydra}/easy_factory.rb +17 -5
  14. data/lib/typhoeus/{hydras → hydra}/easy_pool.rb +4 -2
  15. data/lib/typhoeus/{hydras → hydra}/memoizable.rb +7 -5
  16. data/lib/typhoeus/{hydras → hydra}/queueable.rb +5 -3
  17. data/lib/typhoeus/{hydras → hydra}/runnable.rb +4 -1
  18. data/lib/typhoeus/hydra/stubbable.rb +26 -0
  19. data/lib/typhoeus/request.rb +117 -29
  20. data/lib/typhoeus/request/actions.rb +125 -0
  21. data/lib/typhoeus/request/before.rb +30 -0
  22. data/lib/typhoeus/request/block_connection.rb +52 -0
  23. data/lib/typhoeus/request/callbacks.rb +98 -0
  24. data/lib/typhoeus/{requests → request}/marshal.rb +1 -1
  25. data/lib/typhoeus/{requests → request}/memoizable.rb +4 -2
  26. data/lib/typhoeus/{requests → request}/operations.rb +25 -5
  27. data/lib/typhoeus/{requests → request}/responseable.rb +1 -1
  28. data/lib/typhoeus/request/stubbable.rb +28 -0
  29. data/lib/typhoeus/response.rb +30 -8
  30. data/lib/typhoeus/{responses → response}/header.rb +15 -11
  31. data/lib/typhoeus/response/informations.rb +205 -0
  32. data/lib/typhoeus/{responses → response}/status.rb +10 -7
  33. data/lib/typhoeus/version.rb +1 -1
  34. metadata +32 -135
  35. data/lib/typhoeus/requests/actions.rb +0 -17
  36. data/lib/typhoeus/requests/callbacks.rb +0 -48
  37. data/lib/typhoeus/responses/informations.rb +0 -43
  38. data/lib/typhoeus/responses/legacy.rb +0 -26
@@ -0,0 +1,125 @@
1
+ module Typhoeus
2
+ class Request
3
+
4
+ # Module containing logic about shortcuts to
5
+ # http methods. Like
6
+ # Typhoeus.get("www.example.com")
7
+ module Actions
8
+
9
+ # Make a get request.
10
+ #
11
+ # @example Make get request.
12
+ # Typhoeus.get("www.example.com")
13
+ #
14
+ # @param (see Typhoeus::Request#initialize)
15
+ #
16
+ # @option (see Typhoeus::Request#initialize)
17
+ #
18
+ # @return (see Typhoeus::Request#initialize)
19
+ #
20
+ # @note (see Typhoeus::Request#initialize)
21
+ def get(url, options = {})
22
+ Request.run(url, options.merge(:method => :get))
23
+ end
24
+
25
+ # Make a post request.
26
+ #
27
+ # @example Make post request.
28
+ # Typhoeus.post("www.example.com")
29
+ #
30
+ # @param (see Typhoeus::Request#initialize)
31
+ #
32
+ # @option (see Typhoeus::Request#initialize)
33
+ #
34
+ # @return (see Typhoeus::Request#initialize)
35
+ #
36
+ # @note (see Typhoeus::Request#initialize)
37
+ def post(url, options = {})
38
+ Request.run(url, options.merge(:method => :post))
39
+ end
40
+
41
+ # Make a put request.
42
+ #
43
+ # @example Make put request.
44
+ # Typhoeus.put("www.example.com")
45
+ #
46
+ # @param (see Typhoeus::Request#initialize)
47
+ #
48
+ # @option options :params [ Hash ] Params hash which
49
+ # is attached to the url.
50
+ # @option options :body [ Hash ] Body hash which
51
+ # becomes a PUT request body.
52
+ #
53
+ # @return (see Typhoeus::Request#initialize)
54
+ #
55
+ # @note (see Typhoeus::Request#initialize)
56
+ def put(url, options = {})
57
+ Request.run(url, options.merge(:method => :put))
58
+ end
59
+
60
+ # Make a delete request.
61
+ #
62
+ # @example Make delete request.
63
+ # Typhoeus.delete("www.example.com")
64
+ #
65
+ # @param (see Typhoeus::Request#initialize)
66
+ #
67
+ # @option (see Typhoeus::Request#initialize)
68
+ #
69
+ # @return (see Typhoeus::Request#initialize)
70
+ #
71
+ # @note (see Typhoeus::Request#initialize)
72
+ def delete(url, options = {})
73
+ Request.run(url, options.merge(:method => :delete))
74
+ end
75
+
76
+ # Make a head request.
77
+ #
78
+ # @example Make head request.
79
+ # Typhoeus.head("www.example.com")
80
+ #
81
+ # @param (see Typhoeus::Request#initialize)
82
+ #
83
+ # @option (see Typhoeus::Request#initialize)
84
+ #
85
+ # @return (see Typhoeus::Request#initialize)
86
+ #
87
+ # @note (see Typhoeus::Request#initialize)
88
+ def head(url, options = {})
89
+ Request.run(url, options.merge(:method => :head))
90
+ end
91
+
92
+ # Make a patch request.
93
+ #
94
+ # @example Make patch request.
95
+ # Typhoeus.patch("www.example.com")
96
+ #
97
+ # @param (see Typhoeus::Request#initialize)
98
+ #
99
+ # @option (see Typhoeus::Request#initialize)
100
+ #
101
+ # @return (see Typhoeus::Request#initialize)
102
+ #
103
+ # @note (see Typhoeus::Request#initialize)
104
+ def patch(url, options = {})
105
+ Request.run(url, options.merge(:method => :patch))
106
+ end
107
+
108
+ # Make a options request.
109
+ #
110
+ # @example Make options request.
111
+ # Typhoeus.options("www.example.com")
112
+ #
113
+ # @param (see Typhoeus::Request#initialize)
114
+ #
115
+ # @option (see Typhoeus::Request#initialize)
116
+ #
117
+ # @return (see Typhoeus::Request#initialize)
118
+ #
119
+ # @note (see Typhoeus::Request#initialize)
120
+ def options(url, options = {})
121
+ Request.run(url, options.merge(:method => :options))
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,30 @@
1
+ module Typhoeus
2
+ class Request
3
+
4
+ # This module provides a way to hook into before
5
+ # a request runs. This is very powerful
6
+ # and you should be careful because when you accidently
7
+ # return a falsy value the request won't be executed.
8
+ #
9
+ # @api private
10
+ module Before
11
+
12
+ # Overrride run in order to execute callbacks in
13
+ # Typhoeus.before. Will break and return when a
14
+ # callback returns nil or false. Calls super
15
+ # otherwise.
16
+ #
17
+ # @example Run the request.
18
+ # request.run
19
+ def run
20
+ Typhoeus.before.each do |callback|
21
+ value = callback.call(self)
22
+ if value.nil? || value == false || value.is_a?(Response)
23
+ return value
24
+ end
25
+ end
26
+ super
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,52 @@
1
+ module Typhoeus
2
+ class Request
3
+
4
+ # This module handles the blocked connection request mode on
5
+ # the request side, where only stubbed requests
6
+ # are allowed.
7
+ # Connection blocking needs to be turned on:
8
+ # Typhoeus.configure do |config|
9
+ # config.block_connection = true
10
+ # end
11
+ #
12
+ # When trying to do real requests a NoStub error
13
+ # is raised.
14
+ #
15
+ # @api private
16
+ module BlockConnection
17
+
18
+ # Overrides run in order to check before if block connection
19
+ # is turned on. If thats the case a NoStub error is
20
+ # raised.
21
+ #
22
+ # @example Run request.
23
+ # request.run
24
+ #
25
+ # @raise [Typhoeus::Errors::NoStub] If connection is blocked
26
+ # and no stub defined.
27
+ def run
28
+ if blocked?
29
+ raise Typhoeus::Errors::NoStub.new(self)
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ # Returns wether a request is blocked or not. Takes
36
+ # request.block_connection and Typhoeus::Config.block_connection
37
+ # into consideration.
38
+ #
39
+ # @example Blocked?
40
+ # request.blocked?
41
+ #
42
+ # @return [ Boolean ] True if blocked, false else.
43
+ def blocked?
44
+ if block_connection.nil?
45
+ Typhoeus::Config.block_connection
46
+ else
47
+ block_connection
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,98 @@
1
+ module Typhoeus
2
+ class Request
3
+
4
+ # This module contains the logic for the response callbacks.
5
+ # The on_complete callback is the only one at the moment.
6
+ #
7
+ # You can set multiple callbacks, which are then executed
8
+ # in the same order.
9
+ #
10
+ # request.on_complete { |response| p 1 }
11
+ # request.on_complete { |response| p 2 }
12
+ # request.execute_callbacks
13
+ # #=> 1
14
+ # #=> 2
15
+ #
16
+ # You can clear the callbacks:
17
+ #
18
+ # request.on_complete { |response| p 1 }
19
+ # request.on_complete { |response| p 2 }
20
+ # request.on_complete.clear
21
+ # request.execute_callbacks
22
+ # #=> []
23
+ module Callbacks
24
+
25
+ module Types # :nodoc:
26
+ # Set on_complete callback.
27
+ #
28
+ # @example Set on_complete.
29
+ # request.on_complete { |response| p "yay" }
30
+ #
31
+ # @param [ Block ] block The block to execute.
32
+ #
33
+ # @yield [ Typhoeus::Response ]
34
+ #
35
+ # @return [ Array<Block> ] All on_complete blocks.
36
+ def on_complete(&block)
37
+ @on_complete ||= []
38
+ @on_complete << block if block_given?
39
+ @on_complete
40
+ end
41
+
42
+ # Set on_success callback.
43
+ #
44
+ # @example Set on_success.
45
+ # request.on_success { |response| p "yay" }
46
+ #
47
+ # @param [ Block ] block The block to execute.
48
+ #
49
+ # @yield [ Typhoeus::Response ]
50
+ #
51
+ # @return [ Array<Block> ] All on_success blocks.
52
+ def on_success(&block)
53
+ @on_success ||= []
54
+ @on_success << block if block_given?
55
+ @on_success
56
+ end
57
+
58
+ # Set on_failure callback.
59
+ #
60
+ # @example Set on_failure.
61
+ # request.on_failure { |response| p "yay" }
62
+ #
63
+ # @param [ Block ] block The block to execute.
64
+ #
65
+ # @yield [ Typhoeus::Response ]
66
+ #
67
+ # @return [ Array<Block> ] All on_failure blocks.
68
+ def on_failure(&block)
69
+ @on_failure ||= []
70
+ @on_failure << block if block_given?
71
+ @on_failure
72
+ end
73
+ end
74
+
75
+ # Execute nessecary callback and yields response. This
76
+ # include in every case on_complete, on_success if
77
+ # successful and on_failure if not.
78
+ #
79
+ # @example Execute callbacks.
80
+ # request.execute_callbacks
81
+ #
82
+ # @return [ void ]
83
+ #
84
+ # @api private
85
+ def execute_callbacks
86
+ callbacks = Typhoeus.on_complete + on_complete
87
+
88
+ if response && response.success?
89
+ callbacks += Typhoeus.on_success + on_success
90
+ elsif response
91
+ callbacks += Typhoeus.on_failure + on_failure
92
+ end
93
+
94
+ callbacks.map{ |callback| callback.call(self.response) }
95
+ end
96
+ end
97
+ end
98
+ end
@@ -1,5 +1,5 @@
1
1
  module Typhoeus
2
- module Requests
2
+ class Request
3
3
 
4
4
  # This module contains custom serializer.
5
5
  module Marshal
@@ -1,12 +1,14 @@
1
1
  module Typhoeus
2
- module Requests
2
+ class Request
3
3
 
4
4
  # This module handles the GET request memoization
5
5
  # on the request side. Memoization needs to be turned
6
6
  # on:
7
- # Typhoeus.configre do |config|
7
+ # Typhoeus.configure do |config|
8
8
  # config.memoize = true
9
9
  # end
10
+ #
11
+ # @api private
10
12
  module Memoizable
11
13
 
12
14
  # Override response setter and memoizes response
@@ -1,5 +1,5 @@
1
1
  module Typhoeus
2
- module Requests
2
+ class Request
3
3
 
4
4
  # This module contains everything what is necessary
5
5
  # to make a single request.
@@ -21,6 +21,8 @@ module Typhoeus
21
21
  # @param [ Hash ] options The options hash.
22
22
  #
23
23
  # @return [ Response ] The response.
24
+ #
25
+ # @deprecated
24
26
  def run(url, options = {})
25
27
  new(url, options).run
26
28
  end
@@ -29,7 +31,7 @@ module Typhoeus
29
31
  # Run a request.
30
32
  #
31
33
  # @example Run a request.
32
- # request.run
34
+ # Typhoeus::Request.new("www.example.com").run
33
35
  #
34
36
  # @return [ Response ] The response.
35
37
  def run
@@ -41,10 +43,28 @@ module Typhoeus
41
43
  )
42
44
  easy.prepare
43
45
  easy.perform
44
- @response = Response.new(easy.to_hash)
46
+ finish(Response.new(easy.to_hash))
45
47
  Typhoeus.release_easy(easy)
46
- complete
47
- @response
48
+ response
49
+ end
50
+
51
+ # Sets a response, the request on the response
52
+ # and executes the callbacks.
53
+ #
54
+ # @param [Typhoeus::Response] response The response.
55
+ # @param [Boolean] bypass_memoization Wether to bypass
56
+ # memoization or not. Decides how the response is set.
57
+ #
58
+ # @return [Typhoeus::Response] The response.
59
+ def finish(response, bypass_memoization = nil)
60
+ if bypass_memoization
61
+ @response = response
62
+ else
63
+ self.response = response
64
+ end
65
+ self.response.request = self
66
+ execute_callbacks
67
+ response
48
68
  end
49
69
  end
50
70
  end
@@ -1,5 +1,5 @@
1
1
  module Typhoeus
2
- module Requests
2
+ class Request
3
3
 
4
4
  # This module contains logic for having a reponse
5
5
  # getter and setter.
@@ -0,0 +1,28 @@
1
+ module Typhoeus
2
+ class Request
3
+
4
+ # This module handles stubbing on the request side.
5
+ # It plays well with the block_connection configuration,
6
+ # which raises when you make a request which is not stubbed.
7
+ #
8
+ # @api private
9
+ module Stubbable
10
+
11
+ # Override run in order to check for matching expecations.
12
+ # When an expecation is found, super is not called. Instead a
13
+ # canned response is assigned to the request.
14
+ #
15
+ # @example Run the request.
16
+ # request.run
17
+ #
18
+ # @return [ Response ] The response.
19
+ def run
20
+ if expectation = Expectation.find_by(self)
21
+ finish(expectation.response)
22
+ else
23
+ super
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,17 +1,33 @@
1
- require 'typhoeus/responses/legacy'
2
- require 'typhoeus/responses/informations'
3
- require 'typhoeus/responses/status'
4
- require 'typhoeus/responses/header'
1
+ require 'typhoeus/response/header'
2
+ require 'typhoeus/response/informations'
3
+ require 'typhoeus/response/status'
5
4
 
6
5
  module Typhoeus
7
6
 
8
7
  # This class respresents the response.
9
8
  class Response
10
- include Responses::Status
11
- include Responses::Informations
12
- include Responses::Legacy
9
+ include Response::Informations
10
+ include Response::Status
13
11
 
14
- attr_accessor :request, :options
12
+ # Remembers the corresponding request.
13
+ #
14
+ # @example Get request.
15
+ # request = Typhoeus::Request.get("www.example.com")
16
+ # response = request.run
17
+ # response == request.response
18
+ # #=> true
19
+ #
20
+ # @return [ Typhoeus::Request ]
21
+ attr_accessor :request
22
+
23
+ # The options provided, contains all the
24
+ # informations about the request.
25
+ #
26
+ # @return [ Hash ]
27
+ attr_accessor :options
28
+
29
+ # @api private
30
+ attr_writer :mock
15
31
 
16
32
  # Create a new response.
17
33
  #
@@ -23,6 +39,12 @@ module Typhoeus
23
39
  # @return [ Response ] The new response.
24
40
  def initialize(options = {})
25
41
  @options = options
42
+ @headers = options[:headers]
43
+ end
44
+
45
+ # @api private
46
+ def mock
47
+ defined?(@mock) ? @mock : options[:mock]
26
48
  end
27
49
  end
28
50
  end