Class: GCal4Ruby::Service

Inherits:
Base
  • Object
show all
Defined in:
lib/gcal4ruby/service.rb

Overview

The service class is the main handler for all direct interactions with the Google Calendar API. A service represents a single user account. Each user account can have multiple calendars, so you’ll need to find the calendar you want from the service, using the Calendar#find class method.

Usage

  1. Authenticate service = Service.new service.authenticate(“[email protected]”, “password”)

  2. Get Calendar List calendars = service.calendars

Constant Summary collapse

AUTH_TYPE_OAUTH =
'OAuth'

Constants inherited from Base

Base::AUTH_URL, Base::CALENDAR_LIST_FEED

Instance Attribute Summary collapse

Attributes inherited from Base

#debug, #proxy_info

Instance Method Summary collapse

Methods inherited from Base

#do_delete, #do_get, #do_post, #do_put, #send_delete, #send_get, #send_post, #send_put

Constructor Details

#initialize(attributes = {}) ⇒ Service

Returns a new instance of Service.



25
26
27
28
29
30
31
# File 'lib/gcal4ruby/service.rb', line 25

def initialize(attributes = {})
  super()
  attributes.each do |key, value|
    self.send("#{key}=", value)
  end    
  @check_public ||= true
end

Instance Attribute Details

#accountObject

Returns the value of attribute account.



23
24
25
# File 'lib/gcal4ruby/service.rb', line 23

def 
  @account
end

#auth_tokenObject

Returns the value of attribute auth_token.



23
24
25
# File 'lib/gcal4ruby/service.rb', line 23

def auth_token
  @auth_token
end

#auth_typeObject

Returns the value of attribute auth_type.



23
24
25
# File 'lib/gcal4ruby/service.rb', line 23

def auth_type
  @auth_type
end

#check_publicObject

Returns the value of attribute check_public.



23
24
25
# File 'lib/gcal4ruby/service.rb', line 23

def check_public
  @check_public
end

Instance Method Details

#authenticate(username, password) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gcal4ruby/service.rb', line 33

def authenticate(username, password)
  ret = nil
  ret = send_post(AUTH_URL, "Email=#{username}&Passwd=#{password}&source=GCal4Ruby&service=cl&accountType=HOSTED_OR_GOOGLE")
  if ret.class == Net::HTTPOK
    @auth_token = ret.read_body.to_a[2].gsub("Auth=", "").strip
    @account = username
    @auth_type = 'ClientLogin'
    return true
  else
    raise AuthenticationFailed
  end
end

#authsub_authenticate(authsub_token, account) ⇒ Object

added authsub authentication. pass in the upgraded authsub token and the username/email address



47
48
49
50
51
52
# File 'lib/gcal4ruby/service.rb', line 47

def authsub_authenticate(authsub_token, )
  @auth_token = authsub_token
  @account = 
  @auth_type = 'AuthSub'
  return true
end

#calendarsObject

Returns an array of Calendar objects for each calendar associated with the authenticated account.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gcal4ruby/service.rb', line 65

def calendars
  unless @auth_token
    raise NotAuthenticated
  end
  ret = send_get(CALENDAR_LIST_FEED+"?max-results=100")
  cals = []
  REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
    entry.attributes["xmlns:gCal"] = "http://schemas.google.com/gCal/2005"
    entry.attributes["xmlns:gd"] = "http://schemas.google.com/g/2005"
    entry.attributes["xmlns:app"] = "http://www.w3.org/2007/app"
    entry.attributes["xmlns"] = "http://www.w3.org/2005/Atom"
    cal = Calendar.new(self)
    cal.load("<?xml version='1.0' encoding='UTF-8'?>#{entry.to_s}")
    cals << cal
  end
  return cals
end

#oauth?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/gcal4ruby/service.rb', line 59

def oauth?
  @auth_type == AUTH_TYPE_OAUTH
end

#oauth_authenticate(access_token) ⇒ Object



54
55
56
57
# File 'lib/gcal4ruby/service.rb', line 54

def oauth_authenticate(access_token)
  @auth_token = access_token
  @auth_type = AUTH_TYPE_OAUTH
end

#to_iframe(cals, params = {}) ⇒ Object

This is for building a composite calendar I’m sure it doesn’t work, needs review!



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gcal4ruby/service.rb', line 85

def to_iframe(cals, params = {})
  calendar_set = case cals
    when :all then calendars
    when :first then calendars[0]
    else cals
  end

  units = calendar_set.collect do |cal|
    "src=#{cal.id}" + cal.build_options_set(params).join("&amp;")
  end
        
  "<iframe src='http://www.google.com/calendar/embed?#{units.join("&amp;")}' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
end