Module: TogglV8::Connection

Includes:
Logging
Included in:
API, ReportsV2
Defined in:
lib/togglv8/connection.rb

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



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.headers = { "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
55
# File 'lib/togglv8/connection.rb', line 38

def _call_api(procs)
  # logger.debug(procs[:debug_output].call)
  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.headers['warning'] if full_resp.headers['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



91
92
93
94
95
96
97
# File 'lib/togglv8/connection.rb', line 91

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/togglv8/connection.rb', line 57

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



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

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



82
83
84
85
86
87
88
89
# File 'lib/togglv8/connection.rb', line 82

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

Raises:

  • (ArgumentError)


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