Class: OfflineAddressBook

Inherits:
Object
  • Object
show all
Defined in:
lib/exchange-offline-address-book.rb,
lib/exchange-offline-address-book/parser.rb,
lib/exchange-offline-address-book/version.rb

Defined Under Namespace

Classes: Parser

Constant Summary collapse

VERSION =
'0.0.19'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email: nil, password: nil, username: nil, cachedir: nil, baseurl: nil, update: true) ⇒ OfflineAddressBook

Returns a new instance of OfflineAddressBook.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/exchange-offline-address-book.rb', line 30

def initialize(email: nil, password: nil, username: nil, cachedir: nil, baseurl: nil, update: true)
  @email = email
  @username = username || email
  @password = password
  @cachedir = cachedir
  @update = update
  @baseurl = baseurl

  if cachedir
    fetch_to(cachedir)
  else
    Dir.mktmpdir{|dir| fetch_to(dir) }
  end
end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



45
46
47
# File 'lib/exchange-offline-address-book.rb', line 45

def records
  @records
end

Instance Method Details

#baseurlObject



47
48
49
50
51
52
53
54
# File 'lib/exchange-offline-address-book.rb', line 47

def baseurl
  @baseurl ||= begin
    client = Autodiscover::Client.new(email: @email, password: @password, username: @username)
    data = client.autodiscover(ignore_ssl_errors: true)
    raise "No data" unless data
    data.response['Account']['Protocol'].detect{|p| p['OABUrl'] && p['Type'] == 'EXPR'}['OABUrl']
  end
end

#fetch_to(dir) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/exchange-offline-address-book.rb', line 61

def fetch_to(dir)
  begin
    @dir = dir

    if File.file?(cache)
      @records = JSON.parse(open(cache).read, object_class: Hashie::Mash)
      return
    end

    if !File.file?(addressbook)
      %w{json oab lzx}.each{|ext|
        Dir[File.join(@dir, "*.#{ext}")].each{|f| File.delete(f) }
      }
      lzx = File.basename(addressbook, File.extname(addressbook)) + '.lzx'
      puts "OfflineAddressBook: Downloading #{lzx}" if ENV['DEBUG']
      download(lzx)
      puts "OfflineAddressBook: Decompressing #{lzx} to #{addressbook}" if ENV['DEBUG']
      LibMsPack.oab_decompress(File.join(@dir, lzx), addressbook)
    end
    puts "OfflineAddressBook: Addressbook ready at #{addressbook}" if ENV['DEBUG']

    parsed = Parser.new(addressbook)
    @records = parsed.records.collect{|record|
      record.to_h.each_pair{|k, v|
        record[k] = v[0] if v.length == 1
      }
      # no idea what's going on here
      record.AddressBookObjectGuid = record.AddressBookObjectGuid.inspect if record.AddressBookObjectGuid
      record
    }
    open(cache, 'w'){|f| f.write(JSON.pretty_generate(@records)) } if @cachedir
  ensure
    @dir = nil
  end
end

#headerObject



56
57
58
59
# File 'lib/exchange-offline-address-book.rb', line 56

def header
  @parsed ||= Parser.new(addressbook)
  @parsed.header
end