module TogglV8::Connection

Constants

API_TOKEN
DELAY_SEC
MAX_RETRIES
TOGGL_FILE

Public Class Methods

open(username=nil, password=API_TOKEN, url=nil, opts={}) click to toggle source
# 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

Public Instance Methods

_call_api(procs) click to toggle source
# 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) click to toggle source
# 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={}, whole_body = false) click to toggle source
# File lib/togglv8/connection.rb, line 57
def get(resource, params={}, whole_body = false)
  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') && whole_body == false
    return resp
  rescue Oj::ParseError
    return full_resp.body
  end
end
post(resource, data='') click to toggle source
# 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='') click to toggle source
# 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=[]) click to toggle source
# 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