toggl-worktime 0.2.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d7d312022057b262ea569daa0ff5a2d4fd81f515c6c0fbe32d4dbbac1d5f8bd
4
- data.tar.gz: 2412b9d69c4a482400bde188a1a7523b215c63891f1d7159a7834b257cb673b8
3
+ metadata.gz: 07e6ba242a332844e6949c51f0b680590d7167aac06910ff6612e732ee29d459
4
+ data.tar.gz: 6ddb96ae57367ca506240f0c5a25af8df0a7e20adaa7d2b3f091eecae7d44d88
5
5
  SHA512:
6
- metadata.gz: a643c84a3603192a66519cc95cf40523ff98bb626702608f958b9494b2ec8fd6c5551554c18cfac95ac71099a6936553361263e5471cecd7e0c1c2c4616cd79b
7
- data.tar.gz: 6f1c575654a741742f3e4efcf438fa0ac89bed10b68b685e2aed44b2e4fe5bbbb292b67ff17dc10fa828acb1c2e133133ac3803cb07812b9f8ccde7d7b444b3c
6
+ metadata.gz: cc4498ba37d5807d14f46dfd6f6b04bc2e383046fb7e40f65ed0ef2380a07192f0b39bcadb04cd91898f2a6d7d5de3654eafe8b6c1eb650f79174ca30d87c6bd
7
+ data.tar.gz: 16432e596c87bd4e8f0bc714eaff67d057dabee4030cc9262ff5bd4e1ed711cbfb34933901f82c7f5690e453ec2ef3ccab1a3548766c45ec11d81f95e6d65301
data/.rubocop.yml CHANGED
@@ -8,3 +8,7 @@ Style/Documentation:
8
8
 
9
9
  Metrics/MethodLength:
10
10
  Max: 15
