class TogglIntegrator::GoogleCalendar

class GoogleCalendar

Public Class Methods

sync_time_entries() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 15
def sync_time_entries
  time_entries.each do |time_entory|
    res = sync(time_entory)
    time_entory.update status: TogglIntegrator::TimeEntory::STATUS[:DONE]
    Logging.info("Synced event '#{res.summary}' (#{res.id})")
  end
rescue StandardError => e
  Logging.error(e.message)
end

Private Class Methods

authorized_service() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 70
def authorized_service
  service = Google::Apis::CalendarV3::CalendarService.new
  service.client_options.application_name =
    config['google']['application_name']
  service.authorization = credentials
  service
end
authorizer() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 58
def authorizer
  return @authorizer if @authorizer.present?

  client_id   = Google::Auth::ClientId.from_file ENV['CLIENT_SECRET_FILE']
  token_store = Google::Auth::Stores::FileTokenStore.new file:
    FileUtil.join('google-calendar.yaml')
  @authorizer = Google::Auth::UserAuthorizer.new client_id,
                                                 Google::Apis::CalendarV3::\
                                                 AUTH_CALENDAR,
                                                 token_store
end
config() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 41
def config
  @config ||= YAML.load_file File.join(__dir__, '../../config.yml')
end
credentials() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 78
def credentials
  return @credentials if @credentials.present?

  FileUtil.new_file_if_not_exists('google-calendar.yaml')

  @credentials = authorizer.get_credentials user_id
  @credentials = credentials_from_code if @credentials.nil?
  @credentials
rescue StandardError => e
  Logging.error(e.message)
end
credentials_from_code() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 94
def credentials_from_code
  url = authorizer.get_authorization_url base_url:
    config['google']['oob_uri']

  info_message = 'Open the following URL in the browser and enter the ' \
                "resulting code after authorization\n\n URL: #{url}\n\n" \
                'Got resulting code? Please input your resulting code'
  Logging.info(info_message)
  puts info_message
  code = gets
  authorizer.get_and_store_credentials_from_code user_id:
    user_id, code: code, base_url: config['google']['oob_uri']
end
generate_event(time_entry) click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 45
def generate_event(time_entry)
  {
    summary: "#{time_entry[:project_name]} : #{time_entry[:description]}",
    start: {
      date_time: DateTime.parse(time_entry[:start].localtime.to_s)
    },
    end: {
      date_time: DateTime.parse(time_entry[:stop].localtime.to_s)
    },
    color_id: ENV['COLOR_ID']
  }
end
service() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 37
def service
  @service ||= authorized_service
end
sync(time_entory) click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 32
def sync(time_entory)
  service.insert_event 'primary', generate_event(time_entory),
                       send_notifications: true
end
time_entries() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 27
def time_entries
  @time_entries ||= TimeEntory.where status:
    TogglIntegrator::TimeEntory::STATUS[:NOT_YET]
end
user_id() click to toggle source
# File lib/toggl_integrator/google_calendar.rb, line 90
def user_id
  @user_id ||= 'default'
end