Class: ICloudReader

Inherits:
Object
  • Object
show all
Defined in:
lib/icloud-reader.rb,
lib/icloud-reader/version.rb

Constant Summary collapse

PRINCIPAL_URL_XML =
%Q{<A:propfind xmlns:A='DAV:'>
  <A:prop>
  <A:current-user-principal/>
  </A:prop>
  </A:propfind>
}
CALENDARS_XML =
%Q{  <A:propfind xmlns:A='DAV:'>
  <A:prop>
  <A:displayname/>
  </A:prop>
  </A:propfind>
}
VERSION =
"1.0.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ICloudReader

Returns a new instance of ICloudReader.



25
26
27
28
29
30
31
32
# File 'lib/icloud-reader.rb', line 25

def initialize(options = {})
  @server_number = options[:server_number] || 1
  @client = HTTPClient.new
  @username = options[:username]
  @password = options[:password]
  @user_id = load_user_id
  @calendars = load_calendars
end

Instance Attribute Details

#calendarsObject (readonly)

Returns the value of attribute calendars.



23
24
25
# File 'lib/icloud-reader.rb', line 23

def calendars
  @calendars
end

#user_idObject (readonly)

Returns the value of attribute user_id.



23
24
25
# File 'lib/icloud-reader.rb', line 23

def user_id
  @user_id
end

Instance Method Details

#base_calendars_urlObject



34
35
36
# File 'lib/icloud-reader.rb', line 34

def base_calendars_url
  "https://p0#{@server_number}-caldav.icloud.com"
end

#base_contacts_urlObject



38
39
40
# File 'lib/icloud-reader.rb', line 38

def base_contacts_url
  "https://p0#{@server_number}-contacts.icloud.com"
end

#calendars_urlObject



42
43
44
# File 'lib/icloud-reader.rb', line 42

def calendars_url
  URI.join(base_calendars_url, "#{user_id}/calendars/").to_s 
end

#contacts_urlObject



46
47
48
# File 'lib/icloud-reader.rb', line 46

def contacts_url
  URI.join(base_contacts_url, "#{user_id}/carddavhome/card/").to_s
end

#load_calendarsObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/icloud-reader.rb', line 55

def load_calendars   
  document = xml_request(calendars_url, CALENDARS_XML)  
  calendars = {}

  document.search("response").each do |response|
    path = response.search("href").first.text
    url = URI.join(calendars_url, path)
    displayName = response.search("displayname").first
    calendars[displayName.text] = url.to_s if displayName
  end

  calendars
end

#load_user_idObject



50
51
52
53
# File 'lib/icloud-reader.rb', line 50

def load_user_id
  document = xml_request(base_calendars_url, PRINCIPAL_URL_XML)
  document.search("current-user-principal href").first.text.split("/")[1]    
end