Module: Mytime

Extended by:
Mytime, Client
Included in:
Mytime
Defined in:
lib/mytime.rb,
lib/mytime/setup.rb,
lib/mytime/client.rb,
lib/mytime/config.rb,
lib/mytime/version.rb,
lib/mytime/timesheet.rb

Defined Under Namespace

Modules: Client, Config

Constant Summary collapse

USER_FILE =
File.expand_path('~/.mytime')
VERSION =
"0.0.3"

Instance Method Summary collapse

Methods included from Client

project, submit, tasks, validate

Instance Method Details

#commit(hours, message = "") ⇒ Object

Send a custom time entry to API with string message

Option:

hours: Required - number of hours for this time entry
message: Optional - custom message to send with time entry


18
19
20
21
22
# File 'lib/mytime/timesheet.rb', line 18

def commit(hours, message = "")
  return finish_setup unless setup? && init?
  puts "Submitting: #{message}"
  puts Client.submit(hours, message)
end

#execute(*args) ⇒ Object

Parses command line arguments and does what needs to be done.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mytime.rb', line 21

def execute(*args)
  @options = parse_options(*args)
  command = args.shift || 'list'

  case command.to_sym
  when :setup
    puts "We have all the time in the world."
    setup
  when :init
    init
  when :status, :list, :log
    puts status
  when :commit, :add, :a
    commit(args.first, args.last)
  when :push, :submit, :p 
    puts "Submitting Timesheet..."
    push(args.first)
  when :project, :detail, :info
    puts "Project Details:"
    puts Config.details(Dir.pwd).to_yaml
  when :debug
    puts init?
  else
    puts @options
  end
end

#finish_setupObject



58
59
60
61
# File 'lib/mytime.rb', line 58

def finish_setup
  return puts "Please run `mytime setup` connect your account." unless setup?
  return puts "Please run `'mytime init` to setup this project directory" unless init?
end

#initObject

Setup .mytime file to include project specific data



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mytime/setup.rb', line 20

def init
  puts "Choose Project:"
  projects = Client.project["project"]
  projects.each do |project|
    puts "#{project['project_id']}: #{project['name']}"
  end
  project_id = STDIN.gets.chomp

  puts "Choose Task"
  tasks = Client.tasks(project_id)['tasks']['task']
  tasks.each do |task|
    puts "#{task['task_id']}: #{task['name']}"
  end
  task_id = STDIN.gets.chomp

  project_details = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc)}
  project_details[project_id]["project_path"] = Dir.pwd
  project_details[project_id]["project_id"] = project_id
  project_details[project_id]["task_id"] = task_id
  Config.add(project_details)
end

#init?Boolean

Check if mytime is initialized for this project

Returns:

  • (Boolean)


54
55
56
# File 'lib/mytime.rb', line 54

def init?
  Config.details(Dir.pwd).has_key?("project_id")
end

#parse_options(*args) ⇒ Object

Set configuration variables to values passed in the command line options



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mytime/setup.rb', line 44

def parse_options(*args)
  options = OptionParser.new do |opts|
    opts.banner = "\nUsage: mytime [options] [command]"
    opts.separator "mytime uses git to log your time\n\n"
    opts.separator "Commands:"
    opts.separator "  status    Prints formatted commit log from today"
    opts.separator "  setup     Sets up mytime authorization information"
    opts.separator "  init      Sets up mytime for this project"
    opts.separator "  commit    Creates a custom timesheet entry"
    opts.separator "  push      Saves timesheet entry with git commit log"
    opts.separator ""
    opts.separator "Options:"
    opts.on('-h', '--help', 'Display this screen') { puts opts; exit }
    opts.on('-v', '--version', 'Display the current version') do
      puts Mytime::VERSION
      exit
    end
  end
  options.parse!(args)
  options
end

#push(hours) ⇒ Object

Send git commit log as timesheet entry to client

Options

hours: Required parameter for number of hours on time entry


29
30
31
32
33
34
35
36
# File 'lib/mytime/timesheet.rb', line 29

def push(hours)
  return finish_setup unless setup? && init?
  if hours.nil?
    puts "How many hours did you spend on these commits?"
    hours = STDIN.gets.chomp
  end
  puts Client.submit(hours, status)
end

#setupObject

Create .mytime file in the profile directory

and insert the user account and Freshbooks token


7
8
9
10
11
12
13
14
15
16
# File 'lib/mytime/setup.rb', line 7

def setup
  puts "What is your Freshbooks account name?" 
   = STDIN.gets.chomp
  puts "What is your Freshbooks token?" 
  token = STDIN.gets.chomp
   = Hash.new
  ["account"] = "#{}.freshbooks.com"
  ["token"] = token
  Config.save()
end

#setup?Boolean

Check if mytime is setup

Returns:

  • (Boolean)


49
50
51
# File 'lib/mytime.rb', line 49

def setup?
  Config.details.has_key?("account")
end

#statusObject

Return log of git commits from today



6
7
8
9
10
# File 'lib/mytime/timesheet.rb', line 6

def status
  return finish_setup unless setup? && init?
  user = `git config --get user.name`
  `git log --oneline --author='#{user}' --since='6am'`
end