class Cropio::Resource::Base

Represents ActiveRecord::Base-like model's class with Cropio data selection and mutation.

Constants

LIMIT
PROXY

Encapsulates communication with Cropio API. Proxies calls to API as HTTPS requests.

Public Class Methods

all() click to toggle source

Get all resources.

# File lib/cropio/resource/base.rb, line 26
def self.all
  to_instances(get_all_chunks)
end
changes(from_time = nil, to_time = nil) click to toggle source
# File lib/cropio/resource/base.rb, line 39
def self.changes(from_time = nil, to_time = nil)
  from_time = to_datetime_if_string(from_time)
  to_time = to_datetime_if_string(to_time)
  to_instances(get_all_changes(from_time, to_time))
end
count() click to toggle source

Count all resources.

# File lib/cropio/resource/base.rb, line 31
def self.count
  all.count
end
find(id) click to toggle source
# File lib/cropio/resource/base.rb, line 45
def self.find(id)
  obj = PROXY.get(resources_name, id: id).fetch('data', nil)
  return if obj.nil?

  to_instance(obj)
end
ids() click to toggle source
# File lib/cropio/resource/base.rb, line 35
def self.ids
  get_all_ids
end
new(attributes = {}) click to toggle source
# File lib/cropio/resource/base.rb, line 11
def initialize(attributes = {})
  self.attributes = attributes
end
resource_name() click to toggle source

Returns name of Resource

# File lib/cropio/resource/base.rb, line 16
def self.resource_name
  @resource_name ||= StringInflector.underscore(name.split('::').last)
end
resources_name() click to toggle source

Return pluralized version of Resource's name

# File lib/cropio/resource/base.rb, line 21
def self.resources_name
  @resources_name ||= StringInflector.pluralize(resource_name)
end

Private Class Methods

data?(response = nil) click to toggle source

Returns false if chunk is not empty.

# File lib/cropio/resource/base.rb, line 140
def self.data?(response = nil)
  if response.nil?
    true
  else
    response['meta']['response']['obtained_records'].nonzero?
  end
end
get_all_changes(from_time, to_time) click to toggle source
# File lib/cropio/resource/base.rb, line 110
def self.get_all_changes(from_time, to_time)
  response = nil
  buffer = []
  limit = 2 ** 32 - 1
  while data?(response) && limit > 0
    response = get_changes(limit: limit, from_time: from_time,
                           to_time: to_time)
    limit -= limit < LIMIT ? limit : LIMIT
    to_time = last_record_time(response) || from_time

    # if there is one record in time range we need to step back
    # for 1ms to exclude cycling
    if response['data'].count == 1
      to_time = (Time.parse(to_time) - 1/999999.0).iso8601(6)
    end
    buffer += response['data']
  end
  buffer
end
get_all_chunks(options = {}) click to toggle source

Download resources from Cropio by Chunks.

# File lib/cropio/resource/base.rb, line 98
def self.get_all_chunks(options = {})
  response = nil
  buffer = []
  limit = options[:limit] || (2 ** 32 - 1)
  while data?(response) && limit > 0
    limit -= limit < LIMIT ? limit : LIMIT
    response = get_chunk(limit: limit, from_id: offset(buffer))
    buffer += response['data']
  end
  buffer
end
get_all_ids() click to toggle source
# File lib/cropio/resource/base.rb, line 163
def self.get_all_ids
  PROXY.get(resources_name, resource_method: :ids)['data']
end
get_changes(options) click to toggle source
# File lib/cropio/resource/base.rb, line 155
def self.get_changes(options)
  PROXY.get(resources_name,
            resource_method: :changes,
            limit: options[:limit],
            from_time: options[:from_time],
            to_time: options[:to_time])
end
get_chunk(options) click to toggle source

Download chunk from Cropio.

# File lib/cropio/resource/base.rb, line 149
def self.get_chunk(options)
  PROXY.get(resources_name,
            limit: options[:limit],
            from_id: options[:from_id])
end
last_record_time(response) click to toggle source
# File lib/cropio/resource/base.rb, line 130
def self.last_record_time(response)
  response['meta']['response']['last_record_time']
end
offset(buffer) click to toggle source

Gets offset for next chunk during download.

# File lib/cropio/resource/base.rb, line 135
def self.offset(buffer)
  buffer.any? ? buffer.last['id'] + 1 : 0
end
to_datetime_if_string(param) click to toggle source
# File lib/cropio/resource/base.rb, line 84
def self.to_datetime_if_string(param)
  if param.is_a?(Date) || param.is_a?(DateTime)
    param.strftime
  elsif !param.nil?
    DateTime.parse(param).strftime
  end
end
to_instance(attr_set) click to toggle source

Converts specified attribute's hash to resource.

# File lib/cropio/resource/base.rb, line 175
def self.to_instance(attr_set)
  new(attr_set).tap do |resource|
    resource.instance_eval do
      @persisted = true
    end
  end
end
to_instances(attr_sets) click to toggle source

Converts each received attribute's hash to resources.

# File lib/cropio/resource/base.rb, line 168
def self.to_instances(attr_sets)
  attr_sets.map do |attr_set|
    to_instance(attr_set)
  end
end

Public Instance Methods

destroy() click to toggle source

Remove this resource from Cropio.

# File lib/cropio/resource/base.rb, line 72
def destroy
  if persisted?
    PROXY.delete("#{resources_name}/#{id}")
    @persisted = false
    true
  else
    fail 'Cropio record is not persisted!'
  end
end
persisted?() click to toggle source

Returns persistance of the resource. Resource is persisted if it is saved and not deleted, if this resource exists on Cropio servers.

# File lib/cropio/resource/base.rb, line 56
def persisted?
  !@persisted.nil? && (@persisted ||= false)
end
save() click to toggle source

Saves current resource to Cropio.

# File lib/cropio/resource/base.rb, line 61
def save
  self.attributes =
    if persisted?
      PROXY.patch("#{resources_name}/#{id}", attributes)
    else
      @persisted = true
      PROXY.post(resources_name, attributes)
    end
end

Private Instance Methods

resources_name() click to toggle source

Returns pluralized name of own type.

# File lib/cropio/resource/base.rb, line 93
def resources_name
  self.class.resources_name
end