class TogglCLI

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

client(name) click to toggle source
# File lib/toggl_cli.rb, line 71
def client(name)
  client = ""
  result = @toggl_api.my_clients
  result.each do |r|
    if r['name'].downcase == name
      client = r
    end
  end
  client
end
clients() click to toggle source
# File lib/toggl_cli.rb, line 62
def clients
  clients = []
  result = @toggl_api.my_clients
  result.each do |r|
    clients << "#{r['id']} #{r['name']}"
  end
  clients
end
project(client_id, name) click to toggle source
# File lib/toggl_cli.rb, line 93
def project(client_id, name)
  project = ""
  result = @toggl_api.my_projects
  result.each do |r|
    if r['cid'] == client_id && r['name'].downcase == name
      project = r
    end
  end
  project
end
projects(client_id = nil) click to toggle source
# File lib/toggl_cli.rb, line 82
def projects(client_id = nil)
  projects = []
  result = @toggl_api.my_projects
  result.each do |r|
    if r['cid']
      projects << r
    end
  end
  projects
end
start(client, project, description) click to toggle source
# File lib/toggl_cli.rb, line 15
def start(client, project, description)
  cid = client(client)['id']
  pid = project(cid, project)['id']

  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
stop() click to toggle source
# File lib/toggl_cli.rb, line 28
def stop
  current = @toggl_api.get_current_time_entry
  if current
    result = @toggl_api.stop_time_entry(current['id'])
  end
end
today(client = nil, project = nil) click to toggle source
# File lib/toggl_cli.rb, line 35
def today(client = nil, project = nil)
  pids = []
  if client
    cid = client(client)['id']
    presult = projects
    presult.each do |r|
      if r['cid'] == cid
        pids << r['id']
      end
    end
  end
  entries = []
  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|
    if (client && (pids.include? r['pid']))
      entries << "#{r['duration']} - #{r['description']}"
      total = total + r['duration']
    end
  end
  {entries: entries, total: (total / 60.0).round(2)}
end