class Helium::Collection

Attributes

filter_criteria[R]

Public Class Methods

new(opts) click to toggle source
# File lib/helium/collection.rb, line 7
def initialize(opts)
  @client     = opts.fetch(:client)
  @klass      = opts.fetch(:klass)
  @belongs_to = opts.fetch(:belongs_to, nil)

  @filter_criteria = {}
end

Public Instance Methods

+(other) click to toggle source

TODO: could support something like label.sensors << new_sensor see Label#add_sensors for where it would be applied

# File lib/helium/collection.rb, line 60
def +(other)
  collection + other
end
-(other) click to toggle source
# File lib/helium/collection.rb, line 64
def -(other)
  collection - other
end
==(other) click to toggle source

Collections are considered equal if they contain the same resources as determined by the resources' ids

# File lib/helium/collection.rb, line 74
def ==(other)
  self.map(&:id).sort == other.map(&:id).sort
end
[](index) click to toggle source
# File lib/helium/collection.rb, line 68
def [](index)
  collection[index]
end
add_relationships(items) click to toggle source

Adds resources of the same type to the collection related to the '@belongs_to' resource

# File lib/helium/collection.rb, line 85
def add_relationships(items)
  body = relationship_request_body(items)
  @client.post(relationship_path, body: body)
end
all() click to toggle source

Returns all resources @option opts [Client] :client A Helium::Client @return [Helium::Collection] a Collection of all of the given Resource

# File lib/helium/collection.rb, line 18
def all
  @filter_criteria = {}
  self
end
collection() click to toggle source

Returns an array of the Resources belonging to the Collection @return [Resource]

# File lib/helium/collection.rb, line 42
def collection
  fetch_collection
end
each() { |element| ... } click to toggle source
# File lib/helium/collection.rb, line 46
def each
  collection.each{ |element| yield element }
end
inspect() click to toggle source
# File lib/helium/collection.rb, line 50
def inspect
  collection
end
last() click to toggle source

NOTE: if we implement pagination, we'll need to rethink this

# File lib/helium/collection.rb, line 79
def last
  collection.last
end
remove_relationships(items) click to toggle source

Removes resources of the same type to the collection related to the '@belongs_to' resource. An empty array removes all resources.

# File lib/helium/collection.rb, line 99
def remove_relationships(items)
  body = relationship_request_body(items)
  @client.delete(relationship_path, body: body)
end
replace_relationships(items) click to toggle source

Replaces resources of the same type to the collection related to the '@belongs_to' resource. An empty array removes all resources.

# File lib/helium/collection.rb, line 92
def replace_relationships(items)
  body = relationship_request_body(items)
  @client.patch(relationship_path, body: body)
end
to_json(*options) click to toggle source
# File lib/helium/collection.rb, line 54
def to_json(*options)
  collection.to_json(*options)
end
where(criteria) click to toggle source

Uses metadata filtering (docs.helium.com/api/v1/metadata/index.html#filtering) to search for a collection of resources matching the search parameters @param [Hash] criteria a set of search criteria

@example Search for sensors by location

client.sensors.where(location: 'Building B') #=> [Sensor, Sensor]

@example Search for multiple matching search parameters

client.sensors.where(departments: ['it', 'engineering']) #=> [Sensor, Sensor]

@return [Collection] a collection of resources matching the provided search criteria

# File lib/helium/collection.rb, line 35
def where(criteria)
  add_filter_criteria(criteria)
  self
end

Protected Instance Methods

add_filter_criteria(criteria) click to toggle source
# File lib/helium/collection.rb, line 106
def add_filter_criteria(criteria)
  criteria.each do |key, value|
    if existing_value = @filter_criteria[key]
      @filter_criteria[key] = (Array(existing_value) + Array(value)).uniq
    else
      @filter_criteria[key] = value
    end
  end
end
collection_from_response(response) click to toggle source
# File lib/helium/collection.rb, line 149
def collection_from_response(response)
  resources_data = JSON.parse(response.body)["data"]

  resources = resources_data.map do |resource_data|
    @klass.new(client: @client, params: resource_data)
  end

  return resources
end
fetch_collection() click to toggle source
# File lib/helium/collection.rb, line 116
def fetch_collection
  response = @client.get(resource_path)
  return collection_from_response(response)
end
filter_param() click to toggle source
# File lib/helium/collection.rb, line 121
def filter_param
  "filter[metadata]=#{@filter_criteria.to_json}"
end
relationship_path() click to toggle source
# File lib/helium/collection.rb, line 139
def relationship_path
  if @belongs_to
    URI.parse("#{@belongs_to.resource_path}/relationships/#{@klass.resource_name}").to_s
  else
    raise Helium::Error.new(
      "The collection must be associated with a resource to modify the
      relationship")
  end
end
relationship_request_body(items) click to toggle source
# File lib/helium/collection.rb, line 159
def relationship_request_body(items)
  items = Array(items)
  if items.all? {|item| item.is_a? @klass }
    new_items_data = items.map do |item|
      {
        id: item.id,
        type: "#{@klass.resource_name}"
      }
    end
    { data: new_items_data }
  else
    raise Helium::Error.new(
      "All items added to the collection must be of type " + @klass.to_s)
  end

end
resource_path() click to toggle source
# File lib/helium/collection.rb, line 125
def resource_path
  uri = if @belongs_to
          URI.parse("#{@belongs_to.resource_path}/#{@klass.resource_name}")
        else
          URI.parse(@klass.all_path)
        end

  if @filter_criteria.any?
    uri.query = [uri.query, filter_param].compact.join('&')
  end

  uri.to_s
end