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.



157
158
159
# File 'lib/logical_model/rest_actions.rb', line 157

def enable_delete_multiple
  @enable_delete_multiple
end

Instance Method Details

#all(options = {}) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
# File 'lib/logical_model/rest_actions.rb', line 199

def all(options={})
  result = nil
  self.retries.times do
    begin
      async_all(options){|i| result = i}
      self.hydra.run
      break unless result.nil?
    end
  end
  result
end

#async_all(options = {}) ⇒ Object

Parameters:

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

    will be forwarded to API



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/logical_model/rest_actions.rb', line 181

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}


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/logical_model/rest_actions.rb', line 279

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: {})


318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/logical_model/rest_actions.rb', line 318

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 async_find_response(id, params, response.body), response.code
    else
      log_failed(response)
      yield nil, response.code
    end
  end

  self.hydra.queue(request)
end

#async_find_response(id, params, body) ⇒ Object



335
336
337
338
339
340
341
342
343
# File 'lib/logical_model/rest_actions.rb', line 335

def async_find_response(id, params, body)
  if body.blank?
    # if request failed failed unexpectedly we may get code 200 but empty body
    self.logger.warn("got response code 200 but empty body")
    return nil
  end

  self.new.from_json(body)
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}


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

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



301
302
303
304
305
306
# File 'lib/logical_model/rest_actions.rb', line 301

def count(options={})
  result = nil
  async_count(options){|i| result = i}
  self.hydra.run
  result
end

#default_headersHash

User specified default headers

Returns:

  • (Hash)


166
167
168
# File 'lib/logical_model/rest_actions.rb', line 166

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



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/logical_model/rest_actions.rb', line 371

def delete(id, params={})

  params = self.merge_key(params)

  response = Typhoeus::Request.delete( self.resource_uri(id),
                                       params: params,
                                       timeout: self.timeout
  )
  if response.code == 200
    log_ok(response)
    return self
  else
    log_failed(response)
    return nil
  end
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



399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/logical_model/rest_actions.rb', line 399

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

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

  response = Typhoeus::Request.delete( self.resource_uri+"/destroy_multiple",
                                       params: params,
                                       timeout: self.timeout
  )
  if response.code == 200
    log_ok(response)
    return self
  else
    log_failed(response)
    return nil
  end
end

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

synchronic find



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

def find(id, params = {})
  result = nil
  self.retries.times do
    begin
      response_code = nil
      async_find(id, params) do |res,code|
        result = res
        response_code = code
      end
      self.hydra.run
      break unless result.nil? && (response_code != 404) # don't retry if response was 404
    end
  end
  result
end

#paginate(options = {}) ⇒ Object

synchronic pagination



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/logical_model/rest_actions.rb', line 255

def paginate(options={})
  result = nil
  self.retries.times do
    begin
      async_paginate(options){|i| result = i}
      self.hydra.run
      break unless result.nil?
    end
  end
  result
end

#set_default_headers(header) ⇒ Object

Parameters:

  • header (Hash)


160
161
162
# File 'lib/logical_model/rest_actions.rb', line 160

def set_default_headers(header)
  @headers = header
end