Class: TogglV8::ReportsV2

Inherits:
Object
  • Object
show all
Includes:
Connection
Defined in:
lib/reportsv2.rb

Constant Summary collapse

REPORTS_V2_URL =
TOGGL_REPORTS_URL + 'v2/'

Constants included from Connection

Connection::API_TOKEN, Connection::DELAY_SEC, Connection::MAX_RETRIES, Connection::TOGGL_FILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Connection

#_call_api, #delete, #get, open, #post, #put, qualify, #requireParams

Methods included from Logging

#debug, included, logger, #logger, logger=

Constructor Details

#initialize(opts = {}) ⇒ ReportsV2

Returns a new instance of ReportsV2.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/reportsv2.rb', line 13

def initialize(opts={})
  debug(false)

  @user_agent = 'togglv8'

  username = opts[:api_token]
  if username.nil?
    toggl_api_file = opts[:toggl_api_file] || File.join(Dir.home, TOGGL_FILE)
    if FileTest.exist?(toggl_api_file) then
      username = IO.read(toggl_api_file)
    else
      raise "Expecting\n" +
        " 1) api_token in file #{toggl_api_file}, or\n" +
        " 2) parameter: (api_token), or\n" +
        "\n\tSee https://github.com/toggl/toggl_api_docs/blob/master/reports.md#authentication"
    end
  end

  @conn = TogglV8::Connection.open(username, API_TOKEN, REPORTS_V2_URL, opts)
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/reportsv2.rb', line 9

def conn
  @conn
end

#workspace_idObject

Returns the value of attribute workspace_id.



11
12
13
# File 'lib/reportsv2.rb', line 11

def workspace_id
  @workspace_id
end

Instance Method Details

#details(extension = '', opts = {}) ⇒ Object



85
86
87
# File 'lib/reportsv2.rb', line 85

def details(extension='', opts={})
  report('details', extension, opts)
end

#envObject



135
136
137
# File 'lib/reportsv2.rb', line 135

def env
  get "env"
end

#error400Object



139
140
141
# File 'lib/reportsv2.rb', line 139

def error400
  get "error400"
end

#error500Object



143
144
145
# File 'lib/reportsv2.rb', line 143

def error500
  get "error500"
end

#indexObject



123
124
125
# File 'lib/reportsv2.rb', line 123

def index
  get "index"
end

#project(opts = {}) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/reportsv2.rb', line 127

def project(opts={})
  raise "workspace_id is required" if @workspace_id.nil?
  get "project", {
    'user_agent': @user_agent,
    'workspace_id': @workspace_id,
  }.merge(opts)
end

#report(type, extension, opts) ⇒ Object

extension can be one of [‘.pdf’, ‘.csv’, ‘.xls’]. Possibly others?



73
74
75
76
77
78
79
# File 'lib/reportsv2.rb', line 73

def report(type, extension, opts)
  raise "workspace_id is required" if @workspace_id.nil?
  get "#{type}#{extension}", {
    'user_agent': @user_agent,
    'workspace_id': @workspace_id,
  }.merge(opts)
end

#revisionObject



119
120
121
# File 'lib/reportsv2.rb', line 119

def revision
  get "revision"
end

#summary(extension = '', opts = {}) ⇒ Object



89
90
91
# File 'lib/reportsv2.rb', line 89

def summary(extension='', opts={})
  report('summary', extension, opts)
end

#weekly(extension = '', opts = {}) ⇒ Object



81
82
83
# File 'lib/reportsv2.rb', line 81

def weekly(extension='', opts={})
  report('weekly', extension, opts)
end

#write_details(filename, opts = {}) ⇒ Object



107
108
109
110
111
# File 'lib/reportsv2.rb', line 107

def write_details(filename, opts={})
  write_report(filename) do |extension|
    details(extension, opts)
  end
end

#write_report(filename) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/reportsv2.rb', line 93

def write_report(filename)
  extension = File.extname(filename)
  report = yield(extension)
  File.open(filename, "wb") do |file|
    file.write(report)
  end
end

#write_summary(filename, opts = {}) ⇒ Object



113
114
115
116
117
# File 'lib/reportsv2.rb', line 113

def write_summary(filename, opts={})
  write_report(filename) do |extension|
    summary(extension, opts)
  end
end

#write_weekly(filename, opts = {}) ⇒ Object



101
102
103
104
105
# File 'lib/reportsv2.rb', line 101

def write_weekly(filename, opts={})
  write_report(filename) do |extension|
    weekly(extension, opts)
  end
end