module LogicalModel::RESTActions::ClassMethods
Attributes
Public Instance Methods
# 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
@param options [Hash] will be forwarded to API
# 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
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
@example ‘Count bobs’
Person.async_count(:when => {:name => 'bob'}}){|i| result = i}
# File lib/logical_model/rest_actions.rb, line 277 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
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
@param [String/Integer] id @param [Hash] params
Usage:
Person.async_find(params[:id])
# File lib/logical_model/rest_actions.rb, line 316 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
# File lib/logical_model/rest_actions.rb, line 333 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
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}
# 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], limit: options[:per_page], offset: options[:per_page] * ([options[:page], 1].max - 1), total_count: result_set[:total] ) yield collection else log_failed(response) end end self.hydra.queue(request) end
synchronic count
# File lib/logical_model/rest_actions.rb, line 299 def count(options={}) result = nil async_count(options){|i| result = i} self.hydra.run result end
User specified default headers @return [Hash]
# File lib/logical_model/rest_actions.rb, line 166 def default_headers @headers end
Deletes Object#id
Returns nil if delete failed
@param [String] id - id of contact to be deleted @param [Hash] params - other params to be sent to WS on request
Usage:
Person.delete(params[:id])
# File lib/logical_model/rest_actions.rb, line 369 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
Deletes all Objects matching given ids
This method will make a DELETE request to resource_uri/destroy_multiple
Returns nil if delete failed
@param [Array] ids - ids of contacts to be deleted @param [Hash] params - other params to be sent to WS on request
Usage:
Person.delete_multiple([1,2,4,5,6])
# File lib/logical_model/rest_actions.rb, line 397 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
synchronic find
# File lib/logical_model/rest_actions.rb, line 344 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
synchronic pagination
# File lib/logical_model/rest_actions.rb, line 253 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
@param header [Hash]
# File lib/logical_model/rest_actions.rb, line 160 def set_default_headers(header) @headers = header end