Search code examples
rubygoogle-calendar-api

Google Calendar Ruby API insert_event errors "each_with_index" undefined


I'm copying and pasting the code from the Ruby Quickstart and combining it with the create-events code sample from here.

The only things I changed were:

  • the SCOPE from Google::Apis::CalendarV3::AUTH_CALENDAR_READONLY to Google::Apis::CalendarV3::AUTH_CALENDAR_READ
  • from client to service in the call to .insert_event('primary', event)

Despite this, I'm getting the error:

.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/representable-3.0.4/lib/representable/pipeline.rb:38:in `call': undefined method `each_with_index' for nil:NilClass (NoMethodError)

This is befuddling, none more so than knowing it was working earlier. Is there something obviously wrong in the example that I'm not catching? Or did representable or the api-client cop out all of a sudden?

Are folks able to reproduce the problem? Here's my code along with my Gemfile.lock

require "google/apis/calendar_v3"
require "googleauth"
require "googleauth/stores/file_token_store"
require "date"
require "fileutils"

OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze
CREDENTIALS_PATH = "credentials.json".freeze
# The file token.yaml stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
TOKEN_PATH = "token.yaml".freeze
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR

##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
  client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
  token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
  authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
  user_id = "default"
  credentials = authorizer.get_credentials user_id
  if credentials.nil?
    url = authorizer.get_authorization_url base_url: OOB_URI
    puts "Open the following URL in the browser and enter the " \
         "resulting code after authorization:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

# Initialize the API
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

event = Google::Apis::CalendarV3::Event.new(
  summary: 'Google I/O 2015',
  location: '800 Howard St., San Francisco, CA 94103',
  description: 'A chance to hear more about Google\'s developer products.',
  start: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2015-05-28T09:00:00-07:00',
    time_zone: 'America/Los_Angeles'
  ),
  end: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2015-05-28T17:00:00-07:00',
    time_zone: 'America/Los_Angeles'
  ),
  recurrence: [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  attendees: [
    Google::Apis::CalendarV3::EventAttendee.new(
      email: '[email protected]'
    ),
    Google::Apis::CalendarV3::EventAttendee.new(
      email: '[email protected]'
    )
  ],
  reminders: Google::Apis::CalendarV3::Event::Reminders.new(
    use_default: false,
    overrides: [
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'email',
        minutes: 24 * 60
      ),
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'popup',
        minutes: 10
      )
    ]
  )
)

result = service.insert_event('primary', event)
puts "Event created: #{result.html_link}"

Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    activesupport (6.1.3)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
      zeitwerk (~> 2.3)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    concurrent-ruby (1.1.8)
    declarative (0.0.20)
    declarative-option (0.1.1)
    faraday (1.3.0)
      faraday-net_http (~> 1.0)
      multipart-post (>= 1.2, < 3)
      ruby2_keywords
    faraday-net_http (1.0.1)
    gems (1.2.0)
    google-api-client (0.53.0)
      google-apis-core (~> 0.1)
      google-apis-generator (~> 0.1)
    google-apis-core (0.3.0)
      addressable (~> 2.5, >= 2.5.1)
      googleauth (~> 0.14)
      httpclient (>= 2.8.1, < 3.0)
      mini_mime (~> 1.0)
      representable (~> 3.0)
      retriable (>= 2.0, < 4.0)
      rexml
      signet (~> 0.14)
      webrick
    google-apis-discovery_v1 (0.2.0)
      google-apis-core (~> 0.1)
    google-apis-generator (0.2.0)
      activesupport (>= 5.0)
      gems (~> 1.2)
      google-apis-core (~> 0.1)
      google-apis-discovery_v1 (~> 0.0)
      thor (>= 0.20, < 2.a)
    googleauth (0.16.0)
      faraday (>= 0.17.3, < 2.0)
      jwt (>= 1.4, < 3.0)
      memoist (~> 0.16)
      multi_json (~> 1.11)
      os (>= 0.9, < 2.0)
      signet (~> 0.14)
    httpclient (2.8.3)
    i18n (1.8.9)
      concurrent-ruby (~> 1.0)
    jwt (2.2.2)
    memoist (0.16.2)
    mini_mime (1.0.2)
    minitest (5.14.4)
    multi_json (1.15.0)
    multipart-post (2.1.1)
    os (1.1.1)
    public_suffix (4.0.6)
    representable (3.0.4)
      declarative (< 0.1.0)
      declarative-option (< 0.2.0)
      uber (< 0.2.0)
    retriable (3.1.2)
    rexml (3.2.4)
    ruby2_keywords (0.0.4)
    signet (0.15.0)
      addressable (~> 2.3)
      faraday (>= 0.17.3, < 2.0)
      jwt (>= 1.5, < 3.0)
      multi_json (~> 1.10)
    thor (1.1.0)
    tzinfo (2.0.4)
      concurrent-ruby (~> 1.0)
    uber (0.1.0)
    webrick (1.7.0)
    zeitwerk (2.4.2)

PLATFORMS
  ruby

DEPENDENCIES
  google-api-client

BUNDLED WITH
   2.1.4

Solution

  • so we have the same issue with google storage api after they updated it to 1.31. So we downgraded it to 1.29.1. Please, try to downgrade your version too, it will problably work!

    And we needed to clear our heroku cache and reinstall all the gems again.

    https://help.heroku.com/18PI5RSY/how-do-i-clear-the-build-cache

    Hope it helps