Class: Enc::CollinsHelper::Api

Inherits:
Collins::Client
  • Object
show all
Defined in:
lib/enc/collins_helper/api.rb

Defined Under Namespace

Classes: AssetNotConfigured, AssetNotValid, CannotConnect, NoAssets, TooManyAssets

Instance Method Summary collapse

Instance Method Details

#loggerObject

The logger method is used by Collins::Client, so an override is needed rather than a simple include.



7
8
9
# File 'lib/enc/collins_helper/api.rb', line 7

def logger
  Enc::Utils::Logging.logger_for(self.class.name)
end

#safe_find(options = {}) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/enc/collins_helper/api.rb', line 17

def safe_find(options = {})
  query = asset_hash_to_find_query(options)
  params = query.to_a.map do |param|
    (key, val) = param
    if val.is_a?(Array)
      val.map{|v| "#{key}=#{asset_escape_attribute(v)}"}.join('&')
    else
      "#{key}=#{asset_escape_attribute(val)}"
    end
  end.reject{|s| s.empty?}
  logger.debug("Searching for asset using query: #{params.join('&')}")
  begin
    response = http_get('/api/assets', params) do |r|
      parse_response(r, :expects => 200, :as => :paginated)
    end
  rescue SocketError, TimeoutError => e
    raise CannotConnect, e
  end
  logger.debug("Got response: #{response.inspect}")
  raise TooManyAssets, 'Too many assets' if response.count > 1
  raise NoAssets, 'No assets found' if response.first.nil? or response.first.empty?
  asset = CollinsHelper::Node::NodeAsset.from_json(response.first)
  raise AssetNotConfigured, 'Asset is missing required tags' if (not asset.is_valid? and asset.uses_enc?)
  raise AssetNotValid, 'Asset is not valid' unless asset.is_valid?
  asset
end

#safe_find_exact(options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/enc/collins_helper/api.rb', line 44

def safe_find_exact(options = {})
  exact_options = {}
  options.each do |k,v|
    if v.is_a?String
      exact_options[k] = "^#{v}$"
    else
      exact_options[k] = v
    end
  end
  safe_find(exact_options)
end