11
+
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - 'spec/**/*'
data/.travis.yml CHANGED
@@ -2,4 +2,6 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.3.1
5
- before_install: gem install bundler -v 1.15.0
5
+ - 2.7.3
6
+ - 3.0.1
7
+ before_install: gem install bundler -v 2.2.20
data/CHANGELOG.md ADDED
@@ -0,0 +1,97 @@
1
+ # Changelog
2
+
3
+ ## [0.4.1](https://github.com/limitusus/toggl-worktime/tree/0.4.1) (2021-06-22)
4
+
5
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.4.0...0.4.1)
6
+
7
+ **Merged pull requests:**
8
+
9
+ - Use patched version of togglv8, for the new API endpoint [\#12](https://github.com/limitusus/toggl-worktime/pull/12) ([limitusus](https://github.com/limitusus))
10
+
11
+ ## [v0.4.0](https://github.com/limitusus/toggl-worktime/tree/v0.4.0) (2021-02-14)
12
+
13
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.3.2...v0.4.0)
14
+
15
+ **Merged pull requests:**
16
+
17
+ - Use newer rake [\#9](https://github.com/limitusus/toggl-worktime/pull/9) ([limitusus](https://github.com/limitusus))
18
+ - Implement calendar mode [\#8](https://github.com/limitusus/toggl-worktime/pull/8) ([limitusus](https://github.com/limitusus))
19
+
20
+ ## [v0.3.2](https://github.com/limitusus/toggl-worktime/tree/v0.3.2) (2018-09-26)
21
+
22
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.3.1...v0.3.2)
23
+
24
+ **Merged pull requests:**
25
+
26
+ - Deal with empty time slot [\#7](https://github.com/limitusus/toggl-worktime/pull/7) ([limitusus](https://github.com/limitusus))
27
+
28
+ ## [v0.3.1](https://github.com/limitusus/toggl-worktime/tree/v0.3.1) (2018-09-26)
29
+
30
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.3.0...v0.3.1)
31
+
32
+ **Merged pull requests:**
33
+
34
+ - Bugfix/interval [\#6](https://github.com/limitusus/toggl-worktime/pull/6) ([limitusus](https://github.com/limitusus))
35
+
36
+ ## [v0.3.0](https://github.com/limitusus/toggl-worktime/tree/v0.3.0) (2018-08-01)
37
+
38
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.2.0...v0.3.0)
39
+
40
+ **Closed issues:**
41
+
42
+ - ignore vacation tag [\#3](https://github.com/limitusus/toggl-worktime/issues/3)
43
+
44
+ **Merged pull requests:**
45
+
46
+ - Update README [\#11](https://github.com/limitusus/toggl-worktime/pull/11) ([limitusus](https://github.com/limitusus))
47
+ - Use newer rake [\#10](https://github.com/limitusus/toggl-worktime/pull/10) ([limitusus](https://github.com/limitusus))
48
+ - Config YAML [\#5](https://github.com/limitusus/toggl-worktime/pull/5) ([limitusus](https://github.com/limitusus))
49
+ - Specify year [\#4](https://github.com/limitusus/toggl-worktime/pull/4) ([limitusus](https://github.com/limitusus))
50
+
51
+ ## [v0.2.0](https://github.com/limitusus/toggl-worktime/tree/v0.2.0) (2018-04-02)
52
+
53
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.8...v0.2.0)
54
+
55
+ **Merged pull requests:**
56
+
57
+ - Remove const [\#2](https://github.com/limitusus/toggl-worktime/pull/2) ([limitusus](https://github.com/limitusus))
58
+
59
+ ## [v0.1.8](https://github.com/limitusus/toggl-worktime/tree/v0.1.8) (2018-04-01)
60
+
61
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.7...v0.1.8)
62
+
63
+ **Merged pull requests:**
64
+
65
+ - Export work\_time from driver [\#1](https://github.com/limitusus/toggl-worktime/pull/1) ([limitusus](https://github.com/limitusus))
66
+
67
+ ## [v0.1.7](https://github.com/limitusus/toggl-worktime/tree/v0.1.7) (2017-09-19)
68
+
69
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.6...v0.1.7)
70
+
71
+ ## [v0.1.6](https://github.com/limitusus/toggl-worktime/tree/v0.1.6) (2017-09-19)
72
+
73
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.5...v0.1.6)
74
+
75
+ ## [v0.1.5](https://github.com/limitusus/toggl-worktime/tree/v0.1.5) (2017-08-15)
76
+
77
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.4...v0.1.5)
78
+
79
+ ## [v0.1.4](https://github.com/limitusus/toggl-worktime/tree/v0.1.4) (2017-07-07)
80
+
81
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.3...v0.1.4)
82
+
83
+ ## [v0.1.3](https://github.com/limitusus/toggl-worktime/tree/v0.1.3) (2017-07-07)
84
+
85
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.2...v0.1.3)
86
+
87
+ ## [v0.1.2](https://github.com/limitusus/toggl-worktime/tree/v0.1.2) (2017-07-05)
88
+
89
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.1...v0.1.2)
90
+
91
+ ## [v0.1.1](https://github.com/limitusus/toggl-worktime/tree/v0.1.1) (2017-07-05)
92
+
93
+ [Full Changelog](https://github.com/limitusus/toggl-worktime/compare/v0.1.0...v0.1.1)
94
+
95
+
96
+
97
+ \* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
data/README.md CHANGED
@@ -13,6 +13,17 @@ worktime
13
13
  2017-06-12 13:29:09 - 2017-06-12 19:23:23
14
14
  ```
15
15
 
16
+ or you can specify year (default is "this" year)
17
+
18
+ ```console
19
+ $ toggl-worktime 2017 6 12
20
+ Tomoya Kabe
21
+ worktime
22
+ 2017-06-12 09:54:34 - 2017-06-12 12:40:45
23
+ 2017-06-12 13:29:09 - 2017-06-12 19:23:23
24
+ ```
25
+
26
+
16
27
  Weekend
17
28
 
18
29
  ```console
@@ -22,6 +33,47 @@ worktime
22
33
  nil - nil
23
34
  ```
24
35
 
36
+ ## Calendar mode
37
+
38
+ You can see your monthly worktime in calendar view
39
+
40
+ ```console
41
+ $ toggl-worktime --calendar 2021 1
42
+ ┌────────┬─────────────────┬─────────────────┬─────────────────┬─────────────────┬─────────────────┬────────┐
43
+ │Sun │Mon │Tue │Wed │Thu │Fri │Sat │
44
+ ├────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼────────┤
45
+ │ │ │ │ │ │Day: 1 │Day: 2 │
46
+ │ │ │ │ │ │- │- │
47
+ │ │ │ │ │ │00:00:00 │00:00:00│
48
+ ├────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼────────┤
49
+ │Day: 3 │Day: 4 │Day: 5 │Day: 6 │Day: 7 │Day: 8 │Day: 9 │
50
+ │- │- │10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│- │
51
+ │00:00:00│00:00:00 │07:00:00 │07:00:00 │07:00:00 │07:00:00 │00:00:00│
52
+ ├────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼────────┤
53
+ │Day: 10 │Day: 11 │Day: 12 │Day: 13 │Day: 14 │Day: 15 │Day: 16 │
54
+ │- │- │10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│- │
55
+ │00:00:00│00:00:00 │07:00:00 │07:00:00 │07:00:00 │07:00:00 │00:00:00│
56
+ ├────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼────────┤
57
+ │Day: 17 │Day: 18 │Day: 19 │Day: 20 │Day: 21 │Day: 22 │Day: 23 │
58
+ │- │10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│- │
59
+ │00:00:00│07:00:00 │07:00:00 │07:00:00 │07:00:00 │07:00:00 │00:00:00│
60
+ ├────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼────────┤
61
+ │Day: 24 │Day: 25 │Day: 26 │Day: 27 │Day: 28 │Day: 29 │Day: 30 │
62
+ │- │10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│10:00:00-18:00:00│- │
63
+ │00:00:00│07:00:00 │07:00:00 │07:00:00 │07:00:00 │07:00:00 │00:00:00│
64
+ ├────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼─────────────────┼────────┤
65
+ │Day: 31 │ │ │ │ │ │ │
66
+ │- │ │ │ │ │ │ │
67
+ │00:00:00│ │ │ │ │ │ │
68
+ └────────┴─────────────────┴─────────────────┴─────────────────┴─────────────────┴─────────────────┴────────┘
69
+ ```
70
+
71
+ You can change the beginning day of the week with `-b` option:
72
+
73
+ ```console
74
+ $ toggl-worktime --calendar 2021 1 -b Mon
75
+ ```
76
+
25
77
  ## Installation
26
78
 
27
79
  Add this line to your application's Gemfile:
@@ -50,6 +102,36 @@ abcdef0123456789
50
102
 
51
103
  You can get your API token from Toggl at your profile settings page.
52
104
 
105
+ NOTE: as of togglv8 v1.2.1, `.toggl` file **MUST NOT** end with a newline (`\010` or LF).
106
+ The recommended way to create the file is `echo -n 'YOUR_TOGGL_API_TOKEN' > ~/.toggl`.
107
+ This issue [will be fixed in the next togglv8 release](https://github.com/kanet77/togglv8/pull/21).
108
+
109
+ ### Configuration
110
+
111
+ Place configuration file in `~/.toggl_worktime`.
112
+ Or you can specify your favorite path with `-c CONFIG` option.
113
+
114
+ ```
115
+ # -*- yaml -*-
116
+
117
+ # working time interval within <working_interval_min> minutes is ignored
118
+ working_interval_min: 10
119
+ # Split the day at <day_begin_hour> o'clock
120
+ day_begin_hour: 6
121
+ # Timezone
122
+ timezone: Asia/Tokyo
123
+
124
+ # Time entries which match the condition below will not be counted as working time
125
+ # Multiple conditions can be specified as an array in top level,
126
+ # multiple keys (only "tags" for now) can be specified as hash keys in a condition,
127
+ # and multiple values can be specifeid as an array.
128
+ # Array conditions will be treated as "OR"
129
+ # Hash conditions will be treated as "AND"
130
+ ignore_conditions:
131
+ - tags:
132
+ - vacation
133
+ ```
134
+
53
135
  ### Just run
54
136
 
55
137
  ```console
data/Rakefile CHANGED
@@ -2,7 +2,15 @@
2
2
 
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rspec/core/rake_task'
5
+ require 'github_changelog_generator/task'
5
6
 
6
7
  RSpec::Core::RakeTask.new(:spec)
7
8
 
8
9
  task default: :spec
10
+
11
+ GitHubChangelogGenerator::RakeTask.new :changelog do |config|
12
+ config.user = 'limitusus'
13
+ config.project = 'toggl-worktime'
14
+ config.since_tag = 'v0.1.0'
15
+ config.future_release = '0.4.1'
16
+ end
data/exe/toggl-worktime CHANGED
@@ -3,27 +3,50 @@
3
3
 
4
4
  $LOAD_PATH << File.expand_path("#{File.dirname __FILE__}/../lib")
5
5
 
6
+ require 'optparse'
6
7
  require 'rubygems'
7
8
  require 'toggl/worktime'
8
9
 
9
10
  Version = Toggl::Worktime::VERSION
10
- MAX_WORKING_INTERVAL = 10
11
- DAY_BEGIN_HOUR = 6
12
- DEFAULT_TIMEZONE = 'Asia/Tokyo'
13
-
14
- driver = Toggl::Worktime::Driver.new(max_working_interval: MAX_WORKING_INTERVAL)
15
-
16
- month = ARGV[0].to_i
17
- day = ARGV[1].to_i
18
- hour = DAY_BEGIN_HOUR
19
- timezone = DEFAULT_TIMEZONE
20
-
21
- driver.merge!(month, day, hour, timezone)
22
- user = driver.me
23
-
24
- puts user['fullname']
25
- puts 'worktime'
26
-
27
- driver.write
28
-
29
- puts "Total worktime: #{driver.total_time}"
11
+ config_file = "#{ENV['HOME']}/.toggl_worktime"
12
+ mode = :default
13
+ week_begin = :Sun
14
+ debug = false
15
+
16
+ opt = OptionParser.new
17
+ opt.on('-c CONFIG', '--config CONFIG') { |v| config_file = v }
18
+ opt.on('-l', '--calendar', 'Calendar view mode') { mode = :calendar }
19
+ opt.on('-b', '--week-begin=VAL', 'Beginning of the week: Sun, Mon, etc.') { |v| week_begin = v }
20
+ opt.on('--debug') { |v| debug = v }
21
+ opt.parse!(ARGV)
22
+
23
+ config = Toggl::Worktime::Config.new(path: config_file)
24
+ driver = Toggl::Worktime::Driver.new(config: config)
25
+ p config if debug
26
+
27
+ case mode
28
+ when :calendar
29
+ raise ArgumentError, 'Usage: toggl-worktime --cal YEAR MONTH' if ARGV.size != 2
30
+
31
+ driver.calendar(week_begin, *ARGV.map(&:to_i)).write
32
+ else
33
+ case ARGV.size
34
+ when 3
35
+ year, month, day = *ARGV.map(&:to_i)
36
+ when 2
37
+ year = Time.now.year
38
+ month, day = *ARGV.map(&:to_i)
39
+ else
40
+ raise ArgumentError, 'Usage: toggl-worktime [YEAR] MONTH DAY'
41
+ end
42
+
43
+ driver.merge!(year, month, day)
44
+ user = driver.me
45
+
46
+ puts user['fullname']
47
+ puts 'worktime'
48
+
49
+ driver.write
50
+
51
+ puts "Total worktime: #{driver.total_time}"
52
+ end
@@ -1,11 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'togglv8'
4
+ require 'date'
4
5
  require 'json'
5
6
  require 'toggl/worktime/version'
7
+ require 'toggl/worktime/config'
6
8
  require 'toggl/worktime/merger'
7
9
  require 'toggl/worktime/time'
8
10
  require 'toggl/worktime/driver'
11
+ require 'toggl/worktime/calendar'
12
+ require 'tty-table'
9
13
 
10
14
  module Toggl
11
15
  # Main module
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Toggl
4
+ module Worktime
5
+ # Worktime Calendar
6
+ class Calendar
7
+ WEEK = %i[Sun Mon Tue Wed Thu Fri Sat].freeze
8
+
9
+ class UnknownWeekdayError < StandardError; end
10
+
11
+ def initialize(driver, zone_offset, week_begin, year, month)
12
+ @driver = driver
13
+ @zone_offset = zone_offset
14
+ @year = year
15
+ @month = month
16
+ today = ::Time.new
17
+ @days_in_month = Date.new(year, month, -1).day
18
+ @last_fetch_day = @days_in_month
19
+ @last_fetch_day = today.day if today.year == year && today.month == month && today.day < @days_in_month
20
+ @data = nil
21
+ @week_begin_day = week_begin.to_sym
22
+ raise UnknownWeekdayError if WEEK.index(@week_begin_day).nil?
23
+ end
24
+
25
+ def week
26
+ begin_index = WEEK.index(@week_begin_day)
27
+ WEEK.rotate(begin_index)
28
+ end
29
+
30
+ def write
31
+ fetch if @data.nil?
32
+ rotation = week.index(:Sun)
33
+ table = TTY::Table.new header: week
34
+ week_data = []
35
+ @day_data.each do |datum|
36
+ wday = datum.day.wday
37
+ # wday may be rotated
38
+ wday_index = (wday + rotation) % 7
39
+ week_data[wday_index] = datum.format
40
+ if wday_index == 6
41
+ table << week_data
42
+ week_data = []
43
+ end
44
+ end
45
+ # last week data may exist
46
+ unless week_data.length.zero?
47
+ # Padding
48
+ (7 - week_data.length).times do
49
+ week_data << nil
50
+ end
51
+ table << week_data
52
+ end
53
+ multi_renderer = TTY::Table::Renderer::Unicode.new(table, multiline: true)
54
+ multi_renderer.border.separator = :each_row
55
+ puts multi_renderer.render
56
+ end
57
+
58
+ def fetch
59
+ @day_data = []
60
+ (1..@days_in_month).each do |day|
61
+ dateobj = Date.new(@year, @month, day)
62
+ day_datum = nil
63
+ if day <= @last_fetch_day
64
+ @driver.merge!(@year, @month, day)
65
+ time = @driver.total_time
66
+ begin_at = @driver.work_time.first[0]&.getlocal(@zone_offset)&.strftime('%T')
67
+ end_at = @driver.work_time.last[1]&.getlocal(@zone_offset)&.strftime('%T')
68
+ day_datum = Toggl::Worktime::Day.new(dateobj, time, begin_at, end_at)
69
+ else
70
+ day_datum = Toggl::Worktime::Day.new(dateobj, 0, '', '')
71
+ end
72
+ @day_data << day_datum
73
+ end
74
+ end
75
+ end
76
+
77
+ # One-day datum
78
+ class Day
79
+ attr_reader :day
80
+
81
+ def initialize(day, time, begin_at, end_at)
82
+ @day = day
83
+ @time = time
84
+ @begin = begin_at
85
+ @end = end_at
86
+ end
87
+
88
+ def format
89
+ "Day: #{day.day}\n#{@begin}-#{@end}\n#{@time}"
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module Toggl
6
+ module Worktime
7
+ # Config for Toggl::Worktime
8
+ class Config
9
+ attr_accessor :foo
10
+
11
+ ATTRS = %i[
12
+ working_interval_min
13
+ day_begin_hour
14
+ timezone
15
+ ignore_conditions
16
+ ].freeze
17
+
18
+ ATTR_DEFAULTS = {
19
+ working_interval_min: 10,
20
+ day_begin_hour: 6,
21
+ timezone: 'Asia/Tokyo',
22
+ ignore_conditions: []
23
+ }.freeze
24
+
25
+ ATTRS.each do |attr|
26
+ attr_accessor attr
27
+ end
28
+
29
+ def initialize(args)
30
+ c = self.class.load_config(args[:path])
31
+ attr_set(c)
32
+ end
33
+
34
+ class << self
35
+ def load_config(path)
36
+ YAML.safe_load(File.open(path).read).transform_keys(&:to_sym)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def attr_set(hash)
43
+ ATTRS.each do |k|
44
+ send((k.to_s + '=').to_sym, hash.key?(k) ? hash[k] : ATTR_DEFAULTS[k])
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -4,31 +4,57 @@ module Toggl
4
4
  module Worktime
5
5
  # Toggle API driver
6
6
  class Driver
7
+ ONE_DAY_SECONDS = 86_400
8
+
7
9
  attr_reader :toggl
8
10
  attr_reader :work_time
9
11
 
10
- def initialize(max_working_interval: 10)
12
+ def initialize(config:)
11
13
  @toggl = TogglV8::API.new
12
- @max_working_interval = max_working_interval
14
+ @config = config
13
15
  @merger = nil
14
16
  @work_time = nil
17
+ @zone_offset = Toggl::Worktime::Time.zone_offset(@config.timezone)
18
+ @calendar = nil
19
+ end
20
+
21
+ def calendar(week_begin, year, month)
22
+ @calendar = Toggl::Worktime::Calendar.new(self, @zone_offset, week_begin, year, month)
23
+ end
24
+
25
+ def time_entries(year, month, day)
26
+ beginning_day = ::Time.new(
27
+ year, month, day, @config.day_begin_hour, 0, 0, @zone_offset
28
+ )
29
+ ending_day = beginning_day + ONE_DAY_SECONDS
30
+ start_iso = beginning_day.strftime('%FT%T%:z')
31
+ end_iso = ending_day.strftime('%FT%T%:z')
32
+ toggl.get_time_entries(start_date: start_iso, end_date: end_iso)
15
33
  end
16
34
 
17
- def time_entries(month, day, hour, timezone)
18
- now = DateTime.now
19
- offset = Toggl::Worktime::Time.zone_offset(timezone)
20
- beginning_day = DateTime.new(now.year, month, day, hour, 0, 0, offset)
21
- ending_day = beginning_day + 1
22
- toggl.get_time_entries(start_date: beginning_day.iso8601, end_date: ending_day.iso8601)
35
+ # time_entries filter with @config.ignore_conditions
36
+ def filter_entries(entries)
37
+ pass_l = lambda { |entry|
38
+ @config.ignore_conditions.none? do |cond|
39
+ cond.keys.all? do |key|
40
+ case key
41
+ when 'tags'
42
+ entry['tags']&.any? { |t| cond[key].include?(t) }
43
+ end
44
+ end
45
+ end
46
+ }
47
+ entries.select { |e| pass_l.call(e) }
23
48
  end
24
49
 
25
50
  def me
26
51
  @toggl.me(true)
27
52
  end
28
53
 
29
- def merge!(month, day, hour, timezone)
30
- time_entries = time_entries(month, day, hour, timezone)
31
- @merger = Toggl::Worktime::Merger.new(time_entries, timezone, @max_working_interval)
54
+ def merge!(year, month, day)
55
+ time_entries = time_entries(year, month, day)
56
+ time_entries = filter_entries(time_entries)
57
+ @merger = Toggl::Worktime::Merger.new(time_entries, @config)
32
58
  @work_time = @merger.merge
33
59
  end
34
60
 
@@ -40,17 +66,16 @@ module Toggl
40
66
  end
41
67
  end
42
68
 
43
- def time_expr(t)
44
- t ? t.strftime('%F %T') : 'nil'
69
+ def time_expr(time)
70
+ time ? time.getlocal(@zone_offset).strftime('%F %T') : 'nil'
45
71
  end
46
72
 
47
73
  def total_time
48
- time = @merger.total_time
49
- total_seconds = (time * 86400).to_i
74
+ total_seconds = @merger.total_time.to_i
50
75
  hours = total_seconds / (60 * 60)
51
76
  minutes = (total_seconds - (hours * 60 * 60)) / 60
52
77
  seconds = total_seconds % 60
53
- format("%02d:%02d:%02d", hours, minutes, seconds)
78
+ format('%02d:%02d:%02d', hours, minutes, seconds)
54
79
  end
55
80
  end
56
81
  end
@@ -1,17 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'time'
4
+
3
5
  module Toggl
4
6
  module Worktime
5
7
  # Time-entries merger
6
8
  class Merger
7
9
  attr_reader :total_time
8
10
 
9
- ONE_DAY_MINUTES = 24 * 60
11
+ ONE_MINUTE_SECONDS = 60
10
12
 
11
- def initialize(time_entries, timezone, max_working_interval)
13
+ def initialize(time_entries, config)
12
14
  @time_entries = time_entries
13
- @timezone = timezone
14
- @max_working_interval = max_working_interval
15
+ @config = config
15
16
  @current_start = nil
16
17
  @current_stop = nil
17
18
  @continuing = true
@@ -27,15 +28,23 @@ module Toggl
27
28
  next
28
29
  end
29
30
  work_time << [@current_start, @current_stop]
31
+ count_total_time
30
32
  @current_start = start
31
33
  @current_stop = stop
32
34
  end
33
35
  work_time << [@current_start, @last_stop]
36
+ count_total_time
34
37
  work_time
35
38
  end
36
39
 
40
+ def count_total_time
41
+ return if @current_start.nil? || @current_stop.nil?
42
+
43
+ @total_time += @current_stop - @current_start
44
+ end
45
+
37
46
  def time_entries_each
38
- zone_offset = Toggl::Worktime::Time.zone_offset(@timezone)
47
+ zone_offset = Toggl::Worktime::Time.zone_offset(@config.timezone)
39
48
  @time_entries.each do |te|
40
49
  start = parse_date(te['start'], zone_offset)
41
50
  stop = parse_date(te['stop'], zone_offset)
@@ -43,9 +52,7 @@ module Toggl
43
52
  @current_start = start if @current_start.nil?
44
53
  @current_stop = stop if @current_stop.nil?
45
54
  if start.nil? || stop.nil?
46
- warn "start or stop time is nil: total time may be incomplete"
47
- else
48
- @total_time += stop - start
55
+ warn 'start or stop time is nil: total time may be incomplete'
49
56
  end
50
57
  yield [start, stop]
51
58
  end
@@ -53,13 +60,15 @@ module Toggl
53
60
 
54
61
  def parse_date(date, zone_offset)
55
62
  return nil if date.nil?
56
- DateTime.parse(date).new_offset(zone_offset)
63
+
64
+ ::Time.parse(date).getlocal(zone_offset)
57
65
  end
58
66
 
59
67
  def continuing(start)
60
68
  return true if @current_stop.nil?
61
- interval = (start - @current_stop) * ONE_DAY_MINUTES
62
- @continuing = interval < @max_working_interval
69
+
70
+ interval = (start - @current_stop) / ONE_MINUTE_SECONDS
71
+ @continuing = interval < @config.working_interval_min
63
72
  end
64
73
  end
65
74
  end
@@ -4,11 +4,11 @@ module Toggl
4
4
  module Worktime
5
5
  # Timezone
6
6
  module Time
7
- # rational
7
+ # Seconds
8
8
  def self.zone_offset(timezone)
9
9
  tz = ENV['TZ']
10
10
  ENV['TZ'] = timezone
11
- offset = DateTime.now.offset
11
+ offset = ::Time.now.utc_offset
12
12
  ENV['TZ'] = tz
13
13
  offset
14
14
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Toggl
4
4
  module Worktime
5
- VERSION = '0.2.0'
5
+ VERSION = '0.4.1'
6
6
  end
7
7
  end
@@ -1,7 +1,6 @@
1
- # coding: utf-8
2
1
  # frozen_string_literal: true
3
2
 
4
- lib = File.expand_path('../lib', __FILE__)
3
+ lib = File.expand_path('lib', __dir__)
5
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
5
  require 'toggl/worktime/version'
7
6
 
@@ -23,12 +22,14 @@ Gem::Specification.new do |spec|
23
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
23
  spec.require_paths = ['lib']
25
24
 
26
- spec.add_dependency 'togglv8'
27
25
  spec.add_dependency 'awesome_print'
26
+ spec.add_dependency 'togglv8-limitusus'
27
+ spec.add_dependency 'tty-table'
28
+ spec.add_development_dependency 'bundler', '>= 2.2.10'
29
+ spec.add_development_dependency 'github_changelog_generator'
28
30
  spec.add_development_dependency 'pry'
31
+ spec.add_development_dependency 'rake', '>= 12.3.3'
29
32
  spec.add_development_dependency 'rb-readline'
30
- spec.add_development_dependency 'rubocop'
31
- spec.add_development_dependency 'bundler', '~> 1.15'
32
- spec.add_development_dependency 'rake', '~> 10.0'
33
33
  spec.add_development_dependency 'rspec', '~> 3.0'
34
+ spec.add_development_dependency 'rubocop'
34
35
  end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toggl-worktime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomoya KABE
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-02 00:00:00.000000000 Z
11
+ date: 2021-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: togglv8
14
+ name: awesome_print
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: awesome_print
28
+ name: togglv8-limitusus
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,13 +39,13 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: pry
42
+ name: tty-table
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
- type: :development
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -53,7 +53,21 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rb-readline
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 2.2.10
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.2.10
69
+ - !ruby/object:Gem::Dependency
70
+ name: github_changelog_generator
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
73
  - - ">="
@@ -67,7 +81,7 @@ dependencies:
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
- name: rubocop
84
+ name: pry
71
85
  requirement: !ruby/object:Gem::Requirement
72
86
  requirements:
73
87
  - - ">="
@@ -81,33 +95,33 @@ dependencies:
81
95
  - !ruby/object:Gem::Version
82
96
  version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
- name: bundler
98
+ name: rake
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
- - - "~>"
101
+ - - ">="
88
102
  - !ruby/object:Gem::Version
89
- version: '1.15'
103
+ version: 12.3.3
90
104
  type: :development
91
105
  prerelease: false
92
106
  version_requirements: !ruby/object:Gem::Requirement
93
107
  requirements:
94
- - - "~>"
108
+ - - ">="
95
109
  - !ruby/object:Gem::Version
96
- version: '1.15'
110
+ version: 12.3.3
97
111
  - !ruby/object:Gem::Dependency
98
- name: rake
112
+ name: rb-readline
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
- - - "~>"
115
+ - - ">="
102
116
  - !ruby/object:Gem::Version
103
- version: '10.0'
117
+ version: '0'
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
- - - "~>"
122
+ - - ">="
109
123
  - !ruby/object:Gem::Version
110
- version: '10.0'
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rspec
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +136,20 @@ dependencies:
122
136
  - - "~>"
123
137
  - !ruby/object:Gem::Version
124
138
  version: '3.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
125
153
  description: Summarise Toggl Time Entries
126
154
  email:
127
155
  - limit.usus@gmail.com
@@ -134,6 +162,7 @@ files:
134
162
  - ".rspec"
135
163
  - ".rubocop.yml"
136
164
  - ".travis.yml"
165
+ - CHANGELOG.md
137
166
  - CODE_OF_CONDUCT.md
138
167
  - Gemfile
139
168
  - LICENSE.txt
@@ -143,6 +172,8 @@ files:
143
172
  - bin/setup
144
173
  - exe/toggl-worktime
145
174
  - lib/toggl/worktime.rb
175
+ - lib/toggl/worktime/calendar.rb
176
+ - lib/toggl/worktime/config.rb
146
177
  - lib/toggl/worktime/driver.rb
147
178
  - lib/toggl/worktime/merger.rb
148
179
  - lib/toggl/worktime/time.rb
@@ -167,8 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
198
  - !ruby/object:Gem::Version
168
199
  version: '0'
169
200
  requirements: []
170
- rubyforge_project:
171
- rubygems_version: 2.7.3
201
+ rubygems_version: 3.1.4
172
202
  signing_key:
173
203
  specification_version: 4
174
204
  summary: '["Summarise Toggl Time Entries", "Summarise Toggl Time Entries", "https://github.com/limitusus/toggl-worktime"]'