Module: LogicalModel::RESTActions::ClassMethods

Defined in:
lib/logical_model/rest_actions.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#enable_delete_multipleObject

Returns the value of attribute enable_delete_multiple.



172
173
174
# File 'lib/logical_model/rest_actions.rb', line 172

def enable_delete_multiple
  @enable_delete_multiple
end

Instance Method Details

#all(options = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/logical_model/rest_actions.rb', line 203

def all(options={})
  result = nil
  self.retries.times do
    begin
      async_all(options){|i| result = i}
      Timeout::timeout(self.timeout/1000) do
        self.hydra.run
      end
      break unless result.nil?
    rescue Timeout::Error
      self.logger.warn("timeout")
      result = nil
    end
  end
  result
end

#async_all(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

    will be forwarded to API



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/logical_model/rest_actions.rb', line 185

def async_all(options={})
  options = self.merge_key(options)
  request = Typhoeus::Request.new(resource_uri, :params => options)
  request.on_complete do |response|
    if response.code >= 200 && response.code < 400
      log_ok(response)

      result_set = self.from_json(response.body)
      collection = result_set[:collection]

      yield collection
    else
      log_failed(response)
    end
  end
  self.hydra.queue(request)
end

#async_count(options = {}) ⇒ Object

Asynchronic Count

This count won't block excecution waiting for result, count will be enqueued in Objectr#hydra.

Parameters:

@param options [Hash].
Valid options are:
@option options [Integer] :page - indicated what page to return. Defaults to 1.
@option options [Integer] :per_page - indicates how many records to be returned per page. Defauls to 20
@option options [Hash] all other options will be forwarded in :params to WebService

Examples:

‘Count bobs’

Person.async_count(:when => {:name => 'bob'}}){|i| result = i}


293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/logical_model/rest_actions.rb', line 293

def async_count(options={})
  options[:page] = 1
  options[:per_page] = 1

  options = self.merge_key(options)

  request = Typhoeus::Request.new(resource_uri, :params => options)
  request.on_complete do |response|
    if response.code >= 200 && response.code < 400
      log_ok(response)

      result_set = self.from_json(response.body)

      yield result_set[:total]
    else
      log_failed(response)
    end
  end
  self.hydra.queue(request)
end

#async_find(id, params = {}) ⇒ Object

Asynchronic Find

This find won't block excecution waiting for result, excecution will be enqueued in Objectr#hydra.

Parameters:

- id, id of object to find

Usage:

Person.async_find(params[:id])

Parameters:

  • id (String/Integer)
  • params (Hash) (defaults to: {})


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/logical_model/rest_actions.rb', line 337

def async_find(id, params = {})
  params = self.merge_key(params)
  request = Typhoeus::Request.new( resource_uri(id), :params => params )

  request.on_complete do |response|
    if response.code >= 200 && response.code < 400
      log_ok(response)
      yield self.new.from_json(response.body) # this from_json is defined in ActiveModel::Serializers::JSON
    else
      log_failed(response)
    end
  end

  self.hydra.queue(request)
end

#async_paginate(options = {}) ⇒ Object

Asynchronic Pagination

This pagination won't block excecution waiting for result, pagination will be enqueued in Objectr#hydra.

Parameters:

@param options [Hash].
Valid options are:
* :page - indicated what page to return. Defaults to 1.
* :per_page - indicates how many records to be returned per page. Defauls to 20
* all other options will be sent in :params to WebService

Usage:

Person.async_paginate(:page => params[:page]){|i| result = i}


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
# File 'lib/logical_model/rest_actions.rb', line 232

def async_paginate(options={})
  options[:page] ||= 1
  options[:per_page] ||= 20

  options = self.merge_key(options)

  request = Typhoeus::Request.new(resource_uri, :params => options)
  request.on_complete do |response|
    if response.code >= 200 && response.code < 400
      log_ok(response)

      result_set = self.from_json(response.body)

      # this paginate is will_paginate's Array pagination
      collection = Kaminari.paginate_array(
          result_set[:collection],
          {
              :total_count=>result_set[:total],
              :limit => options[:per_page],
              :offset => options[:per_page] * ([options[:page], 1].max - 1)
          }
      )

      yield collection
    else
      log_failed(response)
    end
  end
  self.hydra.queue(request)
end

#count(options = {}) ⇒ Object

synchronic count



315
316
317
318
319
320
321
322
323
324
325
# File 'lib/logical_model/rest_actions.rb', line 315

def count(options={})
  result = nil
  async_count(options){|i| result = i}
  Timeout::timeout(self.timeout/1000) do
    self.hydra.run
  end
  result
rescue Timeout::Error
  self.logger.warn("timeout")
  return nil
end

#delete(id, params = {}) ⇒ Object

Deletes Object#id

Returns nil if delete failed

Usage:

Person.delete(params[:id])

Parameters:

  • id (String)
    • id of contact to be deleted

  • params (Hash) (defaults to: {})
    • other params to be sent to WS on request



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/logical_model/rest_actions.rb', line 375

def delete(id, params={})

  params = self.merge_key(params)

  response = nil
  Timeout::timeout(self.timeout/1000) do
    response = Typhoeus::Request.delete( self.resource_uri(id),
                                         :params => params,
                                         :timeout => self.timeout
    )
  end
  if response.code == 200
    log_ok(response)
    return self
  else
    log_failed(response)
    return nil
  end
rescue Timeout::Error
  self.logger.warn "timeout"
  return nil
end

#delete_multiple(ids, params = {}) ⇒ Object

Deletes all Objects matching given ids

This method will make a DELETE request to resource_uri/destroy_multiple

Returns nil if delete failed

Usage:

Person.delete_multiple([1,2,4,5,6])

Parameters:

  • ids (Array)
    • ids of contacts to be deleted

  • params (Hash) (defaults to: {})
    • other params to be sent to WS on request



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/logical_model/rest_actions.rb', line 409

def delete_multiple(ids, params={})
  raise "not-enabled" unless self.delete_multiple_enabled?

  params = self.merge_key(params)
  params = params.merge({:ids => ids})

  response = nil
  Timeout::timeout(self.timeout/1000) do
    response = Typhoeus::Request.delete( self.resource_uri+"/destroy_multiple",
                                         :params => params,
                                         :timeout => self.timeout
    )
  end
  if response.code == 200
    log_ok(response)
    return self
  else
    log_failed(response)
    return nil
  end
rescue Timeout::Error
  self.logger.warn "timeout"
  return nil
end

#find(id, params = {}) ⇒ Object

synchronic find



354
355
356
357
358
359
360
361
362
363
364
# File 'lib/logical_model/rest_actions.rb', line 354

def find(id, params = {})
  result = nil
  async_find(id, params){|i| result = i}
  Timeout::timeout(self.timeout/1000) do
    self.hydra.run
  end
  result
rescue Timeout::Error
  self.logger.warn("timeout")
  return nil
end

#paginate(options = {}) ⇒ Object

synchronic pagination



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/logical_model/rest_actions.rb', line 264

def paginate(options={})
  result = nil
  self.retries.times do
    begin
      async_paginate(options){|i| result = i}
      Timeout::timeout(self.timeout/1000) do
        self.hydra.run
      end
      break unless result.nil?
    rescue Timeout::Error
      self.logger.warn("timeout")
      result = nil
    end
  end
  result
end