Class: Gigawatt::Commands::Start

Inherits:
Object
  • Object
show all
Defined in:
lib/gigawatt/commands/start.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, options) ⇒ Start

Returns a new instance of Start.



32
33
34
35
36
37
38
# File 'lib/gigawatt/commands/start.rb', line 32

def initialize(settings, options)
  @settings = settings
  @options = options

  @access_key = OAuth.token(@settings.access_key)
  @cache = Cache.new(settings, @access_key)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/gigawatt/commands/start.rb', line 4

def options
  @options
end

Class Method Details

.run!(settings) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gigawatt/commands/start.rb', line 6

def self.run!(settings)
  options = Trollop::options do
    banner <<-EOS
88 Miles Command line application - http://88miles.net

Punch into the project.

Usage
  88miles start [options]

options:
    EOS


    opt :activity, "Select an activity", :type => :flag
  end

  instance = self.new(settings, options)
  begin
    return instance.punch_in
  rescue OAuth2::Error => e
    say "Access to your 88 Miles may have been revoked. Please run <%= color('88miles setup', BOLD) %> again."
    return INVALID_OAUTH_TOKEN_EXIT_CODE
  end
end

Instance Method Details

#punch_inObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gigawatt/commands/start.rb', line 54

def punch_in
  project = Gigawatt::ProjectFile.new.project

  if project
    activity = select_activity(project)
    opts = {}
    opts = { :body => { :activity_uuid => activity["uuid"] } } if activity
    response = JSON.parse(@access_key.post("/api/1/projects/#{project["uuid"]}/punch_in.json", opts).body)
    current = response["response"]
    ProjectFile.write(current)

    company = @cache.companies(true)[project["company_uuid"]]
    say("Punched in to #{company["name"]}: #{project["name"]}")
    return OK_EXIT_CODE
  else
    say("No project found. Did you remember to run <%= color('88miles init [directory]', BOLD) %>?")
    return NO_PROJECT_EXIT_CODE
  end
end

#select_activity(project) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gigawatt/commands/start.rb', line 40

def select_activity(project)
  return nil unless options[:activity]
  return nil unless project["activities"]

  selected = nil
  choose do |menu|
    menu.prompt = "Pick an Activity"
    project["activities"].each do |activity|
      menu.choice("#{activity["name"]}") { selected = activity }
    end
  end
  selected
end