module TogglV8::Connection

Constants

API_TOKEN
DELAY_SEC
MAX_RETRIES
TOGGL_FILE

Attributes

v9_conn[R]

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
    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 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={}) click to toggle source
# 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 == {}

  # if we know explicitly the response is not json, return it
  return full_resp.body if %w[.pdf .csv .xls].include? extension

  # expect that implicit route format responses are json
  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
    # Oj.load now raises EncodingError for responses that are simple strings instead of json (like /revision)
    return full_resp.body
  end
end
get_all(resource, params={}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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='') click to toggle source
# 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='') click to toggle source
# 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=[]) 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