Sha256: 3f985d041b90ac47f86599b88f4a1b5dcf485f0a33f28996aeb111c0b8386acd

Contents?: true

Size: 1.89 KB

Versions: 4

Compression:

Stored size: 1.89 KB

Contents

require 'uri'
require 'contacts/version'

module Contacts

  Identifier = 'Ruby Contacts v' + VERSION::STRING

  def self.configure(configuration)
    configuration.each do |key, value|
      klass =
        case key.to_s
        when 'google'
          Google
        when 'yahoo'
          Yahoo
        when 'windows_live'
          WindowsLive
        else
          raise ArgumentError, "unknown consumer: #{key}"
        end
      klass.configure(value)
    end
  end

  class Contact
    attr_reader :name, :username, :emails

    def initialize(name, emails)
      @name = name
      @emails = Array(emails)
    end

    def email
      @emails.first
    end
  end

  def self.deserialize_consumer(name, serialized_data)
    klass = consumer_class_for(name) and
      klass.deserialize(serialized_data)
  end

  def self.new(name, *args, &block)
    klass = consumer_class_for(name) and
      klass.new(*args, &block)
  end

  def self.consumer_class_for(name)
    class_name = name.to_s.gsub(/(?:\A|_)(.)/){|s| $1.upcase}
    class_name.sub!(/Oauth/, 'OAuth')
    class_name.sub!(/Bbauth/, 'BBAuth')
    begin
      klass = const_get(class_name)
    rescue NameError
      return nil
    end
    klass < Consumer ? klass : nil
  end

  def self.verbose?
    'irb' == $0
  end
  
  class Error < StandardError
  end
  
  class TooManyRedirects < Error
    attr_reader :response, :location
    
    MAX_REDIRECTS = 2
    
    def initialize(response)
      @response = response
      @location = @response['Location']
      super "exceeded maximum of #{MAX_REDIRECTS} redirects (Location: #{location})"
    end
  end

  autoload :Util, 'contacts/util'
  autoload :Consumer, 'contacts/consumer'
  autoload :OAuthConsumer, 'contacts/oauth_consumer'
  autoload :Google, 'contacts/google'
  autoload :Yahoo, 'contacts/yahoo'
  autoload :WindowsLive, 'contacts/windows_live'
  autoload :WindowsLiveLogin, 'contacts/windowslivelogin'
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lperichon-contacts-1.0.8 lib/contacts.rb
lperichon-contacts-1.0.7 lib/contacts.rb
lperichon-contacts-1.0.6 lib/contacts.rb
lperichon-contacts-1.0.5 lib/contacts.rb