Class: TogglCLI

Inherits:
Object
  • Object
show all
Defined in:
lib/toggl_cli.rb

Instance Method Summary collapse

Constructor Details

#initializeTogglCLI

Returns a new instance of TogglCLI.



7
8
9
10
11
12
13
# File 'lib/toggl_cli.rb', line 7

def initialize
  @config       = YAML.load_file(File.join(Dir.home, '.toggl_cli.yml'))
  @toggl_api    = TogglV8::API.new(@config['api_key'])
  @user         = @toggl_api.me(all=true)
  @workspaces   = @toggl_api.my_workspaces(@user)
  @workspace_id = @workspaces.first['id']
end

Instance Method Details

#start(project, description) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/toggl_cli.rb', line 15

def start(project, description)
  pid = ""
  if @config['projects'][project]
    pid = @config['projects'][project]
  end
  time_entry  = @toggl_api.start_time_entry({
    'pid' => pid,
    'description' => "#{description}",
    'wid' => @workspace_id,
    'start' => @toggl_api.iso8601((Time.now).to_datetime),
    'created_with' => "toggl_cli"
  })
end

#stopObject



29
30
31
32
33
34
# File 'lib/toggl_cli.rb', line 29

def stop
  current = @toggl_api.get_current_time_entry
  if current
    result = @toggl_api.stop_time_entry(current['id'])
  end
end

#todayObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/toggl_cli.rb', line 36

def today
  dates = {
    start_date: Time.parse(Date.today.to_s).to_s,
    end_date: Time.now.to_s
  }
  total = 0
  result = @toggl_api.get_time_entries(dates)
  result.each do |r|
    total = total + r['duration']
  end
  (total / 60.0).round(2)
end