Sha256: bdaa02f4465f8d17f4326e643af82caf44bfeaf2cb14ce5b471ed75614c70ece

Contents?: true

Size: 1.84 KB

Versions: 2

Compression:

Stored size: 1.84 KB

Contents

require 'json'

module EasyGravatar
  class JsonParser

    def self.for(json)
      new json
    end

    def initialize(json = '')
      @json = remove_first_layers(json)
    end

    def parse
      hash = strip_basic_fields
      hash.merge! strip_name_data
      hash.merge! strip_ims
      hash.merge! strip_profileBackground
      hash.merge! strip_phoneNumbers
      hash.merge! strip_emails
      hash.merge! strip_currency
    end

    private

    def remove_first_layers(json)
      JSON.parse(json)['entry'][0]
    end

    def strip_basic_fields
      hash = Hash.new
      @json.keys.each do |key|
        hash[key.to_sym] = @json[key] if @json[key].class == String
      end
      hash
    end

    def strip_ims
      strip_basic_group('ims')
    end

    def strip_phoneNumbers
      strip_basic_group('phoneNumbers')
    end

    def strip_currency
      strip_basic_group('currency')
    end

    def strip_name_data
      hash = Hash.new
      return hash unless @json['name']
      @json['name'].keys.each do |key|
        new_key = key
        new_key = "#{key}Name" if key == 'formatted'
        hash[new_key.to_sym] = @json['name'][key]
      end
      hash
    end

    def strip_profileBackground
      hash = Hash.new
      return hash unless @json['profileBackground']
      hash[:profileBackgroundColor] = @json['profileBackground']['color']
      hash
    end

    def strip_emails
      hash = Hash.new
      return hash unless @json['emails']
      array = Array.new
      @json['emails'].each do |h|
        array.push(h['value'])
      end
      hash[:email] = array
      hash
    end

    def strip_basic_group(group)
      hash = Hash.new
      return hash unless @json[group]

      hash[group.to_sym] = Hash.new
      @json[group].each do |j|
        hash[group.to_sym][j['type'].to_sym] = j['value']
      end
      hash
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
easy_gravatar-1.0.1 lib/easy_gravatar/json_parser.rb
easy_gravatar-1.0.0 lib/easy_gravatar/json_parser.rb