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.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



162
163
164
# File 'lib/api_resource/mocks.rb', line 162

def body
  @body
end

#formatObject (readonly)

Returns the value of attribute format.



162
163
164
# File 'lib/api_resource/mocks.rb', line 162

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



162
163
164
# File 'lib/api_resource/mocks.rb', line 162

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



162
163
164
# File 'lib/api_resource/mocks.rb', line 162

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



162
163
164
# File 'lib/api_resource/mocks.rb', line 162

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



162
163
164
# File 'lib/api_resource/mocks.rb', line 162

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



162
163
164
# File 'lib/api_resource/mocks.rb', line 162

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)


200
201
202
203
204
# 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) ⇒ Object



190
191
192
193
194
195
196
197
# 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_sObject

string representation



206
207
208
# File 'lib/api_resource/mocks.rb', line 206

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