Sha256: d302afc47ea8260317076fef578b2dbe891f5f984ceec55a463dde73cc2c0fa5

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# 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(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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
phone_sites-0.1.2 lib/phone_sites/gsm_arena/manufacturer.rb
phone_sites-0.1.1 lib/phone_sites/gsm_arena/manufacturer.rb
phone_sites-0.1.0 lib/phone_sites/gsm_arena/manufacturer.rb