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

Constant Summary collapse

CALENDAR_LIST_FEED =
'http://www.google.com/calendar/feeds/default/allcalendars/full'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Service

Accepts an optional attributes hash for initialization values



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

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



45
46
47
# File 'lib/gcal4ruby/service.rb', line 45

def 
  @account
end

#auth_tokenObject (readonly)

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



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

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



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

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.



70
71
72
# File 'lib/gcal4ruby/service.rb', line 70

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

#calendarsObject

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



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

def calendars
  if not @auth_token
     raise NotAuthenticated
  end
  ret = send_request(GData4Ruby::Request.new(:get, CALENDAR_LIST_FEED, 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

#default_event_feedObject



64
65
66
# File 'lib/gcal4ruby/service.rb', line 64

def default_event_feed
  return "http://www.google.com/calendar/feeds/#{@account}/private/full"
end

#eventsObject

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



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

def events
  if not @auth_token
     raise NotAuthenticated
  end
  ret = send_request(GData4Ruby::Request.new(:get, default_event_feed, 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.



75
76
77
# File 'lib/gcal4ruby/service.rb', line 75

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’



131
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
# File 'lib/gcal4ruby/service.rb', line 131

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