Class: HCl::App

Inherits:
Object
  • Object
show all
Includes:
Commands, Utility
Defined in:
lib/hcl/app.rb

Constant Summary collapse

HCL_DIR =
(ENV['HCL_DIR'] || "#{ENV['HOME']}/.hcl").freeze
SETTINGS_FILE =
"#{HCL_DIR}/settings.yml".freeze
CONFIG_FILE =
"#{HCL_DIR}/config.yml".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Commands

#alias, #aliases, #cancel, #completion, #config, #console, #log, #note, #resume, #set, #show, #start, #stop, #tasks, #unalias, #unset

Methods included from Utility

#as_hours, #current_time, #fail, #get_ident, #get_starting_time, #get_task, #get_task_ids, #time2float

Constructor Details

#initializeApp

Returns a new instance of App.



18
19
20
21
22
23
# File 'lib/hcl/app.rb', line 18

def initialize
  FileUtils.mkdir_p(HCL_DIR)
  read_config
  read_settings
  self
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



16
17
18
# File 'lib/hcl/app.rb', line 16

def http
  @http
end

Class Method Details

.command(*args) ⇒ Object

Run the given command and arguments.



26
27
28
# File 'lib/hcl/app.rb', line 26

def self.command *args
  new.process_args(*args).run
end

Instance Method Details

#command?(command) ⇒ true, false

Return true if the string is a known command, false otherwise.

Parameters:

  • command (#to_s)

    name of command

Returns:

  • (true, false)


34
35
36
# File 'lib/hcl/app.rb', line 34

def command? command
  Commands.method_defined? command
end

#process_args(*args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/hcl/app.rb', line 81

def process_args *args
  @options = Trollop::options(args) do
    stop_on Commands.instance_methods
    version "HCl version #{VERSION}"
    banner <<-EOM
HCl is a command-line client for manipulating Harvest time sheets.

Commands:
# show all available tasks
hcl tasks

# create a task alias
hcl alias <task_alias> <project_id> <task_id>

# list task aliases
hcl aliases

# start a task using an alias
hcl [start] @<task_alias> [+<time>] [<message>]

# add a line to a running timer
hcl note <message>

# stop a running timer
hcl stop [<message>]

# log a task and time without leaving a timer running
hcl log @<task_alias> [+<time>] [<message>]

# resume the last stopped timer or a specific task
hcl resume [@<task_alias>]

# delete the current or last running timer
hcl (cancel | oops | nvm)

# display the daily timesheet
hcl [show [<date>]]

Examples:
hcl alias mytask 1234 4567
hcl @mytask +:15 Doing a thing that I started 15 minutes ago.
hcl note Adding a note to my running task.
hcl stop That's enough for now.
hcl resume
hcl show yesterday
hcl show last tuesday

Options:
EOM
    opt :reauth, "Force refresh of auth details"
  end
  @command = args.shift
  @args = args
  self
end

#runObject

Start the application.



39
40
41
42
43
44
45
46
47
48
49
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
76
77
78
79
# File 'lib/hcl/app.rb', line 39

def run
  request_config if @options[:reauth]
  begin
    if @command
      if command? @command
        result = send @command, *@args
        if not result.nil?
          if result.respond_to? :join
            puts result.join(', ')
          elsif result.respond_to? :to_s
            puts result
          end
        end
      else
        puts start(@command, *@args)
      end
    else
      puts show
    end
  rescue CommandError => e
    $stderr.puts e
    exit 1
  rescue RuntimeError => e
    $stderr.puts "Error: #{e}"
    exit 1
  rescue SocketError => e
    $stderr.puts "Connection failed. (#{e.message})"
    exit 1
  rescue HarvestMiddleware::ThrottleFailure => e
    $stderr.puts "Too many requests, retrying in #{e.retry_after+5} seconds..."
    sleep e.retry_after+5
    run
  rescue HarvestMiddleware::AuthFailure => e
    $stderr.puts "Unable to authenticate: #{e}"
    request_config
    run
  rescue HarvestMiddleware::Failure => e
    $stderr.puts "API failure: #{e}"
    exit 1
  end
end