class ConnpassToCalendar::Api::GoogleApis::Calendar

Constants

OOB_URI
SCOPE

Public Class Methods

new(application_name, credentials_path, token_path, user_id) click to toggle source
# File lib/connpass_to_calendar/api/google_apis/calendar.rb, line 12
def initialize(application_name, credentials_path, token_path, user_id)
  @application_name = application_name
  @credentials_path = credentials_path
  @token_path = token_path
  @user_id = user_id
end

Public Instance Methods

create_event(summary, location, description, start_date_time, end_date_time) click to toggle source
# File lib/connpass_to_calendar/api/google_apis/calendar.rb, line 41
def create_event(summary, location, description, start_date_time, end_date_time)
  client = Google::Apis::CalendarV3::CalendarService.new
  client.client_options.application_name = @application_name
  client.authorization = authorize

  event = Google::Apis::CalendarV3::Event.new({
    summary: summary,
    location: location,
    description: description,
    start: {
      date_time: start_date_time,
      time_zone: "Japan",
    },
    end: {
      date_time: end_date_time,
      time_zone: "Japan",
    },
  })
  result = client.insert_event("primary", event)
  puts "Event created: #{result.html_link}"
end

Private Instance Methods

authorize() click to toggle source

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

# File lib/connpass_to_calendar/api/google_apis/calendar.rb, line 25
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)
  credentials = authorizer.get_credentials(@user_id)
  if credentials.nil?
    url = authorizer.get_authorization_url(base_url: OOB_URI)
    puts "Open #{url} in your browser and enter the resulting code:"
    code = STDIN.gets.chomp
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: @user_id, code: code, base_url: OOB_URI,
    )
  end
  credentials
end