Class: CalendarAssistant::CLI::Authorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/calendar_assistant/cli/authorizer.rb

Defined Under Namespace

Classes: NoCredentials, UnauthorizedError

Constant Summary collapse

OOB_URI =
"urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME =
"Flavorjones Calendar Assistant".freeze
CREDENTIALS_PATH =
File.join (ENV["CA_HOME"] || ENV["HOME"]), ".calendar-assistant.client"
SCOPE =
Google::Apis::CalendarV3::AUTH_CALENDAR

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile_name, config_token_store) ⇒ Authorizer

Returns a new instance of Authorizer.



33
34
35
36
# File 'lib/calendar_assistant/cli/authorizer.rb', line 33

def initialize(profile_name, config_token_store)
  @profile_name = profile_name
  @config_token_store = config_token_store
end

Instance Attribute Details

#config_token_storeObject (readonly)

Returns the value of attribute config_token_store.



31
32
33
# File 'lib/calendar_assistant/cli/authorizer.rb', line 31

def config_token_store
  @config_token_store
end

#profile_nameObject (readonly)

Returns the value of attribute profile_name.



31
32
33
# File 'lib/calendar_assistant/cli/authorizer.rb', line 31

def profile_name
  @profile_name
end

Instance Method Details

#authorizeObject



38
39
40
# File 'lib/calendar_assistant/cli/authorizer.rb', line 38

def authorize
  credentials || prompt_user_for_authorization
end

#serviceObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/calendar_assistant/cli/authorizer.rb', line 42

def service
  if credentials.nil?
    raise UnauthorizedError, "Not authorized. Please run `calendar-assistant authorize #{profile_name}`"
  end

  Google::Apis::CalendarV3::CalendarService.new.tap do |service|
    service.client_options.application_name = APPLICATION_NAME
    service.authorization = credentials
  end
end