Class: Everhour2toggl::Toggl
- Inherits:
-
Object
- Object
- Everhour2toggl::Toggl
- Defined in:
- lib/everhour2toggl/toggl.rb
Defined Under Namespace
Classes: Post
Instance Method Summary collapse
- #convert ⇒ Object
- #converted_str ⇒ Object
- #groups_by_date(data) ⇒ Object
-
#initialize(input:, starting_time:, interval: 3600, wid:, pid:, tags:) ⇒ Toggl
constructor
A new instance of Toggl.
- #request_bodies(group) ⇒ Object
Constructor Details
#initialize(input:, starting_time:, interval: 3600, wid:, pid:, tags:) ⇒ Toggl
Returns a new instance of Toggl.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/everhour2toggl/toggl.rb', line 6 def initialize(input:, starting_time:, interval: 3600, wid:, pid:, tags:) begin raise 'Error' unless pid || wid rescue => e puts "#{e}: wid must be required if pid is not supplied" end @input = input @starting_time = starting_time @interval = interval @wid = wid @pid = pid @tags = end |
Instance Method Details
#convert ⇒ Object
20 21 22 23 24 25 |
# File 'lib/everhour2toggl/toggl.rb', line 20 def convert output_path = "toggl_entries" # output file name is same with input file file_generator = Everhour2toggl::FileGenerator.new(output: output_path, filename: "#{File.basename(@input)}") file_generator.generate(converted_str) end |
#converted_str ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/everhour2toggl/toggl.rb', line 27 def converted_str file = File.read(@input) data = JSON.parse(file) groups = groups_by_date(data) bodies = groups.map do |group| request_bodies(group) end.flatten JSON.pretty_generate(bodies) end |
#groups_by_date(data) ⇒ Object
37 38 39 |
# File 'lib/everhour2toggl/toggl.rb', line 37 def groups_by_date(data) data.group_by { |d| d['date'] } end |
#request_bodies(group) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/everhour2toggl/toggl.rb', line 41 def request_bodies(group) # convert group[0](date) + starting_time to Time # e.g. 2019-01-04 10:00:00 +0900 start = Time.parse([group[0], @starting_time].join(' ')) entry_array = group[1] offset = 0 request_bodies = [] entry_array.each do |entry| request_bodies << { "time_entry": { "description": entry['task']['name'], "wid": @wid, "pid": @pid, "duration": entry['time'], "start": (start + offset).iso8601, "created_with": "everhour2toggl", "tags": @tags&.split(',') }.compact } offset += entry['time'] + @interval end request_bodies end |