Sha256: 31c4325ef875d042f99c6cf5409e79a1fa08c0e2f3185ded976c1e57037c188a

Contents?: true

Size: 996 Bytes

Versions: 1

Compression:

Stored size: 996 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

1 entries across 1 versions & 1 rubygems

Version Path
phone_sites-0.1.1 lib/phone_sites/gsm_arena/model_info_response.rb