class LabClient::HTTP

Request Helper

Attributes

settings[RW]

Public Class Methods

new(settings) click to toggle source
# File lib/labclient/http.rb, line 7
def initialize(settings)
  self.settings = settings
end

Public Instance Methods

disable_ssl() click to toggle source
# File lib/labclient/http.rb, line 38
def disable_ssl
  { ssl_verifyhost: 0, ssl_verifypeer: false }
end
headers(dump_json) click to toggle source
# File lib/labclient/http.rb, line 42
def headers(dump_json)
  default_headers = {

    'Accept' => 'application/json',
    'User-Agent' => "LabClient #{LabClient::VERSION}"
  }

  token_type = settings[:token_type]
  default_headers[token_type] = if token_type == 'Authorization'
                                  "Bearer #{settings[:token]}"
                                else
                                  settings[:token]
                                end

  default_headers['Content-Type'] = 'application/json' if dump_json

  default_headers
end
request(method, path, body = {}, dump_json = true) click to toggle source
# File lib/labclient/http.rb, line 20
def request(method, path, body = {}, dump_json = true)
  options = { method: method, headers: headers(dump_json) }

  if body && !body.empty?
    case method
    when :get
      options[:params] = body
    else
      # File Upload shouldn't be jsonfied
      options[:body] = dump_json ? Oj.dump(body, mode: :compat) : body
    end
  end

  options.merge!(disable_ssl) unless settings[:ssl_verify]

  Typhoeus::Request.new(url(path), options).run
end
url(path) click to toggle source

Pagination Helper, Raw/Full HTTP Requests

# File lib/labclient/http.rb, line 12
def url(path)
  if path.include? 'http'
    path
  else
    "#{settings[:url]}/api/v4/#{path}"
  end
end