Class: Blackbook

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/blackbook.rb

Defined Under Namespace

Modules: Exporter, Importer Classes: BadCredentialsError, BlackbookError

Constant Summary collapse

VERSION =
'1.0.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlackbook

Returns a new instance of Blackbook.



59
60
61
62
# File 'lib/blackbook.rb', line 59

def initialize
  self.importers = {}
  self.exporters = {}
end

Instance Attribute Details

#exportersObject

Returns the value of attribute exporters.



13
14
15
# File 'lib/blackbook.rb', line 13

def exporters
  @exporters
end

#importersObject

Returns the value of attribute importers.



12
13
14
# File 'lib/blackbook.rb', line 12

def importers
  @importers
end

Class Method Details

.get(*args) ⇒ Object



15
16
17
# File 'lib/blackbook.rb', line 15

def self.get( *args )
  instance.get( *args )
end

.register(name, adapter_class) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/blackbook.rb', line 19

def self.register(name, adapter_class)
  case adapter = adapter_class.new
    when Importer::Base
      instance.importers[name.to_sym] = adapter
    when Exporter::Base
      instance.exporters[name.to_sym] = adapter
    else
      raise ArgumentError, "Unknown adapter"
  end
end

Instance Method Details

#export(importer, exporter, options) ⇒ Object

Sends the vcards from the import to whatever is handling the export



31
32
33
# File 'lib/blackbook.rb', line 31

def export( importer, exporter, options )
  exporter.export importer.import( options )
end

#find_importer(options) ⇒ Object

Searches registered importers for one that will handle the given options



36
37
38
39
# File 'lib/blackbook.rb', line 36

def find_importer( options )
  importers.each{ |key, importer| return importer if importer =~ options }
  nil
end

#get(*args) ⇒ Object

Fetches contacts from various services or filetypes. The default is to return an array of hashes - Blackbook’s internal format

Handles several different calls:

get( :username => '[email protected]', :password => 'whatever' )
get( :as => :xml, :username => '[email protected]', :password => 'whatever' )
get( :csv, :file => #<File:/path/to/file.csv> )

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
# File 'lib/blackbook.rb', line 48

def get( *args )
  options   = args.last.is_a?(Hash) ? args.pop : {}
  to_format = exporters[ options[:as] || :basic ]
  source    = (importers[args.first.to_sym] rescue nil) || find_importer(options)

  raise ArgumentError, "Unknown exporter" unless to_format
  raise ArgumentError, "Unknown source" unless source
  
  export source, to_format, options
end