Method: ApiResource::Mocks::MockRequest#initialize

Defined in:
lib/api_resource/mocks.rb

#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