Class: GetToWork::Command::Start

Inherits:
GetToWork::Command show all
Defined in:
lib/get_to_work/command/start.rb

Instance Method Summary collapse

Methods inherited from GetToWork::Command

#config_file, #harvest_service, #last_story, #last_timer, run

Methods included from Menu

#menu_ask

Constructor Details

#initialize(opts = {}) ⇒ Start

Returns a new instance of Start.



6
7
8
9
# File 'lib/get_to_work/command/start.rb', line 6

def initialize(opts = {})
  super(opts)
  @pt_id = parse_pt_id(opts[:pt_id])
end

Instance Method Details

#parse_pt_id(pt_id) ⇒ Object



51
52
53
54
55
56
# File 'lib/get_to_work/command/start.rb', line 51

def parse_pt_id(pt_id)
  return nil if pt_id.nil?

  pt_id.delete("#")
  pt_id.match(/\d+$/)[0]
end

#prompt_to_use_last_storyObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/get_to_work/command/start.rb', line 31

def prompt_to_use_last_story
  last_story = config_file["last_story"]

  if last_story
    shell.say "\nWould you like to start a timer for your last story?", :green
    shell.say "  ##{last_story[:id.to_s]} ", [:bold, :cyan]
    shell.say "#{last_story[:name.to_s]}", :magenta
    answer = shell.yes? "\n[y/N]", :green

    if answer
      @pt_id = last_story["id"]
    else
      exit(0)
    end
  else
    shell.say "Couldn't find your last started timer. Please specify a story id."
    exit(0)
  end
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/get_to_work/command/start.rb', line 11

def run

  if @pt_id.nil?
    prompt_to_use_last_story
  end

  pt = GetToWork::Service::PivotalTracker.new(config_file.data)
  story = pt.story(@pt_id)

  entry = {
    notes: "##{story.id}\n\n#{story.name}\n#{story.url}",
    project_id: harvest_service.project_id,
    task_id: harvest_service.task_id,
  }

  timer = harvest_service.start_timer(entry)
  save_last_timer(timer)
  save_last_story(story)
end

#save_last_story(story) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/get_to_work/command/start.rb', line 58

def save_last_story(story)
  config_file[:last_story.to_s] = {
    "id" => story.id,
    "name" => story.name
  }

  config_file.save
end

#save_last_timer(timer) ⇒ Object



67
68
69
70
# File 'lib/get_to_work/command/start.rb', line 67

def save_last_timer(timer)
  config_file[:last_timer.to_s] = timer.id
  config_file.save
end