class Contacts::Yahoo

Constants

CONSUMER_OPTIONS
REQUEST_TOKEN_PARAMS

Public Class Methods

new(options={}) click to toggle source
Calls superclass method Contacts::OAuthConsumer::new
# File lib/contacts/yahoo.rb, line 15
def initialize(options={})
  super(CONSUMER_OPTIONS, REQUEST_TOKEN_PARAMS)
end

Public Instance Methods

contacts(options={}) click to toggle source
# File lib/contacts/yahoo.rb, line 26
def contacts(options={})
  return nil if @access_token.nil?
  params = {:limit => 200}.update(options)
  yahoo_params = translate_contacts_options(params).merge('format' => 'json')
  guid = @access_token.params['xoauth_yahoo_guid']
  uri = URI.parse("http://social.yahooapis.com/v1/user/#{guid}/contacts")
  uri.query = params_to_query(yahoo_params)
  begin
    response = @access_token.get(uri.to_s)
  rescue OAuth::Unauthorized => error
    # Token probably expired.
    @error = error.message
    return nil
  end
  parse_contacts(response.body)
end
initialize_serialized(data) click to toggle source
# File lib/contacts/yahoo.rb, line 19
def initialize_serialized(data)
  super
  if @access_token && (guid = data['guid'])
    @access_token.params['xoauth_yahoo_guid'] = guid
  end
end
serializable_data() click to toggle source
# File lib/contacts/yahoo.rb, line 43
def serializable_data
  data = super
  data['guid'] = @access_token.params['xoauth_yahoo_guid'] if @access_token
  data
end

Private Instance Methods

parse_contacts(text) click to toggle source
# File lib/contacts/yahoo.rb, line 66
def parse_contacts(text)
  result = JSON.parse(text)
  if result['contacts']
  result['contacts']['contact'].map do |contact_object|
    name, emails = nil, []
    contact_object['fields'].each do |field_object|
      case field_object['type']
      when 'nickname'
        name = field_object['value']
      when 'name'
        name ||= field_object['value'].values_at('givenName', 'familyName').compact.join(' ')
      when 'email'
        emails << field_object['value']
      end
    end
    next if emails.empty?
    Contact.new(emails, name)
  end.compact
  else
    []
  end
end
translate_contacts_options(options) click to toggle source
# File lib/contacts/yahoo.rb, line 51
def translate_contacts_options(options)
  params = {}
  value = options[:limit] and
    params['count'] = value
  value = options[:offset] and
    params['start'] = value
  params['sort'] = (value = options[:descending]) ? 'desc' : 'asc'
  params['sort-fields'] = 'email'
  # :updated_after not supported. Yahoo! appears to support
  # filtering by updated, but does not support a date comparison
  # operation. Lame. TODO: filter unwanted records out of the
  # response ourselves.
  params
end