Class: RackDoubles::Middleware
- Inherits:
-
Object
- Object
- RackDoubles::Middleware
- Defined in:
- lib/rack_doubles/middleware.rb
Defined Under Namespace
Classes: Response
Class Attribute Summary collapse
-
.errors ⇒ Object
Returns the value of attribute errors.
-
.requests ⇒ Object
Returns the value of attribute requests.
-
.responses ⇒ Object
Returns the value of attribute responses.
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #errors ⇒ Object
-
#initialize(app) {|_self| ... } ⇒ Middleware
constructor
A new instance of Middleware.
- #record_error(request_url) ⇒ Object
- #record_success(request_url) ⇒ Object
- #requests ⇒ Object
- #responses ⇒ Object
- #stub(path) ⇒ Object
Constructor Details
#initialize(app) {|_self| ... } ⇒ Middleware
Returns a new instance of Middleware.
24 25 26 27 28 |
# File 'lib/rack_doubles/middleware.rb', line 24 def initialize(app, &block) @app = app @path = '/' yield self if block_given? end |
Class Attribute Details
.errors ⇒ Object
Returns the value of attribute errors.
10 11 12 |
# File 'lib/rack_doubles/middleware.rb', line 10 def errors @errors end |
.requests ⇒ Object
Returns the value of attribute requests.
10 11 12 |
# File 'lib/rack_doubles/middleware.rb', line 10 def requests @requests end |
.responses ⇒ Object
Returns the value of attribute responses.
10 11 12 |
# File 'lib/rack_doubles/middleware.rb', line 10 def responses @responses end |
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
30 31 32 |
# File 'lib/rack_doubles/middleware.rb', line 30 def path @path end |
Instance Method Details
#call(env) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rack_doubles/middleware.rb', line 32 def call(env) request = Rack::Request.new(env) if request.get? case request.path when /#{path}\/?$/ [200, {"Content-Type" => "application/json"}, [responses.to_json]] when /log\/?$/ [200, {"Content-Type" => "application/json"}, [requests.to_json]] else begin response = responses.fetch(request.path) record_success request.path response rescue KeyError record_error request.path @app.call(env) end end elsif request.delete? case request.path when path responses.clear [200, {"Content-Type" => "application/json"}, [responses.to_json]] when /\/log/ requests.clear [200, {"Content-Type" => "application/json"}, [requests.to_json]] else responses.delete request.path [200, {"Content-Type" => "application/json"}, [responses.to_json]] end elsif request.put? body_of_request = JSON.parse(request.body.read.to_s) status, headers, body = body_of_request begin raise ArgumentError if [status, headers].any?(&:nil?) || headers['Content-Type'].nil? responses.merge!({request.path => body_of_request}) [201, { 'Content-Type' => 'application/json' }, [%|{ "url": "#{request.path}", "response": #{responses.fetch(request.path).to_json} }|]] rescue ArgumentError [400, { 'Content-Type' => 'application/json' }, [%|{ "reason": "Subbing a response requires the body of the PUT request to contain a tuple with the response status and header Content-Type" }|]] end end end |
#errors ⇒ Object
20 21 22 |
# File 'lib/rack_doubles/middleware.rb', line 20 def errors self.class.errors end |
#record_error(request_url) ⇒ Object
83 84 85 |
# File 'lib/rack_doubles/middleware.rb', line 83 def record_error request_url requests[request_url]['404'] += 1 end |
#record_success(request_url) ⇒ Object
79 80 81 |
# File 'lib/rack_doubles/middleware.rb', line 79 def record_success request_url requests[request_url]['200'] += 1 end |
#requests ⇒ Object
16 17 18 |
# File 'lib/rack_doubles/middleware.rb', line 16 def requests self.class.requests end |
#responses ⇒ Object
12 13 14 |
# File 'lib/rack_doubles/middleware.rb', line 12 def responses self.class.responses end |