# frozen_string_literal: true require 'uri' module PhoneSites module GsmArena class Manufacturer < PhoneSites::Manufacturer 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 def models pages_count.times.inject([]) do |sum, i| sum.concat(self.class.models_response.perform(self, build_url(i))) end end def pages_count (count.to_f / PhoneSites::GsmArena::PhoneModel::ITEMS_PER_PAGE).ceil end 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 def self.manufacturer_response GsmArena::ManufacturersListResponse.new end def self.models_response PhoneSites::GsmArena::ModelListResponse.new end def self.manufacturers_url ::URI.join(GsmArena::BASE_URL, GsmArena::MANUFACTURERS_URL).to_s end def self.base_url GsmArena::BASE_URL end def self.all url = manufacturers_url manufacturer_response.perform(url) end end end end