class Firebrew::AmoApi::Search

Attributes

data[R]

Public Class Methods

connection() click to toggle source
# File lib/firebrew/amo_api/search.rb, line 12
def self.connection
  @connection ||= Faraday.new(url: 'https://services.addons.mozilla.org')
end
connection=(val) click to toggle source
# File lib/firebrew/amo_api/search.rb, line 8
def self.connection=(val)
  @connection = val
end
fetch(params={}) click to toggle source
# File lib/firebrew/amo_api/search.rb, line 28
def self.fetch(params={})
  response = self.connection.get(self.path params)
  raise Firebrew::NetworkError, "Invalid HTTP status: #{response.status}" unless response.status == 200
  dom = REXML::Document.new response.body
  addons = REXML::XPath.match(dom, '/searchresults/addon')
  addons.map{|v| Search.new v}
  
rescue Faraday::Error => e
  m = e.message
  m[0] = m[0].upcase
  raise Firebrew::NetworkError, m
end
fetch!(params={}) click to toggle source
# File lib/firebrew/amo_api/search.rb, line 41
def self.fetch!(params={})
  results = self.fetch(params)
  raise Firebrew::ExtensionNotFoundError, %[Extension not found: like "#{params[:term]}"] if results.empty?
  results
end
new(data) click to toggle source
Calls superclass method Firebrew::Entity::new
# File lib/firebrew/amo_api/search.rb, line 49
def initialize(data)
  @data = data
  
  val = lambda do |name|
    REXML::XPath.match(self.data, "#{name}/text()").first.value.strip
  end
  
  super(
    name: val[:name],
    guid: val[:guid],
    uri: val[:install],
    version: val[:version]
  )
end
path(params={}) click to toggle source
# File lib/firebrew/amo_api/search.rb, line 16
def self.path(params={})
  path_source = '/ja/firefox/api/%{api_version}/search/%{term}/%{type}/%{max}/%{os}/%{version}'
  default_params = {
    api_version: 1.5,
    type: 'all',
    max: 30,
    os: 'all',
    version: '',
  }
  URI.encode(path_source % default_params.merge(params))
end