Module: TogglV8::Connection
Constant Summary
collapse
- DELAY_SEC =
1
- MAX_RETRIES =
3
- API_TOKEN =
'api_token'
- TOGGL_FILE =
'.toggl'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logging
#debug, included, logger, #logger, logger=
Instance Attribute Details
#v9_conn ⇒ Object
Returns the value of attribute v9_conn.
129
130
131
|
# File 'lib/togglv8/connection.rb', line 129
def v9_conn
@v9_conn
end
|
Class Method Details
.open(username = nil, password = API_TOKEN, url = nil, opts = {}) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/togglv8/connection.rb', line 16
def self.open(username=nil, password=API_TOKEN, url=nil, opts={})
raise 'Missing URL' if url.nil?
Faraday.new(:url => url, :ssl => {:verify => true}) do |faraday|
faraday.request :url_encoded
faraday.response :logger, Logger.new('faraday.log') if opts[:log]
faraday.adapter Faraday.default_adapter
faraday. = { "Content-Type" => "application/json" }
faraday.basic_auth username, password
end
end
|
Instance Method Details
#_call_api(procs) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/togglv8/connection.rb', line 38
def _call_api(procs)
full_resp = nil
i = 0
loop do
i += 1
full_resp = procs[:api_call].call
break if full_resp.status != 429 || i >= MAX_RETRIES
sleep(DELAY_SEC)
end
raise full_resp.['warning'] if full_resp.['warning']
raise "HTTP Status: #{full_resp.status}" unless full_resp.success?
return {} if full_resp.body.nil? || full_resp.body == 'null'
full_resp
end
|
#delete(resource) ⇒ Object
131
132
133
134
135
136
137
|
# File 'lib/togglv8/connection.rb', line 131
def delete(resource)
resource.gsub!('+', '%2B')
full_resp = _call_api(debug_output: lambda { "DELETE #{resource}" },
api_call: lambda { self.conn.delete(resource) } )
return {} if full_resp == {}
full_resp.body
end
|
#get(resource, params = {}) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/togglv8/connection.rb', line 79
def get(resource, params={})
extension = File.extname(resource)
query_params = params.map { |k,v| "#{k}=#{v}" }.join('&')
resource += "?#{query_params}" unless query_params.empty?
resource.gsub!('+', '%2B')
full_resp = _call_api(debug_output: lambda { "GET #{resource}" },
api_call: lambda { self.conn.get(resource) } )
return {} if full_resp == {}
return full_resp.body if %w[.pdf .csv .xls].include? extension
begin
resp = Oj.load(full_resp.body)
return resp['data'] if resp.respond_to?(:has_key?) && resp.has_key?('data')
return resp
rescue Oj::ParseError, EncodingError
return full_resp.body
end
end
|
#get_all(resource, params = {}) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/togglv8/connection.rb', line 56
def get_all(resource, params={})
resp = get_paginated(resource, params)
(2..resp[:pages]).reduce(resp[:data]) do |data, page_number|
data.concat(get(resource, params.merge(page: page_number)))
end
end
|
#get_paginated(resource, params = {}, page = 1) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/togglv8/connection.rb', line 63
def get_paginated(resource, params={}, page=1)
query_params = params.map { |k,v| "#{k}=#{v}" }.join('&')
resource += "?#{query_params}" unless query_params.empty?
resource.gsub!('+', '%2B')
full_resp = _call_api(debug_output: lambda { "GET #{resource}" },
api_call: lambda { self.conn.get(resource) } )
resp = Oj.load(full_resp.body)
total_cnt = resp['total_count']
per_page = resp['per_page']
pages = total_cnt / per_page
unless (total_cnt % per_page) == 0
pages = pages + 1
end
{data: resp['data'], pages: pages}
end
|
#patch_v9(resource, ops) ⇒ Object
122
123
124
125
126
127
|
# File 'lib/togglv8/connection.rb', line 122
def patch_v9(resource, ops)
full_resp = _call_api(debug_output: lambda { "PATCH #{resource} / #{ops}" },
api_call: lambda { self.v9_conn.patch(resource, Oj.dump(ops)) } )
return {} if full_resp == {}
Oj.load(full_resp.body)
end
|
#post(resource, data = '') ⇒ Object
104
105
106
107
108
109
110
111
|
# File 'lib/togglv8/connection.rb', line 104
def post(resource, data='')
resource.gsub!('+', '%2B')
full_resp = _call_api(debug_output: lambda { "POST #{resource} / #{data}" },
api_call: lambda { self.conn.post(resource, Oj.dump(data)) } )
return {} if full_resp == {}
resp = Oj.load(full_resp.body)
resp['data']
end
|
#put(resource, data = '') ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/togglv8/connection.rb', line 113
def put(resource, data='')
resource.gsub!('+', '%2B')
full_resp = _call_api(debug_output: lambda { "PUT #{resource} / #{data}" },
api_call: lambda { self.conn.put(resource, Oj.dump(data)) } )
return {} if full_resp == {}
resp = Oj.load(full_resp.body)
resp['data']
end
|
#requireParams(params, fields = []) ⇒ Object
28
29
30
31
32
33
34
35
36
|
# File 'lib/togglv8/connection.rb', line 28
def requireParams(params, fields=[])
raise ArgumentError, 'params is not a Hash' unless params.is_a? Hash
return if fields.empty?
errors = []
for f in fields
errors.push("params[#{f}] is required") unless params.has_key?(f)
end
raise ArgumentError, errors.join(', ') if !errors.empty?
end
|