class Teamlab::Request

Public Class Methods

new(config, api_additive) click to toggle source
# File lib/teamlab/request.rb, line 12
def initialize(config, api_additive)
  @config = config
  @api_additive = api_additive.to_s
end

Public Instance Methods

delete(*args) click to toggle source
# File lib/teamlab/request.rb, line 29
def delete(*args)
  request(:delete, args)
end
get(*args) click to toggle source
# File lib/teamlab/request.rb, line 21
def get(*args)
  request(:get, args)
end
post(*args) click to toggle source
# File lib/teamlab/request.rb, line 17
def post(*args)
  request(:post, args)
end
put(*args) click to toggle source
# File lib/teamlab/request.rb, line 25
def put(*args)
  request(:put, args)
end

Private Instance Methods

api_path() click to toggle source
# File lib/teamlab/request.rb, line 57
def api_path
  @config&.api_path || Teamlab.config.api_path
end
generate_request_url(command) click to toggle source
# File lib/teamlab/request.rb, line 69
def generate_request_url(command)
  server + api_path + @api_additive + command
end
headers() click to toggle source
# File lib/teamlab/request.rb, line 61
def headers
  @config&.headers || Teamlab.config.headers
end
init_proxy(opts) click to toggle source

@param opts [Hash] options to init @return [Hash] options

# File lib/teamlab/request.rb, line 86
def init_proxy(opts)
  return opts unless proxy

  opts[:http_proxyaddr] ||= proxy.proxy_address
  opts[:http_proxyport] ||= proxy.proxy_port
  opts[:http_proxyuser] ||= proxy.proxy_user
  opts[:http_proxypass] ||= proxy.proxy_pass
  opts
end
parse_args(args, type) click to toggle source
# File lib/teamlab/request.rb, line 73
def parse_args(args, type)
  command = args.first.instance_of?(Array) ? "/#{args.shift.join('/')}" : args.shift.to_s
  opts = {}
  opts[:body] = args.last.instance_of?(Hash) ? args.pop : {}
  opts[:body].delete_if { |_key, value| value == [] }
  opts[:headers] = headers
  opts = init_proxy(opts)
  opts[:query] = opts.delete(:body) if type == :get
  [command, opts]
end
proxy() click to toggle source
# File lib/teamlab/request.rb, line 65
def proxy
  @config&.proxy || Teamlab.config.proxy
end
request(type, args) click to toggle source
# File lib/teamlab/request.rb, line 35
def request(type, args)
  command, opts = parse_args(args, type)
  url = generate_request_url(command)
  attempts = 0
  begin
    response = Teamlab::Response.new(HTTParty.send(type, url, opts))
  rescue Timeout::Error => e
    attempts += 1
    retry if attempts < 3
    raise "Can't #{type} to #{url} because of TimeoutError: #{e}"
  rescue StandardError => e
    raise e
  end
  raise("Error #{response.code}: #{response.error}") unless response.success

  response
end
server() click to toggle source
# File lib/teamlab/request.rb, line 53
def server
  @config&.server || Teamlab.config.server
end