Class: Colleagues::Calendar::Command::ConfigParser

Inherits:
Object
  • Object
show all
Defined in:
lib/colleagues/calendar/command/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigParser



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/colleagues/calendar/command/config.rb', line 12

def initialize
  begin
    config_path = File.expand_path("~/.colleagues-calendar-command.yaml")
    unless File.exist?(config_path)
      message ="Configuration file:<\#{config_path}> not found.\nCreate \#{config_path}.\n\n  Example:\n    ---\n    calendar_ids:\n      John Smith: [email protected]\n\n"
      raise ConfigurationError, message
    end
    @conf = YAML.load_file(config_path)
    @calendar_ids = @conf["calendar_ids"]
    @schedule_type = @conf["schedule_type"]
    @refresh_token = @conf["refresh_token"]

    secret_path = File.expand_path("~/.colleagues-calendar-command.secret.json")
    unless File.exist?(secret_path)
      message ="Client secret file:<\#{secret_path}> not found.\n\n  1. Create project on https://console.developers.google.com/ for Google Calendar API\n  2. Download credential(OAuth client ID) file.\n\n"
      raise ConfigurationError, message
    end
    open(secret_path) do |file|
      @secret = JSON.load(file)
      @client_id = @secret["installed"]["client_id"]
      @client_secret = @secret["installed"]["client_secret"]
    end
  rescue => e
    puts e.message
    exit 1
  end
end

Instance Attribute Details

#calendar_idsObject (readonly)

Returns the value of attribute calendar_ids.



8
9
10
# File 'lib/colleagues/calendar/command/config.rb', line 8

def calendar_ids
  @calendar_ids
end

#client_idObject (readonly)

Returns the value of attribute client_id.



6
7
8
# File 'lib/colleagues/calendar/command/config.rb', line 6

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



7
8
9
# File 'lib/colleagues/calendar/command/config.rb', line 7

def client_secret
  @client_secret
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/colleagues/calendar/command/config.rb', line 10

def refresh_token
  @refresh_token
end

#schedule_typeObject (readonly)

Returns the value of attribute schedule_type.



9
10
11
# File 'lib/colleagues/calendar/command/config.rb', line 9

def schedule_type
  @schedule_type
end

Instance Method Details

#saveObject



55
56
57
58
59
60
61
# File 'lib/colleagues/calendar/command/config.rb', line 55

def save
  @conf["refresh_token"] = @refresh_token
  config_path = File.expand_path("~/.colleagues-calendar-command.yaml")
  open(config_path, "w") do |file|
    file.puts(YAML.dump(@conf))
  end
end