Class: LabClient::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/labclient/http.rb

Overview

Request Helper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ HTTP

Returns a new instance of HTTP.



7
8
9
# File 'lib/labclient/http.rb', line 7

def initialize(settings)
  self.settings = settings
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/labclient/http.rb', line 5

def settings
  @settings
end

Instance Method Details

#disable_sslObject



38
39
40
# File 'lib/labclient/http.rb', line 38

def disable_ssl
  { ssl_verifyhost: 0, ssl_verifypeer: false }
end

#headers(dump_json) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object

Pagination Helper, Raw/Full HTTP Requests



12
13
14
15
16
17
18
# File 'lib/labclient/http.rb', line 12

def url(path)
  if path.include? 'http'
    path
  else
    "#{settings[:url]}/api/v4/#{path}"
  end
end