Class: WebStub::Stub
- Inherits:
-
Object
- Object
- WebStub::Stub
- Defined in:
- lib/webstub/stub.rb
Constant Summary collapse
- METHODS =
["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH"].freeze
Instance Attribute Summary collapse
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#requests ⇒ Object
Returns the value of attribute requests.
-
#response_body ⇒ Object
readonly
Returns the value of attribute response_body.
-
#response_delay ⇒ Object
readonly
Returns the value of attribute response_delay.
-
#response_error ⇒ Object
readonly
Returns the value of attribute response_error.
-
#response_headers ⇒ Object
readonly
Returns the value of attribute response_headers.
-
#response_status_code ⇒ Object
readonly
Returns the value of attribute response_status_code.
Instance Method Summary collapse
- #do_callback(headers, body) ⇒ Object
- #error? ⇒ Boolean
-
#initialize(method, url) ⇒ Stub
constructor
A new instance of Stub.
- #matches?(method, url, options = {}) ⇒ Boolean
- #redirects? ⇒ Boolean
- #requested? ⇒ Boolean
- #to_fail(options) ⇒ Object
- #to_redirect(options) ⇒ Object
- #to_return(options) ⇒ Object
- #with(options) ⇒ Object
- #with_callback(&callback) ⇒ Object
Constructor Details
#initialize(method, url) ⇒ Stub
Returns a new instance of Stub.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/webstub/stub.rb', line 5 def initialize(method, url) @request_method = canonicalize_method(method) raise ArgumentError, "invalid method name" unless METHODS.include? @request_method @requests = 0 @request_url = canonicalize_url(url) @request_headers = nil @request_body = nil @callback = nil @response_body = "" @response_delay = 0.0 @response_error = nil @response_headers = {} @response_status_code = 200 end |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback.
56 57 58 |
# File 'lib/webstub/stub.rb', line 56 def callback @callback end |
#requests ⇒ Object
Returns the value of attribute requests.
55 56 57 |
# File 'lib/webstub/stub.rb', line 55 def requests @requests end |
#response_body ⇒ Object (readonly)
Returns the value of attribute response_body.
66 67 68 |
# File 'lib/webstub/stub.rb', line 66 def response_body @response_body end |
#response_delay ⇒ Object (readonly)
Returns the value of attribute response_delay.
67 68 69 |
# File 'lib/webstub/stub.rb', line 67 def response_delay @response_delay end |
#response_error ⇒ Object (readonly)
Returns the value of attribute response_error.
68 69 70 |
# File 'lib/webstub/stub.rb', line 68 def response_error @response_error end |
#response_headers ⇒ Object (readonly)
Returns the value of attribute response_headers.
69 70 71 |
# File 'lib/webstub/stub.rb', line 69 def response_headers @response_headers end |
#response_status_code ⇒ Object (readonly)
Returns the value of attribute response_status_code.
70 71 72 |
# File 'lib/webstub/stub.rb', line 70 def response_status_code @response_status_code end |
Instance Method Details
#do_callback(headers, body) ⇒ Object
115 116 117 118 119 |
# File 'lib/webstub/stub.rb', line 115 def do_callback(headers, body) if @callback @callback.call(headers, body) end end |
#error? ⇒ Boolean
23 24 25 |
# File 'lib/webstub/stub.rb', line 23 def error? ! @response_error.nil? end |
#matches?(method, url, options = {}) ⇒ Boolean
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/webstub/stub.rb', line 27 def matches?(method, url, ={}) if @request_url != canonicalize_url(url) return false end if @request_method != canonicalize_method(method) return false end if @request_headers headers = [:headers] || {} @request_headers.each do |key, value| if headers[key] != value return false end end end if @request_body if @request_body != [:body] return false end end true end |
#redirects? ⇒ Boolean
58 59 60 |
# File 'lib/webstub/stub.rb', line 58 def redirects? @response_status_code.between?(300, 399) && @response_headers["Location"] != nil end |
#requested? ⇒ Boolean
62 63 64 |
# File 'lib/webstub/stub.rb', line 62 def requested? @requests > 0 end |
#to_fail(options) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/webstub/stub.rb', line 72 def to_fail() if error = .delete(:error) @response_error = error elsif code = .delete(:code) @response_error = NSError.errorWithDomain(NSURLErrorDomain, code: code, userInfo: nil) else raise ArgumentError, "to_fail requires either the code or error option" end self end |
#to_redirect(options) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/webstub/stub.rb', line 125 def to_redirect() unless url = .delete(:url) raise ArgumentError, "to_redirect requires the :url option" end [:headers] ||= {} [:headers]["Location"] = url [:status_code] = 301 to_return() end |
#to_return(options) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/webstub/stub.rb', line 84 def to_return() if status_code = [:status_code] @response_status_code = status_code end if headers = [:headers] @response_headers.merge!(headers) end if json = [:json] @response_body = json @response_headers["Content-Type"] = "application/json" if @response_body.is_a?(Hash) || @response_body.is_a?(Array) @response_body = JSON.generate(@response_body) end else @response_body = [:body] || "" if content_type = [:content_type] @response_headers["Content-Type"] = content_type end end if delay = [:delay] @response_delay = delay end self end |
#with(options) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/webstub/stub.rb', line 137 def with() if body = [:body] @request_body = body if @request_body.is_a?(Hash) @request_body = @request_body.inject({}) { |h, (k,v)| h[k.to_s] = v; h } end end if headers = [:headers] @request_headers = headers end self end |
#with_callback(&callback) ⇒ Object
121 122 123 |
# File 'lib/webstub/stub.rb', line 121 def with_callback(&callback) @callback = callback end |