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

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

#send_delete, #send_get, #send_post, #send_put

Constructor Details

#initializeService

Returns a new instance of Service.



32
33
34
35
# File 'lib/gcal4ruby/service.rb', line 32

def initialize
  super
  @check_public = true
end

Instance Attribute Details

#accountObject (readonly)

Convenience attribute contains the currently authenticated account name



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

def 
  @account
end

#auth_tokenObject (readonly)

The token returned by the Google servers, used to authorize all subsequent messages



25
26
27
# File 'lib/gcal4ruby/service.rb', line 25

def auth_token
  @auth_token
end

#check_publicObject

Determines whether GCal4Ruby ensures a calendar is public. Setting this to false can increase speeds by 50% but can cause errors if you try to do something to a calendar that is not public and you don’t have adequate permissions



30
31
32
# File 'lib/gcal4ruby/service.rb', line 30

def check_public
  @check_public
end

Instance Method Details

#authenticate(username, password) ⇒ Object

The authenticate method passes the username and password to google servers.

If authentication succeeds, returns true, otherwise raises the AuthenticationFailed error.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gcal4ruby/service.rb', line 39

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
    return true
  else
    raise AuthenticationFailed
  end
end

#calendarsObject

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gcal4ruby/service.rb', line 53

def calendars
  if not @auth_token
     raise NotAuthenticated
  end
  ret = send_get(CALENDAR_LIST_FEED)
  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

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

Helper function to return a formatted iframe embedded google calendar. Parameters are:

  1. cals: either an array of calendar ids, or :all for all calendars, or :first for the first (usally default) calendar

  2. params: a hash of parameters that affect the display of the embedded calendar:

height:: the height of the embedded calendar in pixels
width:: the width of the embedded calendar in pixels
title:: the title to display
bgcolor:: the background color.  Limited choices, see google docs for allowable values.
color:: the color of the calendar elements.  Limited choices, see google docs for allowable values.
showTitle:: set to 'false' to hide the title
showDate:: set to 'false' to hide the current date
showNav:: set to 'false to hide the navigation tools
showPrint:: set to 'false' to hide the print icon
showTabs:: set to 'false' to hide the tabs
showCalendars:: set to 'false' to hide the calendars selection drop down
showTimezone:: set to 'false' to hide the timezone selection
border:: the border width in pixels
dates:: a range of dates to display in the format of 'yyyymmdd/yyyymmdd'.  Example: 20090820/20091001
privateKey:: use to display a private calendar.  You can find this key under the calendar settings pane of the Google Calendar website.
colors:: a hash of calendar ids as key and color values as associated hash values.  Example: {'test@gmail.com' => '#7A367A'}


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gcal4ruby/service.rb', line 90

def to_iframe(cals, params = {})
  params[:height] ||= "600"
  params[:width] ||= "600"
  params[:title] ||= (self. ? self. : '')
  params[:bgcolor] ||= "#FFFFFF"
  params[:color] ||= "#2952A3"
  params[:showTitle] = params[:showTitle] == false ? "showTitle=0" : ''
  params[:showNav] = params[:showNav] == false ? "showNav=0" : ''
  params[:showDate] = params[:showDate] == false ? "showDate=0" : ''
  params[:showPrint] = params[:showPrint] == false ? "showPrint=0" : ''
  params[:showTabs] = params[:showTabs] == false ? "showTabs=0" : ''
  params[:showCalendars] = params[:showCalendars] == false ? "showCalendars=0" : ''
  params[:showTimezone] = params[:showTimezone] == false ? 'showTz=0' : ''
  params[:border] ||= "0"
  output = ''
  puts "params = #{params.inspect}" if self.debug
  params.each do |key, value|
    puts "key = #{key} and value = #{value}" if self.debug
    case key
      when :height then output += "height=#{value}"
      when :width then output += "width=#{value}"
      when :title then output += "title=#{CGI.escape(value)}"
      when :bgcolor then output += "bgcolor=#{CGI.escape(value)}"
      when :color then output += "color=#{CGI.escape(value)}"
      when :showTitle then output += value
      when :showDate then output += value
      when :showNav then output += value
      when :showPrint then output += value
      when :showTabs then output += value
      when :showCalendars then output += value
      when :showTimezone then output += value
      when :viewMode then output += "mode=#{value}"
      when :dates then output += "dates=#{CGI.escape(value)}"
      when :privateKey then output += "pvttk=#{value}"
    end
    output += "&amp;"
  end
  
  puts "output = #{output}" if self.debug
  
  if cals.is_a?(Array)
    for c in cals
      output += "src=#{c}&amp;"
      if params[:colors] and params[:colors][c]
        output += "color=#{CGI.escape(params[:colors][c])}&amp;"
      end
    end
  elsif cals == :all
    cal_list = calendars()
    for c in cal_list
      output += "src=#{c.id}&amp;"
    end
  elsif cals == :first
    cal_list = calendars()
    output += "src=#{cal_list[0].id}&amp;"
  end
      
  "<iframe src='http://www.google.com/calendar/embed?#{output}' style='#{params[:border]} px solid;' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
end