don 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/don +141 -0
  2. metadata +47 -0
data/bin/don ADDED
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'json'
5
+ require 'typhoeus'
6
+ require 'highline'
7
+ require 'optparse'
8
+ require 'ostruct'
9
+ require 'yaml'
10
+
11
+ @term = HighLine.new
12
+ @term.page_at = :auto
13
+
14
+ class DonConfig
15
+ def conf
16
+ @config ||= YAML.load_file(path)
17
+ end
18
+
19
+ def reload
20
+ begin
21
+ @config = YAML.load_file(path)
22
+ rescue
23
+ create
24
+ end
25
+ end
26
+
27
+ def create
28
+ File.open(path, 'w') do |f|
29
+ term = HighLine.new
30
+ @config = {
31
+ "api_token" => term.ask("Your api token: ")
32
+ }
33
+ f.write(YAML.dump(@config))
34
+ end
35
+ @config
36
+ end
37
+
38
+ def save
39
+ File.open(path, 'w') do |f|
40
+ f.write(YAML.dump(@config))
41
+ end
42
+ @config
43
+ end
44
+
45
+ def path
46
+ File.expand_path("~/.c5timesheet")
47
+ end
48
+ end
49
+
50
+ @config = DonConfig.new
51
+ API_TOKEN = @config.reload["api_token"]
52
+
53
+
54
+ API_ENDPOINT = ENV["C5TIMESHEET_ENDPOINT"] || 'https://timesheet.carbonfive.com'
55
+
56
+ def projects
57
+ resp = Typhoeus::Request.get("#{API_ENDPOINT}/api/projects",
58
+ :params => { :api_token => API_TOKEN })
59
+ begin
60
+ JSON(resp.body).map { |proj| OpenStruct.new(:id => proj["id"], :name => proj["name"]) }
61
+ rescue
62
+ puts "Make you have a valid api token"
63
+ exit
64
+ end
65
+ end
66
+
67
+ def record_task(options)
68
+ resp = Typhoeus::Request.post("#{API_ENDPOINT}/api/tasks",
69
+ :params => {
70
+ :api_token => API_TOKEN,
71
+ :task => {
72
+ :time => options.time,
73
+ :description => options.description,
74
+ :completed_on => Date.today,
75
+ :project_id => options.project_id,
76
+ :onsite => options.onsite
77
+ }
78
+ }
79
+ )
80
+ if resp.code == 201
81
+ puts "Hours recorded. Don is super psyched."
82
+ else
83
+ puts "Something went wrong"
84
+ end
85
+ end
86
+
87
+ def validate_options(options)
88
+ unless options.description
89
+ options.description = @term.ask("What did you do: ")
90
+ end
91
+
92
+ unless options.project_id
93
+ project_id = @term.choose do |menu|
94
+ menu.prompt = "Choose a project: "
95
+ projects.each do |proj|
96
+ menu.choice(proj.name) { proj.id }
97
+ end
98
+ end
99
+ options.project_id = project_id
100
+ @config.reload["current_project_id"] = project_id
101
+ @config.save
102
+ end
103
+
104
+ options
105
+ end
106
+
107
+ class TaskOptions
108
+ def self.parse(args, config)
109
+ options = OpenStruct.new
110
+ options.onsite = true
111
+ options.time = 8.0
112
+ options.project_id = nil #ENV['PROJECT_ID']
113
+
114
+ opts = OptionParser.new do |opts|
115
+ opts.on("-m", "--message MESSAGE",
116
+ "the task description") { |message|
117
+ options.description = message
118
+ }
119
+
120
+ opts.on("-t", "--time [TIME]",
121
+ "the time spend on the task; default to 8.0") { |time|
122
+ options.time = time
123
+ }
124
+
125
+ opts.on("-p", "--project [PROJECT]",
126
+ "the project id") { |project_id|
127
+ options.project_id = project_id
128
+ }
129
+
130
+ opts.on("-c", "--current", "use the last used project id") { |current|
131
+ options.project_id = config.conf["current_project_id"]
132
+ }
133
+ end
134
+ opts.parse!(args)
135
+ options
136
+ end
137
+ end
138
+
139
+ options = TaskOptions.parse(ARGV, @config)
140
+ options = validate_options(options)
141
+ record_task(options)
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: don
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rit Li
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple command line interface to the Carbon Five Timesheet
15
+ email:
16
+ - rit@carbonfive.com
17
+ executables:
18
+ - don
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - bin/don
23
+ homepage: http://code.google.com/p/c5don
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Command line interface to the Carbon Five Timesheet
47
+ test_files: []