Module: Collins::Api::Attributes

Included in:
Collins::Api
Defined in:
lib/collins/api/attributes.rb

Instance Method Summary collapse

Instance Method Details

#delete_attribute!(asset_or_tag, attribute, group_id = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/collins/api/attributes.rb', line 5

def delete_attribute! asset_or_tag, attribute, group_id = nil
  asset = get_asset_or_tag asset_or_tag
  parameters = {
    :groupId => group_id
  }
  parameters = select_non_empty_parameters parameters
  logger.debug("Deleting attribute #{attribute} on #{asset.tag} with params #{parameters.inspect}")
  http_delete("/api/asset/#{asset.tag}/attribute/#{attribute}", parameters, asset.location) do |response|
    parse_response response, :expects => 202, :as => :status, :raise => strict?, :default => false
  end
end

#set_attribute!(asset_or_tag, key, value, group_id = nil) ⇒ Object



17
18
19
# File 'lib/collins/api/attributes.rb', line 17

def set_attribute! asset_or_tag, key, value, group_id = nil
  set_multi_attribute! asset_or_tag, {key => value}, group_id
end

#set_multi_attribute!(asset_or_tag, kv_hash, group_id = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/collins/api/attributes.rb', line 21

def set_multi_attribute! asset_or_tag, kv_hash, group_id = nil
  asset = get_asset_or_tag asset_or_tag
  http_overrides = {}
  parameters = {
    :groupId => group_id,
    :attribute => []
  }
  kv_hash.each do |key,value|
    if ::Collins::Asset::Update.is_attribute?(key) then
      parameters[:attribute] << "#{key};#{value}"
    else
      p = ::Collins::Asset::Update.get_param(key)
      parameters[p.to_sym] = ::Collins::Asset::Update.get_param_value(key, value)
    end
  end
  if parameters[:attribute].empty? then
    parameters[:attribute] = nil
  elsif parameters[:attribute].size == 1 then
    parameters[:attribute] = parameters[:attribute].first
  else
    http_overrides[:query_string_normalizer] = HTTParty::Request::NON_RAILS_QUERY_STRING_NORMALIZER
  end
  parameters = select_non_empty_parameters parameters
  logger.debug("Setting attributes #{parameters.inspect} on #{asset.tag}")
  parameters[:http_options] = http_overrides unless http_overrides.empty?
  http_post("/api/asset/#{asset.tag}", parameters, asset.location) do |response|
    parse_response response, :expects => 200, :as => :status, :raise => strict?, :default => false
  end
end

#set_status!(asset_or_tag, status, reason = 'Set via API', state = nil) ⇒ Object #set_status!(asset_or_tag, hash) ⇒ Object

Set the status of an asset

Overloads:

  • #set_status!(asset_or_tag, status, reason = 'Set via API', state = nil) ⇒ Object

    Set the status, reason and optionally state of asset

    Parameters:

    • asset_or_tag (String, Collins::Asset)

      The asset or tag

    • status (String)

      the status of the asset

    • reason (String) (defaults to: 'Set via API')

      the reason for the change

    • state (String) (defaults to: nil)

      the asset state

  • #set_status!(asset_or_tag, hash) ⇒ Object

    Set the status, reason, and optionally state of asset

    Parameters:

    • asset_or_tag (String, Collins::Asset)

      The asset or tag

    • hash (Hash)

      the options to set

    Options Hash (hash):

    • :status (String)

      The asset status

    • :reason (String)

      The reason for the change

    • :state (String)

      The asset state

Returns:

  • Boolean



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/collins/api/attributes.rb', line 66

def set_status! asset_or_tag, *varargs
  status = state = nil
  reason = 'Set via ruby client'
  asset = get_asset_or_tag asset_or_tag
  if varargs.size == 0 then
    raise ::Collins::ExpectationFailedError.new("set_status! requires a status")
  elsif varargs.size == 1 and varargs[0].is_a?(Hash) then
    hash = symbolize_hash(varargs[0], :downcase => true)
    status = hash[:status]
    reason = hash.fetch(:reason, reason)
    state = hash[:state]
  elsif varargs.size == 1 and (varargs[0].is_a?(String) or varargs[0].is_a?(Symbol)) then
    status = varargs[0].to_s
  elsif varargs.size > 1 then
    status = varargs[0]
    reason = varargs[1]
    state = varargs[2] if varargs.size > 2
  else
    raise ::Collins::ExpectationFailedError.new("set_status! called with invalid parameters")
  end
  parameters = {
    :status => status,
    :reason => reason,
    :state => state
  }
  parameters = select_non_empty_parameters parameters
  logger.debug("Setting status to #{status} on #{asset.tag}")
  http_post("/api/asset/#{asset.tag}/status", parameters, asset.location) do |response|
    parse_response response, :expects => 200, :as => :status, :raise => strict?, :default => false
  end
end