Class: OldApiResource::Mocks::MockRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/old_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/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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



162
163
164
# File 'lib/old_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)


210
211
212
213
214
# 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) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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_sObject

string representation



216
217
218
# File 'lib/old_api_resource/mocks.rb', line 216

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