goal 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 98340b4a1a1dcf76f5106cb7bcf2acb180628280
4
+ data.tar.gz: dd95639036b8e938b92e0521e26430023f7bfdbb
5
+ SHA512:
6
+ metadata.gz: 076510ffa8c199ddba9eb3f2d043dd5b920a92492d9d8dfa170e92757a9df260dec3e7495962404efedc49cde36df18d846136aa69b13b9aeccf2442f280a98e
7
+ data.tar.gz: 26655283aea051418dce99573167876ffd14bd9489296495d3e61563f0d64c888f0b293316c6bb82762e0f47eb9fa000f99124b25f94895760ddfac1cfa1b1aa
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ example.rb
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ source 'http://rubygems.org'
3
+
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ goal (0.0.2)
5
+ ruby-freshbooks (~> 0.4.1)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ builder (3.2.2)
11
+ colorize (0.6.0)
12
+ diff-lcs (1.2.4)
13
+ httparty (0.12.0)
14
+ json (~> 1.8)
15
+ multi_xml (>= 0.5.2)
16
+ json (1.8.1)
17
+ multi_xml (0.5.5)
18
+ rake (10.1.0)
19
+ rspec (2.14.1)
20
+ rspec-core (~> 2.14.0)
21
+ rspec-expectations (~> 2.14.0)
22
+ rspec-mocks (~> 2.14.0)
23
+ rspec-core (2.14.5)
24
+ rspec-expectations (2.14.2)
25
+ diff-lcs (>= 1.1.3, < 2.0)
26
+ rspec-mocks (2.14.3)
27
+ ruby-freshbooks (0.4.1)
28
+ builder (>= 2.1.2)
29
+ httparty (>= 0.5.0)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ colorize (~> 0.6.0)
36
+ goal!
37
+ rake
38
+ rspec (~> 2.14.1)
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ Goal
2
+ ====
3
+
4
+ ## Examples
5
+
6
+ 1) When do you want to know if you'll reach the goal for the current month.
7
+ Assuming that my goal is 160 hours/month and we are at the 14 business day from
8
+ the month that have 23 business days.
9
+
10
+ ```ruby
11
+ g = Goal.new
12
+
13
+ g.calculate_goal(160) # => 97.39130434782608
14
+ ```
15
+
16
+ So I need to have 97.39 hours to be within my goal.
17
+
18
+
19
+ ## Using the bin
20
+
21
+ ```bash
22
+ $ goal -u freshbooks_user -t freshbooks_token
23
+
24
+ | Goals | Estimate | Avg. to Goal |
25
+ | 160 | 97 | 4.93 |
26
+ | 200 | 121 | 9.37 |
27
+ | 250 | 152 | 14.93 |
28
+ | 300 | 182 | 20.48 |
29
+ |_________________________________|
30
+ | Current: 115.66h
31
+ | Average: 8.26h
32
+ | Left days: 9
33
+ ```
34
+
35
+ ### Available options
36
+
37
+ ```
38
+ Usage: goal [options]
39
+ -u, --username=USERNAME Freshbooks username
40
+ -t, --token=TOKEN Freshbooks token
41
+ -g, --goals=[GOALS] Your goal list. Ex 160:200:300
42
+ ```
43
+
44
+ Options `-u` and `-t` are required and specify the user and token to access the
45
+ Freshbooks hours.
46
+
47
+ The `-g` is optional, by default it has the following value `160:200:250:300`
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ task :default => :spec
data/bin/goal ADDED
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'goal'
4
+ require 'colorize'
5
+ require 'optparse'
6
+
7
+ class MyGoals
8
+ def initialize(options = {})
9
+ @base = Goal::Base.new
10
+ @calculator = Goal::HourCalculator.new
11
+ @goal_list = options.fetch(:goal_list, [160, 200, 250, 300])
12
+ @username = options[:username]
13
+ @token = options[:token]
14
+ end
15
+
16
+ def print
17
+ puts
18
+ print_header
19
+ print_data
20
+ print_footer
21
+ puts
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :base, :calculator, :goal_list, :username, :token
27
+
28
+ def print_header
29
+ puts '| Goals | Estimate | Avg. to Goal |'
30
+ end
31
+
32
+ def print_data
33
+ goals.each do |goal|
34
+ avg = calculator.rate_to_goal(goal[:goal], current_time).round(2)
35
+
36
+ puts '| ' + goal[:goal].to_s.ljust(5) + ' | ' + goal[:expected].to_i.to_s.ljust(8) + ' | ' + avg.to_s.ljust(12).colorize(average_color(avg)) + ' |'
37
+ end
38
+ end
39
+
40
+ def print_footer
41
+ puts '|_________________________________|'
42
+ puts "| Current: " + "#{current_time}h".colorize(current_color)
43
+ puts "| Average: " + "#{rate}h".colorize(current_color)
44
+ puts "| Left days: #{calculator.days_left}"
45
+ end
46
+
47
+ def current_time
48
+ @current_time ||= base.worked_time(username, token).round(2)
49
+ end
50
+
51
+ def rate
52
+ @rate ||= calculator.hour_rate(current_time).round(2)
53
+ end
54
+
55
+ def goals
56
+ base.calculate_goals goal_list
57
+ end
58
+
59
+ def current_color
60
+ if rate < 8
61
+ :red
62
+ elsif rate >= 8 && rate < 9
63
+ :yellow
64
+ else
65
+ :green
66
+ end
67
+ end
68
+
69
+ def average_color(avg)
70
+ if avg < 8
71
+ :green
72
+ elsif avg >= 8 && avg < 9
73
+ :yellow
74
+ else
75
+ :red
76
+ end
77
+ end
78
+ end
79
+
80
+ options = {}
81
+
82
+ OptionParser.new do |opts|
83
+ opts.banner = "Usage: goal [options]"
84
+
85
+ opts.on("-u", "--username=USERNAME", "Freshbooks username") do |u|
86
+ options[:username] = u
87
+ end
88
+
89
+ opts.on("-t", "--token=TOKEN", "Freshbooks token") do |t|
90
+ options[:token] = t
91
+ end
92
+
93
+ opts.on("-g", "--goals=[GOALS]", "Your goal list. Ex 160:200:300") do |g|
94
+ options[:goal_list] = g.split(':').map(&:to_i)
95
+ end
96
+ end.parse!
97
+
98
+ MyGoals.new(options).print
data/goal.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "goal/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "goal"
6
+ s.version = Goal::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.licenses = ["MIT"]
9
+ s.summary = "Keep track of your hour goals"
10
+ s.email = "mcomogo@gmail.com"
11
+ s.homepage = "http://github.com/comogo/goal"
12
+ s.description = s.summary
13
+ s.authors = ['Mateus Lorandi dos Santos']
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- test/*`.split("\n")
17
+ s.require_paths = ["lib"]
18
+ s.executables << 'goal'
19
+
20
+ s.add_dependency 'ruby-freshbooks', '~> 0.4.1'
21
+
22
+ s.add_development_dependency 'colorize', '~> 0.6.0'
23
+ s.add_development_dependency 'rspec', '~> 2.14.1'
24
+ s.add_development_dependency 'rake'
25
+ end
data/lib/goal/base.rb ADDED
@@ -0,0 +1,32 @@
1
+ module Goal
2
+ class Base
3
+ def initialize
4
+ @calculator = HourCalculator.new
5
+ end
6
+
7
+ def worked_time(username, token)
8
+ FreshbooksCalculator.new(username, token).hours
9
+ end
10
+
11
+ def calculate_goal(goal)
12
+ calculator.estimated_for(goal)
13
+ end
14
+
15
+ def calculate_goals(goals)
16
+ calculated_goals = []
17
+
18
+ goals.each do |goal|
19
+ calculated_goals.push({
20
+ goal: goal,
21
+ expected: calculate_goal(goal)
22
+ })
23
+ end
24
+
25
+ calculated_goals
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :calculator
31
+ end
32
+ end
@@ -0,0 +1,48 @@
1
+ require 'ruby-freshbooks'
2
+
3
+ module Goal
4
+ class FreshbooksCalculator
5
+ def initialize(freshbooks_username, token)
6
+ @username = freshbooks_username
7
+ @token = token
8
+ end
9
+
10
+ def hours
11
+ entries_for_month.inject(0) { |sum, entry| sum + entry['hours'].to_f }
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :username, :token
17
+
18
+ def entries_for_month
19
+ entries = connection.time_entry.list(date_from: date_from, date_to: date_to, per_page: per_page)
20
+
21
+ if entries.has_key?('time_entries')
22
+ entries['time_entries']['time_entry']
23
+ else
24
+ []
25
+ end
26
+ end
27
+
28
+ def date_from
29
+ Date.new Date.today.year, Date.today.month, 1
30
+ end
31
+
32
+ def date_to
33
+ Date.new Date.today.year, Date.today.month, -1
34
+ end
35
+
36
+ def per_page
37
+ 1000
38
+ end
39
+
40
+ def client_url
41
+ "#{username}.freshbooks.com"
42
+ end
43
+
44
+ def connection
45
+ @connection ||= FreshBooks::Client.new(client_url, token)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,43 @@
1
+ module Goal
2
+ class HourCalculator
3
+ def total_days_until(date = Date.today)
4
+ total = 0
5
+ month = Date.today.month
6
+ year = Date.today.year
7
+ date.day.downto(1) do |day|
8
+ current = Date.new(year, month, day)
9
+
10
+ total += 1 unless current.saturday? || current.sunday?
11
+ end
12
+
13
+ total
14
+ end
15
+
16
+ def estimated_for(goal)
17
+ current_days = total_days_until
18
+ total_days = total_days_until(end_of_month)
19
+
20
+ hours_per_day = goal.to_f / total_days.to_f
21
+
22
+ current_days * hours_per_day
23
+ end
24
+
25
+ def hour_rate(hours_until_now)
26
+ hours_until_now.to_f / total_days_until.to_f
27
+ end
28
+
29
+ def rate_to_goal(goal, hours_until_now)
30
+ (goal - hours_until_now) / days_left
31
+ end
32
+
33
+ def days_left
34
+ total_days_until(end_of_month) - total_days_until
35
+ end
36
+
37
+ private
38
+
39
+ def end_of_month
40
+ Date.new(Date.today.year, Date.today.month, -1)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Goal
2
+ VERSION = '0.0.2'.freeze
3
+ end
data/lib/goal.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'goal/hour_calculator'
2
+ require 'goal/freshbooks_calculator'
3
+ require 'goal/base'
4
+
5
+ module Goal
6
+ end
data/spec/base_spec.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Goal::Base do
4
+ describe '#worked_time' do
5
+ it 'should get the worked_time from freshbooks' do
6
+ freshbooks_instance = double(:freshbooks_instance)
7
+
8
+ Goal::FreshbooksCalculator.should_receive(:new).with('username', 'token').and_return(freshbooks_instance)
9
+ freshbooks_instance.should_receive(:hours).and_return(30)
10
+
11
+ expect(subject.worked_time('username', 'token')).to eq 30
12
+ end
13
+ end
14
+
15
+ describe '#calculate_goal' do
16
+ it 'should calculate goal using the hour calculator' do
17
+ Goal::HourCalculator.any_instance.should_receive(:estimated_for).with(200).and_return 10
18
+
19
+ expect(subject.calculate_goal(200)).to eq 10
20
+ end
21
+ end
22
+
23
+ describe '#calculate_goals' do
24
+ it 'should calculate goals using the hour calculator' do
25
+ Goal::HourCalculator.any_instance.should_receive(:estimated_for).with(160).and_return 10
26
+ Goal::HourCalculator.any_instance.should_receive(:estimated_for).with(200).and_return 20
27
+
28
+ expect(subject.calculate_goals([160, 200])).to eq([
29
+ {
30
+ goal: 160,
31
+ expected: 10
32
+ },
33
+ {
34
+ goal: 200,
35
+ expected: 20
36
+ }
37
+ ])
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe Goal::FreshbooksCalculator do
4
+ let(:time_entry) { double(:time_entry) }
5
+ let(:connection) { double(:connection, time_entry: time_entry) }
6
+ let(:end_of_month) { Date.new Date.today.year, Date.today.month, -1 }
7
+ let(:begin_of_month) { Date.new Date.today.year, Date.today.month, 1 }
8
+
9
+ let :entries do
10
+ {
11
+ 'time_entries' => {
12
+ 'time_entry' => [
13
+ {
14
+ 'hours' => '10.5'
15
+ },
16
+ {
17
+ 'hours' => '5.4'
18
+ }
19
+ ]
20
+ }
21
+ }
22
+ end
23
+
24
+ let :error_entries do
25
+ {
26
+ message: 'error',
27
+ code: '12345'
28
+ }
29
+ end
30
+
31
+ subject do
32
+ described_class.new 'username', 'token'
33
+ end
34
+
35
+ describe '#hours' do
36
+ context 'when has entries' do
37
+ it 'should be the sum of entry hours' do
38
+ FreshBooks::Client.should_receive(:new).with('username.freshbooks.com', 'token').and_return(connection)
39
+ time_entry.
40
+ should_receive(:list).
41
+ with(date_from: begin_of_month, date_to: end_of_month, per_page: 1000).
42
+ and_return(entries)
43
+
44
+ expect(subject.hours).to eq 15.9
45
+ end
46
+ end
47
+
48
+ context 'when has no entries' do
49
+ it 'should be 0' do
50
+ FreshBooks::Client.should_receive(:new).with('username.freshbooks.com', 'token').and_return(connection)
51
+ time_entry.
52
+ should_receive(:list).
53
+ with(date_from: begin_of_month, date_to: end_of_month, per_page: 1000).
54
+ and_return(error_entries)
55
+
56
+ expect(subject.hours).to eq 0
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Goal::HourCalculator do
4
+ describe '#total_days_until' do
5
+ it 'should calculate the business days from the beginning of the month until the specified day' do
6
+ expect(subject.total_days_until Date.new(2013, 10, 17)).to eq 13 # Thursday
7
+ expect(subject.total_days_until Date.new(2013, 10, 18)).to eq 14 # Friday
8
+ expect(subject.total_days_until Date.new(2013, 10, 19)).to eq 14 # Saturday
9
+ expect(subject.total_days_until Date.new(2013, 10, 20)).to eq 14 # Sunday
10
+ expect(subject.total_days_until Date.new(2013, 10, 21)).to eq 15 # Monday
11
+ end
12
+
13
+ it 'it default option should be the current day' do
14
+ Date.stub(today: Date.new(2013, 10, 18))
15
+
16
+ expect(subject.total_days_until).to eq 14
17
+ end
18
+ end
19
+
20
+ describe '#estimated_for' do
21
+ it 'should calculate the calculate the current total time for goal until today' do
22
+ Date.stub(today: Date.new(2013, 10, 18))
23
+
24
+ expect(subject.estimated_for(160)).to eq 97.39130434782608
25
+ expect(subject.estimated_for(200)).to eq 121.7391304347826
26
+ end
27
+ end
28
+
29
+ describe '#hour_rate' do
30
+ it 'should calculate the hour rate from a given time and the past days' do
31
+ Date.stub(today: Date.new(2013, 10, 18))
32
+
33
+ expect(subject.hour_rate(95)).to eq 6.785714285714286
34
+ end
35
+ end
36
+
37
+ describe '#rate_to_goal' do
38
+ it 'should calculate the hour per day needed to the goal from the left days of the month' do
39
+ Date.stub(today: Date.new(2013, 10, 18))
40
+
41
+ expect(subject.rate_to_goal(200, 115.66)).to eq 9.371111111111112
42
+ end
43
+ end
44
+
45
+ describe '#days_left' do
46
+ it 'should return the days left from de month since today' do
47
+ Date.stub(today: Date.new(2013, 10, 18))
48
+
49
+ expect(subject.days_left).to eq 9
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+ require "goal"
4
+ require "rspec"
5
+ require "rspec/autorun"
6
+
7
+ require "rubygems"
8
+
9
+ Dir["spec/support/**/*.rb"].each { |f| require f }
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mateus Lorandi dos Santos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-freshbooks
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Keep track of your hour goals
70
+ email: mcomogo@gmail.com
71
+ executables:
72
+ - goal
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - Gemfile.lock
79
+ - MIT-LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - bin/goal
83
+ - goal.gemspec
84
+ - lib/goal.rb
85
+ - lib/goal/base.rb
86
+ - lib/goal/freshbooks_calculator.rb
87
+ - lib/goal/hour_calculator.rb
88
+ - lib/goal/version.rb
89
+ - spec/base_spec.rb
90
+ - spec/freshbooks_calculator_spec.rb
91
+ - spec/hour_calculator_spec.rb
92
+ - spec/spec_helper.rb
93
+ homepage: http://github.com/comogo/goal
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.1.5
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Keep track of your hour goals
117
+ test_files: []
118
+ has_rdoc: