Class: ApiResource::Mocks::MockRequest
- Inherits:
-
Object
- Object
- ApiResource::Mocks::MockRequest
- Defined in:
- lib/api_resource/mocks.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#initialize(method, path, opts = {}) ⇒ MockRequest
constructor
A new instance of MockRequest.
-
#match?(request) ⇒ Boolean
because of the context these come from, we can assume that the path already matches.
-
#to_s ⇒ Object
string representation.
- #typecast_values(data) ⇒ Object
Constructor Details
#initialize(method, path, opts = {}) ⇒ MockRequest
Returns a new instance of MockRequest.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/api_resource/mocks.rb', line 173 def initialize(method, path, opts = {}) @method = method.to_sym # set the normalized path, format and query string @path, @query = path.split("?") @path, @format = @path.split(".") # if we have params, it is a MockRequest definition if opts[:params] @params = opts[:params] # otherwise, we need to check either the query string or the body # depending on the http verb else case @method when :post, :put @params = JSON.parse(opts[:body] || "") when :get, :delete, :head @params = typecast_values( Rack::Utils.parse_nested_query(@query || "") ) end end @body = opts[:body] @headers = opts[:headers] || {} @headers["Content-Length"] = @body.blank? ? "0" : @body.size.to_s end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
171 172 173 |
# File 'lib/api_resource/mocks.rb', line 171 def body @body end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
171 172 173 |
# File 'lib/api_resource/mocks.rb', line 171 def format @format end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
171 172 173 |
# File 'lib/api_resource/mocks.rb', line 171 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
171 172 173 |
# File 'lib/api_resource/mocks.rb', line 171 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
171 172 173 |
# File 'lib/api_resource/mocks.rb', line 171 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
171 172 173 |
# File 'lib/api_resource/mocks.rb', line 171 def path @path end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
171 172 173 |
# File 'lib/api_resource/mocks.rb', line 171 def query @query end |
Instance Method Details
#match?(request) ⇒ Boolean
because of the context these come from, we can assume that the path already matches
227 228 229 230 231 |
# File 'lib/api_resource/mocks.rb', line 227 def match?(request) return false unless self.method == request.method return false unless self.format == request.format || request.format.nil? || self.format.nil? Comparator.diff(self.params, request.params) == {} end |
#to_s ⇒ Object
string representation
233 234 235 |
# File 'lib/api_resource/mocks.rb', line 233 def to_s "#{self.method.upcase} #{self.format} #{self.path} #{self.params}" end |
#typecast_values(data) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/api_resource/mocks.rb', line 201 def typecast_values(data) if data.is_a?(Hash) data.each_pair do |k,v| data[k] = typecast_values(v) end elsif data.is_a?(Array) data = data.collect{|v| typecast_values(v) } else data = if data.to_s =~ /^\d+$/ data.to_i elsif data =~ /^[\d\.]+$/ data.to_f elsif data == "true" true elsif data == "false" false else data end end data.nil? ? "" : data end |