Module: TogglV8::Connection
Constant Summary
collapse
- DELAY_SEC =
1
- MAX_RETRIES =
3
- API_TOKEN =
'api_token'
- TOGGL_FILE =
'.toggl'
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logging
#debug, included, logger, #logger, logger=
Class Method Details
.open(username = nil, password = API_TOKEN, url = nil, opts = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/togglv8/connection.rb', line 34
def self.open(username=nil, password=API_TOKEN, url=nil, opts={})
username, password = qualify(username, password)
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
|
.qualify(username, password) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/togglv8/connection.rb', line 16
def self.qualify(username, password)
if username.nil? && password == API_TOKEN
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" +
" 3) parameters: (username, password).\n" +
"\n\tSee https://github.com/toggl/toggl_api_docs/blob/master/chapters/authentication.md"
end
end
return username, password
end
|
Instance Method Details
#_call_api(procs) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/togglv8/connection.rb', line 57
def _call_api(procs)
full_resp = nil
i = 0
loop do
i += 1
full_resp = procs[:api_call].call
logger.ap(full_resp.env, :debug)
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
110
111
112
113
114
115
116
|
# File 'lib/togglv8/connection.rb', line 110
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/togglv8/connection.rb', line 76
def get(resource, params={})
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 == {}
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
return full_resp.body
end
end
|
#post(resource, data = '') ⇒ Object
92
93
94
95
96
97
98
99
|
# File 'lib/togglv8/connection.rb', line 92
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
101
102
103
104
105
106
107
108
|
# File 'lib/togglv8/connection.rb', line 101
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
47
48
49
50
51
52
53
54
55
|
# File 'lib/togglv8/connection.rb', line 47
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
|