class CloudappExport::ItemList

Attributes

limit[W]

Restrict items to a subset from 0 to $number @param number [Integer] Number of items to return @return Integer

Public Class Methods

new(api, options = {}) click to toggle source
# File lib/cloudapp_export/item_list.rb, line 6
def initialize(api, options = {})
  @api = api
  @items = []
  @cache_key = options['cache_key']
  @use_cache = options['use_cache']
  @limit = (options['limit'] || 999_999_999).to_i
  @offset = (options['offset'] || 0).to_i
end

Public Instance Methods

count() click to toggle source
# File lib/cloudapp_export/item_list.rb, line 20
def count
  [total_count, @limit].min
end
data() click to toggle source
# File lib/cloudapp_export/item_list.rb, line 15
def data
  load
  @items
end
each(&block) click to toggle source
# File lib/cloudapp_export/item_list.rb, line 29
def each(&block)
  load
  @items[@offset..(@limit - 1)].each(&block)
end
each_with_index(&block) click to toggle source
# File lib/cloudapp_export/item_list.rb, line 34
def each_with_index(&block)
  load
  @items[@offset..(@limit - 1)].each_with_index(&block)
end
total_count() click to toggle source
# File lib/cloudapp_export/item_list.rb, line 24
def total_count
  load_meta
  @meta['count'].to_i
end

Protected Instance Methods

cache_file_path() click to toggle source
# File lib/cloudapp_export/item_list.rb, line 68
def cache_file_path
  hashed_cache_key = Digest::MD5.hexdigest(@cache_key)
  "/tmp/cloudapp-export-items-cache-#{hashed_cache_key}.json"
end
load() click to toggle source
# File lib/cloudapp_export/item_list.rb, line 46
def load
  @items = begin
    if @use_cache && File.exist?(cache_file_path)
      items = ::JSON.parse(::File.read(cache_file_path))
    else
      response = @api.request("items?per_page=#{@limit}")
      items = response.data
      ::File.write(cache_file_path, ::JSON.dump(items)) if @cache_key
    end
    items.map do |attributes|
      ::CloudappExport::Item.new(attributes)
    end
  end
end
load_meta() click to toggle source
# File lib/cloudapp_export/item_list.rb, line 61
def load_meta
  @load_meta ||= begin
    response = @api.request("items?per_page=1")
    @meta = response.meta
  end
end