Class: PhoneSites::GsmArena::Manufacturer

Inherits:
Manufacturer show all
Defined in:
lib/phone_sites/gsm_arena/manufacturer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, parseable_attributes

Constructor Details

This class inherits a constructor from PhoneSites::Base

Class Method Details

.allObject


52
53
54
55
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 52

def self.all
  url = manufacturers_url
  manufacturer_response.perform(url)
end

.base_urlObject


48
49
50
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 48

def self.base_url
  GsmArena::BASE_URL
end

.manufacturer_responseObject


36
37
38
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 36

def self.manufacturer_response
  GsmArena::ManufacturersListResponse.new
end

.manufacturers_urlObject


44
45
46
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 44

def self.manufacturers_url
  ::URI.join(GsmArena::BASE_URL, GsmArena::MANUFACTURERS_URL).to_s
end

.models_responseObject


40
41
42
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 40

def self.models_response
  PhoneSites::GsmArena::ModelListResponse.new
end

Instance Method Details

#build_url(page) ⇒ Object


31
32
33
34
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 31

def build_url(page)
  url_end = page.zero? ? url : url.gsub(".php", "-#{page}.php")
  URI.join(self.class.base_url, url_end).to_s
end

#modelsObject


21
22
23
24
25
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 21

def models
  pages_count.times.inject([]) do |sum, i|
    sum.concat(self.class.models_response.perform(build_url(i)))
  end
end

#pages_countObject


27
28
29
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 27

def pages_count
  (count.to_f / PhoneSites::GsmArena::PhoneModel::ITEMS_PER_PAGE).ceil
end

#parse(data, attr_name) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/phone_sites/gsm_arena/manufacturer.rb', line 7

def parse(data, attr_name)
  case attr_name
  when :name
    data.children.select { |x| x.is_a?(Nokogiri::XML::Text) }.first.text
  when :url
    data.get_attribute("href")
  when :count
    data.children.select { |x| x.is_a?(Nokogiri::XML::Element) && x.name == "span" }
        .first.children.first.text.split(" ").first.to_i
  else
    raise NotImplementedError
  end
end