artifice-passthru 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -40,4 +40,9 @@ Artifice.activate_with lambda { |env|
40
40
 
41
41
  That's all!
42
42
 
43
+ License
44
+ -------
45
+
46
+ Artifice::Passthru is released under the MIT license.
47
+
43
48
  [artifice]: https://github.com/wycats/artifice
@@ -1,98 +1,5 @@
1
- require 'artifice' unless defined? Artifice
2
-
3
- module Artifice # :nodoc:
4
-
5
- # Artifice.passthru! returns a Rack response created by making a *real* Net::HTTP
6
- # request (using the Artifice::Passthru.last_request_info) and then turning
7
- # the resulting Net::HTTPResponse object into a Rack response (which Artifice expects).
8
- def self.passthru!
9
- Artifice::Passthru.make_real_request_and_return_response!
10
- end
11
-
12
- # Given a constant (class or module), this gives it access to the *real* Net::HTTP so
13
- # every request made from within this class/module will use the real Net::HTTP
14
- def self.use_real_net_http class_or_module
15
- Artifice::Passthru.setup_to_use_real_net_http class_or_module
16
- end
17
-
18
- # Artifice Passthru
19
- module Passthru
20
-
21
- # Simple class for storing information about the last #request that was made,
22
- # allowing us to recreate the request
23
- class RequestInfo
24
- attr_accessor :address, :port, :request, :body, :block
25
-
26
- def initialize address, port, req, body, block
27
- self.address = address
28
- self.port = port
29
- self.request = req
30
- self.body = body
31
- self.block = block
32
- end
33
- end
34
-
35
- # When Artifice::Passthru is included into Artifice::Net::HTTP, it uses "alias_method_chain"
36
- # to override the #request method so we can get the arguments that were passed.
37
- def self.included base
38
- base.class_eval do
39
- alias_method :request_without_passthru_argument_tracking, :request
40
- alias_method :request, :request_with_passthru_argument_tracking
41
- end
42
- end
43
-
44
- # Returns the last information that were passed to Net::HTTP#request (which Artifice hijacked)
45
- # so we can use these arguments to fire off a real request, if necessary.
46
- def self.last_request_info
47
- Thread.current[:artifice_passthru_arguments]
48
- end
49
-
50
- # Accepts and stores the last information that were passed to Net::HTTP#request (which Artifice hijacked)
51
- # so we can use these arguments to fire off a real request, if necessary.
52
- def self.last_request_info= request_info
53
- Thread.current[:artifice_passthru_arguments] = request_info
54
- end
55
-
56
- # Makes a real Net::HTTP request and returns the response, converted to a Rack response
57
- def self.make_real_request_and_return_response!
58
- raise 'Artifice.passthru! was called but no previous Artifice request was found to make via real Net::HTTP' if last_request_info.nil?
59
- to_rack_response make_real_request(last_request_info)
60
- end
61
-
62
- # Given a Net::HTTPResponse, returns a Rack response
63
- def self.to_rack_response net_http_response
64
- # There's some voodoo magic going on that makes the real Net::HTTP#request method populate our response body for us?
65
- headers = net_http_response.instance_variable_get('@header').inject({}){|all,this| all[this.first] = this.last.join("\n"); all }
66
- [ net_http_response.code, headers, [net_http_response.body] ]
67
- end
68
-
69
- # Given the last_request_info (that would normally be passed to Net::HTTP#request),
70
- # makes a real request and returns the Net::HTTPResponse
71
- def self.make_real_request request_info
72
- http = Artifice::NET_HTTP.new request_info.address, request_info.port
73
- http.request request_info.request, request_info.body, &request_info.block
74
- end
75
-
76
- # Given a constant (class or module), this gives it access to the *real* Net::HTTP so
77
- # every request made from within this class/module will use the real Net::HTTP
78
- #
79
- # Taken from: http://matschaffer.com/2011/04/net-http-mock-cucumber/
80
- def self.setup_to_use_real_net_http class_or_module
81
- class_or_module.class_eval %{
82
- Net = ::Net.dup
83
- module Net
84
- HTTP = Artifice::NET_HTTP
85
- end
86
- }
87
- end
88
-
89
- # Simply stores the arguments that are passed and then calls "super" (request_without_passthru_argument_tracking)
90
- def request_with_passthru_argument_tracking req, body = nil, &block
91
- Artifice::Passthru.last_request_info = RequestInfo.new address, port, req, body, block
92
- request_without_passthru_argument_tracking req, body, &block
93
- end
94
- end
95
- end
1
+ require 'artifice-passthru/core'
96
2
 
97
3
  # Inject our #request method override into Artifice's implementation of Net::HTTP
4
+ require 'artifice' unless defined? Artifice::Net::HTTP
98
5
  Artifice::Net::HTTP.send :include, Artifice::Passthru
@@ -0,0 +1,97 @@
1
+ require 'artifice-passthru/version'
2
+
3
+ module Artifice
4
+
5
+ # Artifice.passthru! returns a Rack response created by making a *real* Net::HTTP
6
+ # request (using the Artifice::Passthru.last_request_info) and then turning
7
+ # the resulting Net::HTTPResponse object into a Rack response (which Artifice expects).
8
+ def self.passthru!
9
+ Artifice::Passthru.make_real_request_and_return_response!
10
+ end
11
+
12
+ # Given a constant (class or module), this gives it access to the *real* Net::HTTP so
13
+ # every request made from within this class/module will use the real Net::HTTP
14
+ def self.use_real_net_http class_or_module
15
+ Artifice::Passthru.setup_to_use_real_net_http class_or_module
16
+ end
17
+
18
+ # Artifice Passthru
19
+ module Passthru
20
+
21
+ # Simple class for storing information about the last #request that was made,
22
+ # allowing us to recreate the request
23
+ class RequestInfo
24
+ attr_accessor :address, :port, :request, :body, :block
25
+
26
+ def initialize address, port, req, body, block
27
+ self.address = address
28
+ self.port = port
29
+ self.request = req
30
+ self.body = body
31
+ self.block = block
32
+ end
33
+ end
34
+
35
+ # When Artifice::Passthru is included into Artifice::Net::HTTP, it uses "alias_method_chain"
36
+ # to override the #request method so we can get the arguments that were passed.
37
+ def self.included base
38
+ unless base.instance_methods.map(&:to_s).include?('request_without_passthru_argument_tracking')
39
+ base.class_eval do
40
+ alias_method :request_without_passthru_argument_tracking, :request
41
+ alias_method :request, :request_with_passthru_argument_tracking
42
+ end
43
+ end
44
+ end
45
+
46
+ # Returns the last information that were passed to Net::HTTP#request (which Artifice hijacked)
47
+ # so we can use these arguments to fire off a real request, if necessary.
48
+ def self.last_request_info
49
+ Thread.current[:artifice_passthru_arguments]
50
+ end
51
+
52
+ # Accepts and stores the last information that were passed to Net::HTTP#request (which Artifice hijacked)
53
+ # so we can use these arguments to fire off a real request, if necessary.
54
+ def self.last_request_info= request_info
55
+ Thread.current[:artifice_passthru_arguments] = request_info
56
+ end
57
+
58
+ # Makes a real Net::HTTP request and returns the response, converted to a Rack response
59
+ def self.make_real_request_and_return_response!
60
+ raise 'Artifice.passthru! was called but no previous Artifice request was found to make via real Net::HTTP' if last_request_info.nil?
61
+ to_rack_response make_real_request(last_request_info)
62
+ end
63
+
64
+ # Given a Net::HTTPResponse, returns a Rack response
65
+ def self.to_rack_response net_http_response
66
+ # There's some voodoo magic going on that makes the real Net::HTTP#request method populate our response body for us?
67
+ headers = net_http_response.instance_variable_get('@header').inject({}){|all,this| all[this.first] = this.last.join("\n"); all }
68
+ [ net_http_response.code, headers, [net_http_response.body] ]
69
+ end
70
+
71
+ # Given the last_request_info (that would normally be passed to Net::HTTP#request),
72
+ # makes a real request and returns the Net::HTTPResponse
73
+ def self.make_real_request request_info
74
+ http = Artifice::NET_HTTP.new request_info.address, request_info.port
75
+ http.request request_info.request, request_info.body, &request_info.block
76
+ end
77
+
78
+ # Given a constant (class or module), this gives it access to the *real* Net::HTTP so
79
+ # every request made from within this class/module will use the real Net::HTTP
80
+ #
81
+ # Taken from: http://matschaffer.com/2011/04/net-http-mock-cucumber/
82
+ def self.setup_to_use_real_net_http class_or_module
83
+ class_or_module.class_eval %{
84
+ Net = ::Net.dup
85
+ module Net
86
+ HTTP = Artifice::NET_HTTP
87
+ end
88
+ }
89
+ end
90
+
91
+ # Simply stores the arguments that are passed and then calls "super" (request_without_passthru_argument_tracking)
92
+ def request_with_passthru_argument_tracking req, body = nil, &block
93
+ Artifice::Passthru.last_request_info = RequestInfo.new address, port, req, body, block
94
+ request_without_passthru_argument_tracking req, body, &block
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,10 @@
1
+ module Artifice
2
+ module Passthru
3
+ begin
4
+ old, $VERBOSE = $VERBOSE, nil
5
+ VERSION = '0.1.1'
6
+ ensure
7
+ $VERBOSE = old
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ # Shortcut for: require 'artifice/passthru/core'
2
+ require 'artifice-passthru/core'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: artifice-passthru
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - remi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-28 00:00:00 -07:00
18
+ date: 2011-08-19 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -42,7 +42,10 @@ extra_rdoc_files: []
42
42
 
43
43
  files:
44
44
  - README.md
45
+ - lib/artifice/passthru/core.rb
45
46
  - lib/artifice/passthru.rb
47
+ - lib/artifice-passthru/core.rb
48
+ - lib/artifice-passthru/version.rb
46
49
  - lib/artifice-passthru.rb
47
50
  has_rdoc: true
48
51
  homepage: https://github.com/remi/artifice-passthru