Class: Colleagues::Calendar::Command::CommandLine

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

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Returns a new instance of CommandLine.



18
19
20
# File 'lib/colleagues/calendar/command.rb', line 18

def initialize
  @config = ConfigParser.new
end

Instance Method Details

#input_schedule_date(schedule_type) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/colleagues/calendar/command.rb', line 50

def input_schedule_date(schedule_type)
  message =<<SCHEDULE
#{schedule_type}ですね。登録予定の日時は?
  入力例:
  11/29 (全休などの場合)
  10:00 - 11:00 (当日の予定の場合)
  11/29 10:00 - 11/29 11:00 (他の日の予定の場合)

SCHEDULE
  input = @cli.ask(message)
  begin_time = Time.now
  end_time = Time.now
  begin
    if input.index("-")
      begin_time = Time.parse(input.split("-")[0])
      end_time = Time.parse(input.split("-")[1])
    else
      begin_time = Time.parse(input)
      end_time = begin_time + (60 * 60 * 24)
    end
  rescue
    p "FAIL"
    return
  end
  [begin_time, end_time]
end

#register_all_day_event(title, begin_time, end_time) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/colleagues/calendar/command.rb', line 99

def register_all_day_event(title, begin_time, end_time)
  begin
    @calendar = Calendar.new(@config.client_id,
                             @config.client_secret,
                             @calendar_id,
                             @config.refresh_token)
    @calendar.create_all_day(title,
                             begin_time,
                             end_time)
    unless @config.refresh_token
      @config.refresh_token = @calendar.refresh_token
      @config.save
    end
    @cli.say("登録を完了しました。")
  rescue => e
    puts e.message
    exit 1
  end
end

#register_event(schedule_type, begin_time, end_time) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/colleagues/calendar/command.rb', line 87

def register_event(schedule_type, begin_time, end_time)
  @config.schedule_type.each do |key, item|
    if item["label"] == schedule_type
      if item["all_day"]
        register_all_day_event("#{item["text"]}", begin_time, end_time)
      else
        register_oneshot_event("#{item["text"]}", begin_time, end_time)
      end
    end
  end
end

#register_oneshot_event(title, begin_time, end_time) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/colleagues/calendar/command.rb', line 119

def register_oneshot_event(title, begin_time, end_time)
  begin
    @calendar = Calendar.new(@config.client_id,
                             @config.client_secret,
                             @calendar_id,
                             @config.refresh_token)
    @calendar.create_event(title,
                           begin_time,
                           end_time)
    unless @config.refresh_token
      @config.refresh_token = @calendar.refresh_token
      @config.save
    end
    @cli.say("登録を完了しました。")
  rescue => e
    puts e.message
    exit 1
  end
end

#run(arguments) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/colleagues/calendar/command.rb', line 77

def run(arguments)
  @cli = HighLine.new
  calendar_selection = select_calendar
  @calendar_id = calendar_selection[:calendar_id]
  schedule_type = select_schedule_type
  schedule_date = input_schedule_date(schedule_type)
  register_event(schedule_type, schedule_date[0], schedule_date[1]) 
  true
end

#select_calendarObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/colleagues/calendar/command.rb', line 22

def select_calendar
  selection = {}
  @cli.choose do |menu|
    menu.prompt = "誰のカレンダーに登録しますか?"
    @config.calendar_ids.keys.each do |key|
      menu.choice(key) do |person|
        calendar_id = @config.calendar_ids[person]
        selection[:person] = person
        selection[:calendar_id] = calendar_id
      end
    end
  end
  selection
end

#select_schedule_typeObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/colleagues/calendar/command.rb', line 37

def select_schedule_type
  schedule_type = ""
  @cli.choose do |menu|
    menu.prompt = "登録する予定の種類は?"
    @config.schedule_type.each do |type, item|
      menu.choice(item["label"]) do |selected|
        schedule_type = selected
      end
    end
  end
  schedule_type
end