Class: Googlecalendar::GData
- Inherits:
-
Object
- Object
- Googlecalendar::GData
- Defined in:
- lib/googlecalendar/gdata.rb
Overview
A ruby class to wrap calls to the Google Data API
More informations
Google calendar API: code.google.com/apis/calendar/developers_guide_protocol.html
Instance Attribute Summary collapse
-
#google_url ⇒ Object
Returns the value of attribute google_url.
Class Method Summary collapse
-
.create_conf_file(email, pwd) ⇒ Object
Convinient method to create the conf file use in login_with_conf_file if you don’t want to create it by hand.
Instance Method Summary collapse
-
#add_reminder(event, reminderMinutes, reminderMethod) ⇒ Object
Add a reminder to the event hash * reminderMinutes * reminderMethod [email, alert, sms, none].
-
#delete_event(event_url, if_match = '*') ⇒ Object
method to allow deleting an event from a google calendar.
-
#find_calendar(x) ⇒ Object
get_calendars.
-
#get_calendars ⇒ Object
Retreive user’s calendar urls.
-
#initialize(google = 'www.google.com') ⇒ GData
constructor
A new instance of GData.
-
#login(email, pwd, source = 'googlecalendar.rubyforge.org-googlecalendar-default') ⇒ Object
Log into google data, this method needs to be call once before using other methods of the class * Email The user’s email address.
-
#login_with_conf_file(file = '~/.googlecalendar4ruby/google.yaml') ⇒ Object
Log into google data This method is basically the same as the login with user and password but it tries to find a ~/.googlecalendar4ruby/google.yml Use this if you don’t want to hardcode your user/password in your .rb files.
-
#new_event(event = {}, calendar = nil) ⇒ Object
‘event’ param is a hash containing * :title * :content * :author * :email * :where * :startTime ‘2007-06-06T15:00:00.000Z’ * :endTime ‘2007-06-06T17:00:00.000Z’.
- #post_event(xml, calendar = nil) ⇒ Object
-
#quick_add(text) ⇒ Object
Create a quick add event.
-
#reset_reminders(event) ⇒ Object
Reset reminders.
-
#template(event = {}) ⇒ Object
The atom event template to submit a new event.
Constructor Details
#initialize(google = 'www.google.com') ⇒ GData
Returns a new instance of GData.
15 16 17 18 |
# File 'lib/googlecalendar/gdata.rb', line 15 def initialize(google='www.google.com') @calendars = [] @google_url = google end |
Instance Attribute Details
#google_url ⇒ Object
Returns the value of attribute google_url.
13 14 15 |
# File 'lib/googlecalendar/gdata.rb', line 13 def google_url @google_url end |
Class Method Details
.create_conf_file(email, pwd) ⇒ Object
Convinient method to create the conf file use in login_with_conf_file if you don’t want to create it by hand
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/googlecalendar/gdata.rb', line 22 def self.create_conf_file(email, pwd) p = {'email' => email, 'password' => pwd} path = File.("~/.googlecalendar4ruby/google.yaml") unless File.exists?(path) puts "Creating file in #{path}" Dir.mkdir(File.dirname(path)) end f = File.new(path, File::CREAT|File::TRUNC|File::RDWR) f << p.to_yaml f.close end |
Instance Method Details
#add_reminder(event, reminderMinutes, reminderMethod) ⇒ Object
Add a reminder to the event hash
-
reminderMinutes
-
reminderMethod [email, alert, sms, none]
83 84 85 86 |
# File 'lib/googlecalendar/gdata.rb', line 83 def add_reminder(event, reminderMinutes, reminderMethod) event[:reminders] = event[:reminders].to_s + "<gd:reminder minutes='#{reminderMinutes}' method='#{reminderMethod}' />\n" end |
#delete_event(event_url, if_match = '*') ⇒ Object
method to allow deleting an event from a google calendar
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/googlecalendar/gdata.rb', line 201 def delete_event(event_url, if_match = '*') # create required deletion headers delete_headers = @headers.merge({'If-Match' => if_match}) # now delete it http = Net::HTTP.new(@google_url, 80) response, data = http.delete(event_url, delete_headers) case response when Net::HTTPSuccess, Net::HTTPRedirection redirect_response, redirect_data = http.delete(response['location'], delete_headers) case redirect_response when Net::HTTPSuccess, Net::HTTPRedirection return redirect_data else return redirect_response.error! end else return response.error! end return data end |
#find_calendar(x) ⇒ Object
get_calendars
168 169 170 |
# File 'lib/googlecalendar/gdata.rb', line 168 def find_calendar(x) @calendars.find {|c| c.title.match x} end |
#get_calendars ⇒ Object
Retreive user’s calendar urls.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/googlecalendar/gdata.rb', line 145 def get_calendars http = Net::HTTP.new(@google_url, 80) response, data = http.get("http://#{@google_url}/calendar/feeds/" + @user_id, @headers) case response when Net::HTTPSuccess, Net::HTTPRedirection redirect_response, redirect_data = http.get(response['location'], @headers) case response when Net::HTTPSuccess, Net::HTTPRedirection doc = REXML::Document.new redirect_data doc.elements.each('//entry')do |e| title = e.elements['title'].text url = e.elements['link'].attributes['href'] @calendars << GCalendar.new(title, url.sub!("http://#{@google_url}",'')) end return redirect_response else response.error! end else response.error! end end |
#login(email, pwd, source = 'googlecalendar.rubyforge.org-googlecalendar-default') ⇒ Object
Log into google data, this method needs to be call once before using other methods of the class
-
Email The user’s email address.
-
Passwd The user’s password.
-
source Identifies your client application. Should take the form companyName-applicationName-versionID
Warning Replace the default value with something like: companyName-applicationName-versionID
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/googlecalendar/gdata.rb', line 57 def login(email, pwd, source='googlecalendar.rubyforge.org-googlecalendar-default') # service The string cl, which is the service name for Google Calendar. @user_id = email response = Net::HTTPS.post_form(URI.parse("https://#{@google_url}/accounts/ClientLogin"), { 'Email' => email, 'Passwd' => pwd, 'source' => source, 'accountType' => 'HOSTED_OR_GOOGLE', 'service' => 'cl'}) response.error! unless response.kind_of? Net::HTTPSuccess @token = response.body.split(/=/).last @headers = { 'Authorization' => "GoogleLogin auth=#{@token}", 'Content-Type' => 'application/atom+xml' } return @token end |
#login_with_conf_file(file = '~/.googlecalendar4ruby/google.yaml') ⇒ Object
Log into google data This method is basically the same as the login with user and password but it tries to find a ~/.googlecalendar4ruby/google.yml Use this if you don’t want to hardcode your user/password in your .rb files
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/googlecalendar/gdata.rb', line 38 def login_with_conf_file(file='~/.googlecalendar4ruby/google.yaml') path = File.(file) if(File.exists?(path)) File.open(path) { |f| @yaml = YAML::load(f) } else GData::create_conf_file('[email protected]', 'REPLACE_WITH_YOUR_PASSWORD') throw "Created a default file in: #{path}, you need to edit it !!" end email = @yaml['email'] pwd = @yaml['password'] login(email, pwd) end |
#new_event(event = {}, calendar = nil) ⇒ Object
‘event’ param is a hash containing
-
:title
-
:content
-
:author
-
:email
-
:where
-
:startTime ‘2007-06-06T15:00:00.000Z’
-
:endTime ‘2007-06-06T17:00:00.000Z’
Use add_reminder(event, reminderMinutes, reminderMethod) method to add reminders
113 114 115 116 |
# File 'lib/googlecalendar/gdata.rb', line 113 def new_event(event={},calendar = nil) new_event = template(event) post_event(new_event, calendar) end |
#post_event(xml, calendar = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/googlecalendar/gdata.rb', line 118 def post_event(xml, calendar = nil) #Get calendar url calendar_url = if calendar get_calendars find_calendar(calendar).url else # We will use user'default calendar in this case '/calendar/feeds/default/private/full' end http = Net::HTTP.new(@google_url, 80) response, data = http.post(calendar_url, xml, @headers) case response when Net::HTTPSuccess, Net::HTTPRedirection redirect_response, redirect_data = http.post(response['location'], xml, @headers) case response when Net::HTTPSuccess, Net::HTTPRedirection return redirect_response else response.error! end else response.error! end end |
#quick_add(text) ⇒ Object
Create a quick add event
text = 'Tennis with John April 11 3pm-3:30pm'
code.google.com/apis/calendar/developers_guide_protocol.html#CreatingQuickAdd
93 94 95 96 97 98 99 100 101 |
# File 'lib/googlecalendar/gdata.rb', line 93 def quick_add(text) content = <<EOF <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gCal='http://schemas.google.com/gCal/2005'> <content type="html">#{text}</content> <gCal:quickadd value="true"/> </entry> EOF post_event(content) end |
#reset_reminders(event) ⇒ Object
Reset reminders
76 77 78 |
# File 'lib/googlecalendar/gdata.rb', line 76 def reset_reminders(event) event[:reminders] = "" end |
#template(event = {}) ⇒ Object
The atom event template to submit a new event
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/googlecalendar/gdata.rb', line 173 def template(event={}) content = <<EOF <?xml version="1.0"?> <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2005'> <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'></category> <title type='text'>#{event[:title]}</title> <content type='text'>#{event[:content]}</content> <author> <name>#{event[:]}</name> <email>#{event[:email]}</email> </author> <gd:transparency value='http://schemas.google.com/g/2005#event.opaque'> </gd:transparency> <gd:eventStatus value='http://schemas.google.com/g/2005#event.confirmed'> </gd:eventStatus> <gd:where valueString='#{event[:where]}'></gd:where> <gd:when startTime='#{event[:startTime]}' endTime='#{event[:endTime]}'> #{event[:reminders]} </gd:when> </entry> EOF end |