Class: ApiResource::Mocks::MockRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/api_resource/mocks.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, opts = {}) ⇒ MockRequest

Returns a new instance of MockRequest.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/api_resource/mocks.rb', line 179

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 opts[:body].present? && !opts[:body].is_a?(String)
    raise "#{opts[:body]} must be passed as a String"
  end

  # 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

#bodyObject (readonly)

Returns the value of attribute body.



177
178
179
# File 'lib/api_resource/mocks.rb', line 177

def body
  @body
end

#formatObject (readonly)

Returns the value of attribute format.



177
178
179
# File 'lib/api_resource/mocks.rb', line 177

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



177
178
179
# File 'lib/api_resource/mocks.rb', line 177

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



177
178
179
# File 'lib/api_resource/mocks.rb', line 177

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



177
178
179
# File 'lib/api_resource/mocks.rb', line 177

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



177
178
179
# File 'lib/api_resource/mocks.rb', line 177

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



177
178
179
# File 'lib/api_resource/mocks.rb', line 177

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

Returns:

  • (Boolean)


237
238
239
240
241
# File 'lib/api_resource/mocks.rb', line 237

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_sObject

string representation



243
244
245
# File 'lib/api_resource/mocks.rb', line 243

def to_s
  "#{self.method.upcase} #{self.format} #{self.path} #{self.params}"
end

#typecast_values(data) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/api_resource/mocks.rb', line 211

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