class ALD::API::ItemCollection

Public: Represents a (possibly filtered) set of items on an ALD server

Constants

ARRAY_CONDITIONS

Internal: filter conditions that allow specifying an array.

LOCAL_CONDITIONS

Internal: filter conditions that can be handled locally.

RANGE_CONDITIONS

Internal: filter conditions that allow specifying a range, like ‘version-min=0.2.1&version-max=3.4.5’

Public Class Methods

new(api, conditions = {}, data = nil) click to toggle source

Internal: Create a new instance. This should not be called by library consumers. Instead use API#items or where to get a new instance.

api - the ALD::API instance this collection belongs to conditions - a Hash of conditions items in this collection must meet data - an Array of Hashes representing the items in this

collection:
id      - the GUID of the item
name    - the item's name
version - the item's semver version
Calls superclass method ALD::API::Collection::new
# File lib/ALD/item_collection.rb, line 21
def initialize(api, conditions = {}, data = nil)
  super(api, conditions, data)
end

Private Instance Methods

entry(hash, initialized = false) click to toggle source

Internal: Used by Collection#each and Collection#[] to create new items.

hash - a Hash describing the item, with the keys ‘id’, ‘name’

and 'version'.

initialized - a Boolean indicating if the given Hash already contains

all information about the item or only name and id.
# File lib/ALD/item_collection.rb, line 149
def entry(hash, initialized = false)
  @api.item(hash, initialized)
end
entry_filter(args) click to toggle source

Internal: Implements item access for []. See Collection#entry_filter for more information.

ItemCollection allows access by ID (String) or name and version (both String).

# File lib/ALD/item_collection.rb, line 158
def entry_filter(args)
  if args.length == 1 && args.first.is_a?(String)
    { id: @api.normalize_id(args.first) }
  elsif args.length == 2
    { name: args.first, version: args.last }
  else
    raise ArgumentError
  end
end
request() click to toggle source

Internal: Make a HTTP request to the ALD server to get the list of item hashes matching this collection’s conditions.

Returns nothing.

# File lib/ALD/item_collection.rb, line 106
def request
  data = [
    exact_queries(%w[name user type]),
    switch_queries(%w[stable reviewed]),
    array_queries(%w[tags]),
    range_condition_queries(%w[downloads rating version]),
    sort_query,
    range_query
  ].reduce({}, :merge)

  url = "/items/#{data.empty? ? '' : '?'}#{URI.encode_www_form(data)}"
  @data = @api.request(url).map do |hash|
    hash['id'] = @api.normalize_id(hash['id'])
    hash
  end
end
request_entry(filter) click to toggle source

Internal: Make a HTTP request to the ALD server to get a single item. Used by Collection#[].

filter - a filter Hash as returned by entry_filter

Returns a Hash with all information about the item.

Raises ArgumentError if the filters cannot be handled.

# File lib/ALD/item_collection.rb, line 131
def request_entry(filter)
  url = if filter.key?(:id)
    "/items/#{filter[:id]}"
  elsif %w[name version].all? { |k| filter.key?(k.to_sym) }
    "/items/#{filter[:name]}/#{filter[:version]}"
  else
    raise ArgumentError
  end

  @api.request(url)
end