0

Im working on a sinatra based application, in which I get events from a google calendar and display all events as a list. However I have encountered an unusual error when I tried to get the start and end dates of All day events.

Since all day events have an object of type Date, and timed events have an object of type dateTime , these two objects won't get displayed, the error I get is:

no such method named dateTime

It works fine when there is only timed events (dateTime object) events but not when there is an all day event (date object).

Any help would be great.

Code:

require 'rubygems'
require 'google/api_client'
require 'date'
 # modified from 1m

  # Update these to match your own apps credentials
  service_account_email = "" # Email of service account
  key_file = "" # File containing your private key
  key_secret = 'notasecret' # Password to unlock private key

  # Get the Google API client
  client = Google::APIClient.new(:application_name => 'GCalendar', 
    :application_version => '1.0.0')

  # Load your credentials for the service account
  key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
  client.authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :audience => 'https://accounts.google.com/o/oauth2/token',
    :scope => 'https://www.googleapis.com/auth/calendar',
    :issuer => service_account_email,
    :signing_key => key)

  motd = Array.new

  summary = ""
  description = ""
  tickerEvent = " "
  # Start the scheduler

  # Request a token for our service account
  client.authorization.fetch_access_token!

  # Get the calendar API
  calendar = client.discovered_api('calendar','v3')
  today = DateTime.now().to_s
  # Execute the query
  result = client.execute(:api_method => calendar.events.list,
  :parameters => {'calendarId' => 'idNo', 
    'timeMin' => today,
    'singleEvents' => true,
    'orderBy' => 'startTime'})

  events = result.data.items
  events.each do |e|
    if(DateTime.now() >= e.start.dateTime)
        summary = e.summary
      description = e.description
      tickerEvent = summary.to_s + " - "  +description.to_s
      motd.push(tickerEvent)
    elsif(DateTime.now() >= e.end.dateTime)
      motd.delete(e)
    end
  end
  motd.clear
end

Is there a way to check whether the event is a Date type or a DateTime type?

In the google api the start dateTime and end dateTime looks like (This is a timed event):

"start": {
    "dateTime": "2013-12-03T12:30:00+13:00",
    "timeZone": "Pacific/Auckland"
   },
   "end": {
    "dateTime": "2013-12-03T13:00:00+13:00",
    "timeZone": "Pacific/Auckland"
   },

whereas an all day event looks like:

"start": {
    "date": "2014-01-17"
   },
   "end": {
    "date": "2014-01-18"
   },

they are of different types, this is what is causing the error.

Cheers

falsetru
  • 357,413
  • 63
  • 732
  • 636
Joshi
  • 15
  • 5

1 Answers1

0

The method is event.start.date_time not event.start.dateTime.

For events that do not have a time associated with them, event.start.date_time will return nil. You can then call event.start.date, which should return just the date.

sffc
  • 6,186
  • 3
  • 44
  • 68