Class: Exchange::OfflineAddressBook::AddressBook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/exchange-offline-address-book.rb', line 21

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.



36
37
38
# File 'lib/exchange-offline-address-book.rb', line 36

def records
  @records
end

Instance Method Details

#baseurlObject



38
39
40
41
42
43
44
45
# File 'lib/exchange-offline-address-book.rb', line 38

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

#headerObject



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

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

#load(file) ⇒ Object



52
53
54
55
# File 'lib/exchange-offline-address-book.rb', line 52

def load(file)
  raise "Not loading temporary file" unless @cachedir
  @records = JSON.parse(open(file).read, object_class: Exchange::OfflineAddressBook::Record)
end

#save(file) ⇒ Object



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

def save(file)
  raise "Not saving to temporary file" unless @cachedir
  open(cache, 'w'){|f| f.write(JSON.pretty_generate(@records)) }
end