class OldApiResource::Mocks::MockRequest

Attributes

body[R]
format[R]
headers[R]
method[R]
params[R]
path[R]
query[R]

Public Class Methods

new(method, path, opts = {}) click to toggle source
# File lib/old_api_resource/mocks.rb, line 164
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 = JSON.parse(JSON.unparse(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] || "").sort
      when :get, :delete, :head
        @params = sorted_params(@query || "")
    end
  end
  @body = opts[:body]
  @headers = opts[:headers] || {}
  @headers["Content-Length"] = @body.blank? ? "0" : @body.size.to_s
end

Public Instance Methods

match?(request) click to toggle source

because of the context these come from, we can assume that the path already matches

# File lib/old_api_resource/mocks.rb, line 210
def match?(request)
  return false unless self.method == request.method
  return false unless self.format == request.format || request.format.nil? || self.format.nil?
  PathString.as_sorted_json(self.params) == PathString.as_sorted_json(request.params)
end
sorted_params(data) click to toggle source
# File lib/old_api_resource/mocks.rb, line 190
def sorted_params(data)
  ret = {}
  data.split("&").each do |val|
    val = val.split("=")
    if val.last =~ /^\d+$/
      ret[val.first] = val.last.to_i
    elsif val.last =~ /^[\d\.]+$/
      ret[val.first] = val.last.to_f
    elsif val.last == "true"
      ret[val.first] = true
    elsif val.last == "false"
      ret[val.first] = false  
    else
      ret[val.first] = val.last
    end
  end
  ret.sort
end
to_s() click to toggle source

string representation

# File lib/old_api_resource/mocks.rb, line 216
def to_s
  "#{self.method.upcase} #{self.format} #{self.path} #{self.params}"
end