class ALD::API::Item

Public: An item (e.g. a library or app) uploaded to an ALD server.

Public Class Methods

new(api, data, initialized = false) click to toggle source

Internal: Create a new instance for given data. This method should not called by library consumers. Instead access entries via API#item or ItemCollection#[].

api - the ALD::API instance this item belongs to data - a Hash containing data concerning the item:

id      - the GUID of the item
name    - the name of the item
version - the semver version of the item
The above fields are mandatory. However, the hash may
contain a lot more data about the item.

initialized - a Boolean indicating if data only contains the mandatory

fields or *all* data on the item.
Calls superclass method ALD::API::CollectionEntry::new
# File lib/ALD/item.rb, line 141
def initialize(api, data, initialized = false)
  raise ArgumentError unless Item.valid_data?(data)
  super(api, data, initialized)
end

Private Class Methods

initialized_attributes() click to toggle source

Internal: Override of CollectionEntry#initialized_attributes to enable automatic method definition, in this case id, name and version.

Returns an Array of attribute names (String)

# File lib/ALD/item.rb, line 173
def self.initialized_attributes
  %w[id name version]
end
requested_attributes() click to toggle source

Internal: Override of CollectionEntry#requested_attributes to enable automatic method definition.

Returns an Array of attribute names (String)

# File lib/ALD/item.rb, line 181
def self.requested_attributes
  %w[summary description uploaded reviewed downloads tags authors user ratings]
end
valid_data?(data) click to toggle source

Internal: Ensure a Hash contains all information necessary to be passed to new.

data - the Hash to check for mandatory fields

Returns true if the Hash is valid, false otherwise.

# File lib/ALD/item.rb, line 165
def self.valid_data?(data)
  data.is_a?(Hash) && initialized_attributes.all? { |k| data.key?(k) }
end

Private Instance Methods

request() click to toggle source

Internal: If the item was initialized with only mandatory data, use the API to request all missing information.

Returns nothing.

# File lib/ALD/item.rb, line 152
def request
  @data = @api.request("/items/#{id}")
  @data['uploaded'] = DateTime.parse(@data['uploaded'])
  @data['tags'].map!(&:to_sym)
  @data['user'] = @api.user(@data['user'])
end