Class: Graybook

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

Defined Under Namespace

Modules: Exporter, Importer Classes: Problem

Constant Summary collapse

VERSION =
'1.1.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraybook

Returns a new instance of Graybook.



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

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

Instance Attribute Details

#exportersObject

Returns the value of attribute exporters.



16
17
18
# File 'lib/graybook.rb', line 16

def exporters
  @exporters
end

#importersObject

Returns the value of attribute importers.



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

def importers
  @importers
end

Class Method Details

.get(*args) ⇒ Object



18
19
20
# File 'lib/graybook.rb', line 18

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

.register(name, adapter_class) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/graybook.rb', line 22

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
      return nil
  end
end

Instance Method Details

#export(importer, exporter, options) ⇒ Object



33
34
35
# File 'lib/graybook.rb', line 33

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



38
39
40
41
# File 'lib/graybook.rb', line 38

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 - Graybook’s internal format

Handles several different calls:

get( :username => '[email protected]', :password => 'whatever' )


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

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

  return nil unless to_format
  return nil unless source

  export source, to_format, options
end