class ApiResource::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/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 = sorted_params(URI.decode(opts[:params].to_query))
    # otherwise, we need to check either the query string or the body
    # depending on the http verb
  else
    case @method
      when :post, :put
        @params = sorted_params(JSON.parse((opts[:body] || "")).to_query)
      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/api_resource/mocks.rb, line 200
def match?(request)
  return false unless self.method == request.method
  return false unless self.format == request.format || request.format.nil? || self.format.nil?
  return PathString.as_sorted_json(self.params) == PathString.as_sorted_json(request.params)
end
sorted_params(data) click to toggle source
# File lib/api_resource/mocks.rb, line 190
def sorted_params(data)
  ret = {}
  data.split("&").each do |val|
    val = val.split("=")
    ret[val.first] = val.last
  end
  ret.sort
end
to_s() click to toggle source

string representation

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