class Enc::CollinsHelper::Api

Public Instance Methods

logger() click to toggle source

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

# File lib/enc/collins_helper/api.rb, line 7
def logger
  Enc::Utils::Logging.logger_for(self.class.name)
end
safe_find(options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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