Sha256: ac9ae67b2f16b5e9c30446124dc43bd3f3afe3ffc7b54400415136bb78c9d83f

Contents?: true

Size: 1011 Bytes

Versions: 4

Compression:

Stored size: 1011 Bytes

Contents

# frozen_string_literal: true
module PhoneSites
  module GsmArena
    class ModelInfoResponse < ::PhoneSites::BaseResponse
      def perform(url, method = 'get', options = {})
        response = super(url, method, options)
        body = Nokogiri::HTML(response.body)
        body.css('#specs-list table tr td')
            .map(&:text)
            .each_slice(2)
            .each_with_object({}) do |(key, value), hash|
          properties = normalize_property(key, value)
          hash.merge!(properties) if valid?(key)
        end
      end

      private
      def normalize_property_value(value)
        val = value.tr("\r\n ", '').strip
        case val
          when "Yes"
            true
          when "No"
            false
          when "-"
            nil
          else
            val
        end
      end

      def normalize_property(key, value)
        { key.strip => normalize_property_value(value) }
      end

      def valid?(key)
        key.length != 1
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
phone_sites-0.1.0 lib/phone_sites/gsm_arena/model_info_response.rb
phone_sites-0.0.5 lib/phone_sites/gsm_arena/model_info_response.rb
phone_sites-0.0.5a lib/phone_sites/gsm_arena/model_info_response.rb
phone_sites-0.0.4 lib/phone_sites/gsm_arena/model_info_response.rb