class EasyGravatar::JsonParser

Public Class Methods

for(json) click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 6
def self.for(json)
  new json
end
new(json = '') click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 10
def initialize(json = '')
  @json = remove_first_layers(json)
end

Public Instance Methods

parse() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 14
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 Instance Methods

remove_first_layers(json) click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 26
def remove_first_layers(json)
  JSON.parse(json)['entry'][0]
end
strip_basic_fields() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 30
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
strip_basic_group(group) click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 79
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
strip_currency() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 46
def strip_currency
  strip_basic_group('currency')
end
strip_emails() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 68
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
strip_ims() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 38
def strip_ims
  strip_basic_group('ims')
end
strip_name_data() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 50
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
strip_phoneNumbers() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 42
def strip_phoneNumbers
  strip_basic_group('phoneNumbers')
end
strip_profileBackground() click to toggle source
# File lib/easy_gravatar/json_parser.rb, line 61
def strip_profileBackground
  hash = Hash.new
  return hash unless @json['profileBackground']
  hash[:profileBackgroundColor] = @json['profileBackground']['color']
  hash
end