Class: GCal4Ruby::Service

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



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

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

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#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



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

def check_public
  @check_public
end

Instance Method Details

#authenticate(options = {}) ⇒ Object

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

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



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

def authenticate(options = {})
  default_options = { :service => 'cl' }
  options = default_options.merge(options)
  super(options)
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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gcal4ruby/service.rb', line 88

def calendars(options={})
  if not (@auth_token || @access_token)
     raise GData4Ruby::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, '', {"max-results" => "10000"}))
  cals = []
  REXML::Document.new(ret).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



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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/gcal4ruby/service.rb', line 105

def events
  if not (@auth_token || @access_token)
     raise NotAuthenticated
  end
  ret = send_request(GData4Ruby::Request.new(:get, Event.event_feed_uri(@account), '', {"max-results" => "10000"}))
  events = []
  REXML::Document.new(ret).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(options = {}) ⇒ Object

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



80
81
82
# File 'lib/gcal4ruby/service.rb', line 80

def reauthenticate(options = {})
  authenticate(options)
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’



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/gcal4ruby/service.rb', line 139

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