Class: GCal4Ruby::Service

Inherits:
GData4Ruby::Service
  • 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

  3. Get Calendar List that the authenticated user has owner access level to calendars = service.calendars(:only_owner_access_level => true)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Service

Accepts an optional attributes hash for initialization values



58
59
60
61
62
63
64
# File 'lib/gcal4ruby/service.rb', line 58

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

Instance Attribute Details

#accountObject (readonly)

Convenience attribute contains the currently authenticated account name



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

def 
  @account
end

#auth_tokenObject (readonly)

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



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

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



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

def check_public
  @check_public
end

Instance Method Details

#authenticate(username, password, service = 'cl') ⇒ Object

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

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



68
69
70
# File 'lib/gcal4ruby/service.rb', line 68

def authenticate(username, password, service='cl')
  super(username, password, service)
end

#calendars(options = {}) ⇒ Object

Returns an array of Calendar objects for each calendar associated with the authenticated account. An optional hash of options is the only parameter. The available options are:

:only_owner_access_level

A boolean flag that specifies whether to return only the list of calendars that the authenticated user has owner access to.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gcal4ruby/service.rb', line 81

def calendars(options={})
  if not @auth_token
     raise NotAuthenticated
  end
  feed_url = options[:only_owner_access_level] ? GCal4Ruby::Calendar::OWN_CALENDARS_FEED : GCal4Ruby::Calendar::ALL_CALENDARS_FEED
  ret = send_request(GData4Ruby::Request.new(:get, feed_url, nil, {"max-results" => "10000"}))
  cals = []
  REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
    entry = GData4Ruby::Utils.add_namespaces(entry)
    cal = Calendar.new(self)
    cal.load(entry.to_s)
    cals << cal
  end
  return cals
end

#eventsObject

Returns an array of Event objects for each event in this account



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gcal4ruby/service.rb', line 98

def events
  if not @auth_token
     raise NotAuthenticated
  end
  ret = send_request(GData4Ruby::Request.new(:get, Event.event_feed_uri(@account), nil, {"max-results" => "10000"}))
  events = []
  REXML::Document.new(ret.body).root.elements.each("entry"){}.map do |entry|
    entry = GData4Ruby::Utils.add_namespaces(entry)
    event = Event.new(self)
    event.load(entry.to_s)
    events << event
  end
  return events
end

#reauthenticate(service = 'cl') ⇒ Object

Helper function to reauthenticate to a new Google service without having to re-set credentials.



73
74
75
# File 'lib/gcal4ruby/service.rb', line 73

def reauthenticate(service='cl')
  authenticate(@account, @password, service)
end

#to_iframe(cals, params = {}, colors = {}) ⇒ 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. Accepts any parameter that the google iframe recognizes. Here are the most common:

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.
showTitle:: set to '0' to hide the title
showDate:: set to '0' to hide the current date
showNav:: set to '0 to hide the navigation tools
showPrint:: set to '0' to hide the print icon
showTabs:: set to '0' to hide the tabs
showCalendars:: set to '0' to hide the calendars selection drop down
showTz:: set to '0' 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.
ctz:: The timezone to convert event times to
  1. colors: a hash of calendar ids as key and color values as associated hash values. Example: => ‘#7A367A’



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/gcal4ruby/service.rb', line 132

def to_iframe(cals, params = {}, colors = {})
  params[:height] ||= "600"
  params[:width] ||= "600"
  params[:title] ||= (self. ? self. : '')
  params[:bgcolor] ||= "#FFFFFF"
  params[:border] ||= "0"
  params.each{|key, value| params[key] = CGI::escape(value)}
  output = "#{params.to_a.collect{|a| a.join("=")}.join("&")}&"
  
  if cals.is_a?(Array)
    for c in cals
      output += "src=#{c}&"
      if colors and colors[c]
        output += "color=%23#{colors[c].gsub("#", "")}&"
      end
    end
  elsif cals == :all
    cal_list = calendars()
    for c in cal_list
      output += "src=#{c.id}&"
    end
  elsif cals == :first
    cal_list = calendars()
    output += "src=#{cal_list[0].id}&"
  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