Class: Toggl

Inherits:
Object
  • Object
show all
Defined in:
lib/toggl.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, nonbill) ⇒ Toggl

Returns a new instance of Toggl.



2
3
4
5
6
7
8
9
10
# File 'lib/toggl.rb', line 2

def initialize (api_key, nonbill)
    @api_key = api_key
    @nonbill = nonbill

    @sess = Patron::Session.new
    @sess.base_url = "https://www.toggl.com"
    @sess.username = api_key
    @sess.password = "api_token"
end

Instance Method Details

#entries(from, to) ⇒ Object



18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/toggl.rb', line 18

def entries (from, to)
    log = {}

    res = @sess.get('/api/v5/time_entries.json?start_date=' + from + '&end_date=' + to).body

    JSON.parse(res)["data"].each do |item|
        raw = item["description"]

        # Check all entries start with a issue number
        if !res = raw.match(/^([0-9]+) (.+)$/)
            raise 'Item does not have an issue number: ' + raw
        end

        ims  = res[1]
        desc = res[2]
        duration = (item["duration"] / 60)

        if log[ims] == nil
            log[ims] = []
        end

        # Squash times with the same descriptions together
        exists = false
        log[ims].each do |exist|
            if exist[:desc] == desc
                exist[:duration] += duration
                exists = true
            end
        end

        # TOOD: Rounding should be on the total time for an issue.
        # not per note... either need to bunch them together or 
        # round the final note???

        free = false
            orig = duration
        if !@nonbill.member? ims.to_i
            duration = ((duration.to_f/10).ceil*10).round
            free = true
        end

        if !exists
            log[ims].push({:desc => desc, :duration => duration, :orig => orig})
         end
    end
    return log
end

#todayObject



12
13
14
15
16
# File 'lib/toggl.rb', line 12

def today
    from = Time.utc(Time.now.year, Time.now.month, Time.now.day).iso8601
    to = Time.utc(Time.now.year, Time.now.month, Time.now.day, 23, 59, 59).iso8601
    return self.entries(from, to)
end