Class: SuperSettings::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/super_settings/http_client.rb

Overview

This is a simple HTTP client that is used to communicate with the REST API. It will keep the connection alive and reuse it on subsequent requests.

Defined Under Namespace

Classes: Error, InvalidRecordError, NotFoundError

Constant Summary collapse

DEFAULT_HEADERS =
{"Accept" => "application/json"}.freeze
DEFAULT_TIMEOUT =
5.0
KEEP_ALIVE_TIMEOUT =
60

Instance Method Summary collapse

Constructor Details

#initialize(base_url, headers: nil, params: nil, timeout: nil, user: nil, password: nil) ⇒ HttpClient

Returns a new instance of HttpClient.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/super_settings/http_client.rb', line 29

def initialize(base_url, headers: nil, params: nil, timeout: nil, user: nil, password: nil)
  base_url = "#{base_url}/" unless base_url.end_with?("/")
  @base_uri = URI(base_url)
  @base_uri.query = query_string(params) if params
  @headers = headers ? DEFAULT_HEADERS.merge(headers) : DEFAULT_HEADERS
  @timeout = timeout || DEFAULT_TIMEOUT
  @user = user
  @password = password
  @mutex = Mutex.new
  @connections = []
end

Instance Method Details

#get(path, params = nil) ⇒ Object



41
42
43
44
# File 'lib/super_settings/http_client.rb', line 41

def get(path, params = nil)
  request = Net::HTTP::Get.new(request_uri(path, params))
  send_request(request)
end

#post(path, params = nil) ⇒ Object



46
47
48
49
50
# File 'lib/super_settings/http_client.rb', line 46

def post(path, params = nil)
  request = Net::HTTP::Post.new(request_uri(path))
  request.body = JSON.dump(params) if params
  send_request(request)
end