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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/logical_model/rest_actions.rb', line 214

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/logical_model/rest_actions.rb', line 196

def async_all(options={})
  options = self.merge_key(options)
  request = Typhoeus::Request.new(resource_uri, params: options, headers: default_headers)
  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}


304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/logical_model/rest_actions.rb', line 304

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

  options = self.merge_key(options)

  request = Typhoeus::Request.new(resource_uri, params: options, headers: default_headers)
  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: {})


348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/logical_model/rest_actions.rb', line 348

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}


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

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

  options = self.merge_key(options)

  request = Typhoeus::Request.new(resource_uri, params: options, headers: default_headers)
  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



326
327
328
329
330
331
332
333
334
335
336
# File 'lib/logical_model/rest_actions.rb', line 326

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

#default_headersHash

User specified default headers

Returns:

  • (Hash)


181
182
183
# File 'lib/logical_model/rest_actions.rb', line 181

def default_headers
  @headers
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



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/logical_model/rest_actions.rb', line 386

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



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/logical_model/rest_actions.rb', line 420

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



365
366
367
368
369
370
371
372
373
374
375
# File 'lib/logical_model/rest_actions.rb', line 365

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



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/logical_model/rest_actions.rb', line 275

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

#set_default_headers(header) ⇒ Object

Parameters:

  • header (Hash)


175
176
177
# File 'lib/logical_model/rest_actions.rb', line 175

def set_default_headers(header)
  @headers = header
end