Class: Firebrew::AmoApi::Search

Inherits:
Firefox::BasicExtension show all
Defined in:
lib/firebrew/amo_api/search.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Entity

#==, included

Constructor Details

#initialize(data) ⇒ Search

Returns a new instance of Search.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



47
48
49
# File 'lib/firebrew/amo_api/search.rb', line 47

def data
  @data
end

Class Method Details

.connectionObject



12
13
14
# File 'lib/firebrew/amo_api/search.rb', line 12

def self.connection
  @connection ||= Faraday.new(url: 'https://services.addons.mozilla.org')
end

.connection=(val) ⇒ Object



8
9
10
# File 'lib/firebrew/amo_api/search.rb', line 8

def self.connection=(val)
  @connection = val
end

.fetch(params = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# 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 = {}) ⇒ Object



41
42
43
44
45
# 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

.path(params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# 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