Class: NOMS::HttpClient::RestMock
Instance Method Summary
collapse
#allow_put_to_create, #dbg, #handle_mock, #ltrim, #method_missing, mock!, mockery, #myconfig, #opt, #rtrim, #trim
Constructor Details
#initialize(delegator) ⇒ RestMock
Returns a new instance of RestMock.
180
181
182
183
184
185
186
|
# File 'lib/noms/httpclient.rb', line 180
def initialize(delegator)
@delegator = delegator
@data = { }
@opt = @delegator.opt
@opt['return-hash'] = true unless @opt.has_key? 'return-hash'
self.dbg "Initialized with options: #{opt.inspect}"
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class NOMS::HttpClient
Instance Method Details
#all_data ⇒ Object
221
222
223
224
|
# File 'lib/noms/httpclient.rb', line 221
def all_data
maybe_read
@data
end
|
#allow_partial_updates ⇒ Object
188
189
190
|
# File 'lib/noms/httpclient.rb', line 188
def allow_partial_updates
@delegator.allow_partial_updates
end
|
#config_key ⇒ Object
192
193
194
|
# File 'lib/noms/httpclient.rb', line 192
def config_key
@delegator.config_key
end
|
#default_content_type ⇒ Object
196
197
198
|
# File 'lib/noms/httpclient.rb', line 196
def default_content_type
@delegator.default_content_type
end
|
#do_request(opt = {}) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
# File 'lib/noms/httpclient.rb', line 226
def do_request(opt={})
maybe_read
method = [:GET, :PUT, :POST, :DELETE].find do |m|
opt.has_key? m
end
method ||= :GET
opt[method] ||= ''
rel_uri = opt[method]
dbg "relative URI is #{rel_uri}"
url = URI.parse(myconfig 'url')
unless opt[method] == ''
url.path = rtrim(url.path) + '/' + ltrim(rel_uri) unless opt[:absolute]
end
url.query = opt[:query] if opt.has_key? :query
dbg "url=#{url}"
handled = handle_mock(method, url, opt)
return handled if handled
case method
when :PUT
dbg "Processing PUT"
@data[url.host] ||= { }
collection_path_components = url.path.split('/')
id = collection_path_components.pop
collection_path = collection_path_components.join('/')
@data[url.host][collection_path] ||= [ ]
object_index =
@data[url.host][collection_path].index { |el| el[id_field(collection_path)] == id }
if object_index.nil?
if allow_put_to_create
object = opt[:body].merge({ id_field(collection_path) => id })
dbg "creating in collection #{collection_path}: #{object.inspect}"
@data[url.host][collection_path] << opt[:body].merge({ id_field(collection_path) => id })
else
dbg "not creating in collection #{collection_path}: #{id} (allow_put_to_create is false)"
raise NOMS::Error, "There is no resource at this location"
end
else
if allow_partial_updates
object = @data[url.host][collection_path][object_index].merge(opt[:body])
dbg "updating in collection #{collection_path}: to => #{object.inspect}"
else
object = opt[:body].merge({ id_field(collection_path) => id })
dbg "replacing in collection #{collection_path}: #{object.inspect}"
end
@data[url.host][collection_path][object_index] = object
end
maybe_save
object
when :POST
dbg "Processing POST"
@data[url.host] ||= { }
collection_path = url.path
@data[url.host][collection_path] ||= [ ]
id = opt[:body][id_field(collection_path)] || opt[:body].object_id
object = opt[:body].merge({id_field(collection_path) => id})
dbg "creating in collection #{collection_path}: #{object.inspect}"
@data[url.host][collection_path] << object
maybe_save
object
when :DELETE
dbg "Processing DELETE"
if @data[url.host]
if @data[url.host].has_key? url.path
@data[url.host].delete url.path
true
elsif @data[url.host].has_key? url.path.split('/')[0 .. -2].join('/')
path_components = url.path.split('/')
id = path_components.pop
collection_path = path_components.join('/')
object_index = @data[url.host][collection_path].index { |obj| obj[id_field(collection_path)] == id }
if object_index.nil?
raise NOMS::Error, "Error (#{self.class} making #{config_key} request " +
"(404): No such object id (#{id_field(collection_path)} == #{id}) in #{collection_path}"
else
@data[url.host][collection_path].delete_at object_index
end
maybe_save
true
else
raise NOMS::Error, "Error (#{self.class}) making #{config_key} request " +
"(404): No objects at location or in collection #{url.path}"
end
else
raise NOMS::Error, "Error (#{self.class}) making #{config_key} request " +
"(404): No objects on #{url.host}"
end
when :GET
dbg "Performing GET"
if @data[url.host]
dbg "we store data for #{url.host}"
if @data[url.host].has_key? url.path
dbg "returning collection #{url.path}"
@data[url.host][url.path]
elsif @data[url.host].has_key? url.path.split('/')[0 .. -2].join('/')
path_components = url.path.split('/')
id = path_components.pop
collection_path = path_components.join('/')
dbg "searching in collection #{collection_path}: id=#{id}"
dbg "data: #{@data[url.host][collection_path].inspect}"
object = @data[url.host][collection_path].find { |obj| obj[id_field(collection_path)] == id }
if object.nil?
raise NOMS::Error, "Error (#{self.class} making #{config_key} request " +
"(404): No such object id (#{id_field(collection_path)} == #{id}) in #{collection_path}"
end
dbg " found #{object.inspect}"
object
else
raise NOMS::Error, "Error (#{self.class}) making #{config_key} request " +
"(404): No objects at location or in collection #{url.path}"
end
else
raise NOMS::Error, "Error (#{self.class}) making #{config_key} request " +
"(404): No objects on #{url.host}"
end
end
end
|
#id_field(dummy = nil) ⇒ Object
205
206
207
|
# File 'lib/noms/httpclient.rb', line 205
def id_field(dummy=nil)
'id'
end
|
#ignore_content_type ⇒ Object
200
201
202
|
# File 'lib/noms/httpclient.rb', line 200
def ignore_content_type
@delegator.ignore_content_type
end
|
#maybe_read ⇒ Object
215
216
217
218
219
|
# File 'lib/noms/httpclient.rb', line 215
def maybe_read
if @@mockdata and File.exist? @@mockdata
@data = File.open(@@mockdata, 'r') { |fh| JSON.load(fh) }
end
end
|
#maybe_save ⇒ Object
209
210
211
212
213
|
# File 'lib/noms/httpclient.rb', line 209
def maybe_save
if @@mockdata
File.open(@@mockdata, 'w') { |fh| fh << JSON.pretty_generate(@data) }
end
end
|