Module: Togglapper::Searches::Base

Included in:
Tag
Defined in:
lib/togglapper/searches/base.rb

Instance Method Summary collapse

Instance Method Details

#entries(refresh: false) ⇒ Object

entry = toggl の個タスクAPIの持ち主の entry 一覧を取得するex: entryの中身

"id"=>491600812,
"wid"=>1552948,
"billable"=>false,
"start"=>"2016-12-01T15:42:21+00:00",
"stop"=>"2016-12-01T15:42:53+00:00",
"duration"=>32,
"description"=>"aaa",
"tags"=>["#2661"],
"duronly"=>false,
"at"=>"2016-12-01T15:42:53+00:00",
"uid"=>2344433,

}



23
24
25
26
27
28
29
# File 'lib/togglapper/searches/base.rb', line 23

def entries(refresh: false)
  if refresh
    @entries = client.my_time_entries
  else
    @entries ||= client.my_time_entries
  end
end

#entry_info(entry = latest_entry) ⇒ Object

entry 情報から「タグ情報」「説明文(Description)」「タスク実績時間」を取得する



70
71
72
73
74
75
76
# File 'lib/togglapper/searches/base.rb', line 70

def (entry = latest_entry)
  work_time = get_diff_time_by_entry(entry, '%S')
  tags = entry["tags"] || nil
  description = entry["description"] || nil

  { description: description, tags: tags, work_time: work_time }
end

#entry_info_string(entry = latest_entry) ⇒ Object

entry info の String Version



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/togglapper/searches/base.rb', line 79

def (entry = latest_entry)
  diff_time = get_diff_time_by_entry(entry, '%H')

  # 差分時間が 0.1h 以下の表示になるようであれば 分表示に変更する
  if diff_time >= 0.1
    work_time = "#{diff_time.round(2)}h"
  else
    work_time = "#{(diff_time * 60).round(2)}m"
  end

  tag = entry["tags"].join(" ") unless entry["tags"].nil?
  description = entry["description"]

  "#{tag} #{description} \(#{work_time}\)"
end

#get_diff_time_by_entry(entry = latest_entry, format = nil) ⇒ Object

entry 情報からその entry にどれだけの時間を使ったか返す。返すのは基本的にUnix 秒。ただし format を指定すれば時間や分の単位で返す。



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/togglapper/searches/base.rb', line 43

def get_diff_time_by_entry(entry = latest_entry, format = nil)
  if entry["duration"] && entry["stop"]
    diff_time = entry["duration"]
  else
    # 現在作業中(終了時間がない)であれば現在日時を終了時間として取得
    stop_time_org  = Time.now

    # 差分の時間数を計算
    start_time = Time.parse(entry["start"]).getlocal("+09:00")
    stop_time  = stop_time_org.getlocal("+09:00")
    diff_time  = stop_time - start_time
  end

  if format.nil?
    diff_time.to_i
  elsif format == '%H' || format == '%h'
    diff_time/3600
  elsif format == '%M' || format == '%m'
    diff_time/60
  elsif format == '%S' || format == '%s'
    diff_time.to_i
  else
    raise 'invalid format: #{format}. exsample: %H(h), %M(m), %S(s)'
  end
end

#latest_entryObject



31
32
33
# File 'lib/togglapper/searches/base.rb', line 31

def latest_entry
  entries.sort_by{ |entry| entry["start"] }.last
end

#working_entryObject



35
36
37
38
39
# File 'lib/togglapper/searches/base.rb', line 35

def working_entry
  if latest_entry["stop"].nil?
    latest_entry
  end
end