Module: LogicalModel::RESTActions::ClassMethods
- Defined in:
- lib/logical_model/rest_actions.rb
Instance Attribute Summary collapse
-
#enable_delete_multiple ⇒ Object
Returns the value of attribute enable_delete_multiple.
Instance Method Summary collapse
-
#async_count(options = {}) ⇒ Object
Asynchronic Count This count won’t block excecution waiting for result, count will be enqueued in Objectr#hydra.
-
#async_find(id, params = {}) ⇒ Object
Asynchronic Find This find won’t block excecution waiting for result, excecution will be enqueued in Objectr#hydra.
-
#async_paginate(options = {}) ⇒ Object
Asynchronic Pagination This pagination won’t block excecution waiting for result, pagination will be enqueued in Objectr#hydra.
-
#count(options = {}) ⇒ Object
synchronic count.
-
#delete(id, params = {}) ⇒ Object
Deletes Object#id.
-
#delete_multiple(ids, params = {}) ⇒ Object
Deletes all Objects matching given ids.
-
#find(id, params = {}) ⇒ Object
synchronic find.
-
#paginate(options = {}) ⇒ Object
synchronic pagination.
Instance Attribute Details
#enable_delete_multiple ⇒ Object
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
#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
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/logical_model/rest_actions.rb', line 256 def async_count(={}) [:page] = 1 [:per_page] = 1 = self.merge_key() request = Typhoeus::Request.new(resource_uri, :params => ) 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])
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/logical_model/rest_actions.rb', line 300 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}
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/logical_model/rest_actions.rb', line 195 def async_paginate(={}) [:page] ||= 1 [:per_page] ||= 20 = self.merge_key() request = Typhoeus::Request.new(resource_uri, :params => ) 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 => [:per_page], :offset => [:per_page] * ([[:page], 1].max - 1) } ) yield collection else log_failed(response) end end self.hydra.queue(request) end |
#count(options = {}) ⇒ Object
synchronic count
278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/logical_model/rest_actions.rb', line 278 def count(={}) result = nil async_count(){|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])
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/logical_model/rest_actions.rb', line 338 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])
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/logical_model/rest_actions.rb', line 372 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
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/logical_model/rest_actions.rb', line 317 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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/logical_model/rest_actions.rb', line 227 def paginate(={}) result = nil self.retries.times do begin async_paginate(){|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 |