class Helium::Collection
Attributes
Public Class Methods
# 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
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
# File lib/helium/collection.rb, line 64 def -(other) collection - other end
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
# File lib/helium/collection.rb, line 68 def [](index) collection[index] end
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
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
Returns an array of the Resources belonging to the Collection
@return [Resource]
# File lib/helium/collection.rb, line 42 def collection fetch_collection end
# File lib/helium/collection.rb, line 46 def each collection.each{ |element| yield element } end
# File lib/helium/collection.rb, line 50 def inspect collection end
NOTE: if we implement pagination, we'll need to rethink this
# File lib/helium/collection.rb, line 79 def last collection.last end
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
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
# File lib/helium/collection.rb, line 54 def to_json(*options) collection.to_json(*options) end
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
# 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
# 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
# File lib/helium/collection.rb, line 116 def fetch_collection response = @client.get(resource_path) return collection_from_response(response) end
# File lib/helium/collection.rb, line 121 def filter_param "filter[metadata]=#{@filter_criteria.to_json}" end
# 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
# 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
# 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