Class: GCal4Ruby::Service
- Inherits:
-
GData4Ruby::Service
- Object
- GData4Ruby::Service
- GCal4Ruby::Service
- 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
-
Authenticate service = Service.new service.authenticate(“[email protected]”, “password”)
-
Get Calendar List calendars = service.calendars
Constant Summary collapse
- CALENDAR_LIST_FEED =
'https://www.google.com/calendar/feeds/default/allcalendars/full'
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Convenience attribute contains the currently authenticated account name.
-
#auth_token ⇒ Object
readonly
The token returned by the Google servers, used to authorize all subsequent messages.
-
#check_public ⇒ Object
Determines whether GCal4Ruby ensures a calendar is public.
Instance Method Summary collapse
-
#authenticate(username, password, service = 'cl') ⇒ Object
The authenticate method passes the username and password to google servers.
-
#calendars ⇒ Object
Returns an array of Calendar objects for each calendar associated with the authenticated account.
- #default_event_feed ⇒ Object
-
#events ⇒ Object
Returns an array of Event objects for each event in this account.
-
#initialize(attributes = {}) ⇒ Service
constructor
Accepts an optional attributes hash for initialization values.
-
#reauthenticate(service = 'cl') ⇒ Object
Helper function to reauthenticate to a new Google service without having to re-set credentials.
-
#to_iframe(cals, params = {}, colors = {}) ⇒ Object
Helper function to return a formatted iframe embedded google calendar.
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
#account ⇒ Object (readonly)
Convenience attribute contains the currently authenticated account name
45 46 47 |
# File 'lib/gcal4ruby/service.rb', line 45 def account @account end |
#auth_token ⇒ Object (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_public ⇒ Object
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 |
#calendars ⇒ Object
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_feed ⇒ Object
64 65 66 |
# File 'lib/gcal4ruby/service.rb', line 64 def default_event_feed return "https://www.google.com/calendar/feeds/#{@account}/private/full" end |
#events ⇒ Object
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:
-
cals: either an array of calendar ids, or :all for all calendars, or :first for the first (usally default) calendar
-
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
-
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.account ? self.account : '') 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 |