Class: GCal4Ruby::Service

Inherits:
Object
  • 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 =
'www.google.com/calendar/feeds/default/owncalendars/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
63
64
65
66
67
68
69
70
71
72
# File 'lib/gcal4ruby/service.rb', line 56

def initialize(attributes = {})
  # If the user has specified the type of GData4Ruby class they want, instantiate it
  if(attributes.has_key?(:GData4RubyService))
    @gdata_service = GData4Ruby.const_get(attributes[:GData4RubyService]).new(attributes)
  end
  # Otherwise use the default service
  @gdata_service ||= GData4Ruby::Service.new(attributes)
  attributes.each do |key, value|
    if self.respond_to?("#{key}=")
      self.send("#{key}=", value)
    end
  end    
  @check_public ||= true
  @account ||= "default"
  @debug ||= false
  log("Check Public: #{check_public}")
end

Instance Attribute Details

#accountObject

Convenience attribute contains the currently authenticated account name



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

def 
  @account
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

#gdata_serviceObject

The type of GData4Ruby service we want to use



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

def gdata_service
  @gdata_service
end

Instance Method Details

#authenticate(options = {}) ⇒ Object

The authenticate method passes an for the service to use to access Google’s servers

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



93
94
95
96
97
98
# File 'lib/gcal4ruby/service.rb', line 93

def authenticate(options = {})
  if not options.has_key?(:service)
    options[:service] = 'cl'
  end
  @gdata_service.authenticate(options)
end

#calendars(options = {}) ⇒ Object

Returns an array of Calendar objects for each calendar associated with the authenticated account. If you want to only load some attributes, you can pass in an array of string attributes via options, for example [“@gd:*”, “id”, “title”]



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/gcal4ruby/service.rb', line 121

def calendars(options = {})
  if(options[:fields])
    params = "?fields=entry(#{options[:fields].join(",")})"
  end
  params ||= ""
  ret = send_request(GData4Ruby::Request.new(:get, create_url(@@calendar_list_feed + params), 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, {:debug => debug})
    log(entry.inspect)
    cal.load(entry.to_s)
    cals << cal
  end
  return cals
end

#create_url(path) ⇒ Object

Builds a URL



153
154
155
# File 'lib/gcal4ruby/service.rb', line 153

def create_url(path)
  return @gdata_service.create_url(path)
end

#debugObject



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

def debug
  return @debug
end

#debug=(value) ⇒ Object



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

def debug=(value)
  @debug=value
  @gdata_service.debug = value
end

#default_event_feedObject



87
88
89
# File 'lib/gcal4ruby/service.rb', line 87

def default_event_feed
  return create_url("www.google.com/calendar/feeds/#{@account}/private/full")
end

#eventsObject

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



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gcal4ruby/service.rb', line 140

def events
 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, {:debug => debug})
    event.load(entry.to_s)
    events << event
  end
  return events
end

#log(string) ⇒ Object



83
84
85
# File 'lib/gcal4ruby/service.rb', line 83

def log(string)
  puts string if debug
end

#reauthenticate(options = {}) ⇒ Object

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



101
102
103
104
105
106
# File 'lib/gcal4ruby/service.rb', line 101

def reauthenticate(options = {})
  if not options.has_key?(:service)
    options[:service] = 'cl'
  end
  @gdata_service.reauthenticate(options)
end

#send_request(request) ⇒ Object

Passes a request along from a GData4Ruby GDataObject to a GData4Ruby Base (Service) to be invoked



109
110
111
112
113
114
# File 'lib/gcal4ruby/service.rb', line 109

def send_request(request)
  if not @gdata_service.authenticated?
     raise GData4Ruby::NotAuthenticated
  end
  @gdata_service.send_request(request)
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’



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/gcal4ruby/service.rb', line 176

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='#{create_url("www.google.com/calendar/embed?"+output)}' style='#{params[:border]} px solid;' width='#{params[:width]}' height='#{params[:height]}' frameborder='#{params[:border]}' scrolling='no'></iframe>"
end