Class: HttpConnect::BasicRestClient

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

Overview

BasicRestClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ BasicRestClient

initialize()



29
30
31
32
33
34
35
# File 'lib/http_connect.rb', line 29

def initialize(base_url)
  @base_url = base_url
  @custom_headers = {}
  @read_write_timeout = 8000
  @connection_timeout = 2000
  @use_ssl = false
end

Instance Attribute Details

#base_urlObject (readonly)

base url of the http request



22
23
24
# File 'lib/http_connect.rb', line 22

def base_url
  @base_url
end

#connection_timeoutObject

connection timeout



25
26
27
# File 'lib/http_connect.rb', line 25

def connection_timeout
  @connection_timeout
end

#custom_headersObject

custom http headers to add to the request



23
24
25
# File 'lib/http_connect.rb', line 23

def custom_headers
  @custom_headers
end

#read_write_timeoutObject

read and write http request timeout



24
25
26
# File 'lib/http_connect.rb', line 24

def read_write_timeout
  @read_write_timeout
end

#use_sslObject

helps to set the request scheme to https



26
27
28
# File 'lib/http_connect.rb', line 26

def use_ssl
  @use_ssl
end

Instance Method Details

#delete(path, accept, content = {}) ⇒ Object

delete(). It helps send an HTTP DELETE request to delete the specified resource

Parameters:

  • path

    the resource path

  • content_type

    the http request content type

  • accept

    the expected mime type of the response

  • content (defaults to: {})

    the http request payload



98
99
100
# File 'lib/http_connect.rb', line 98

def delete(path, accept, content = {})
  execute HttpConnect::HttpDelete.new(path, nil, accept, content)
end

#execute(http_webrequest) ⇒ Object

execute(). It helps execute an Http request one can use the following object:

- HttpPost
- HttpGet
- HttpDelete
- HttpPut

Parameters:

  • http_webrequest

    the HttpRequest object

Returns:

  • HttpResponse object or an Http Error object



53
54
55
56
57
58
59
60
# File 'lib/http_connect.rb', line 53

def execute(http_webrequest)
  (http_webrequest.is_a? HttpRequest) ?
      do_http_request(http_webrequest.path,
                      http_webrequest.http_method,
                      http_webrequest.content_type,
                      http_webrequest.accept, http_webrequest.content) :
      raise('http_webrequest is not a subclass of HttpRequest')
end

#get(path, accept, content = {}) ⇒ Object

get(). It helps send an HTTP GET request to fetch the specified resource

Parameters:

  • path

    the resource path

  • content_type

    the http request content type

  • accept

    the expected mime type of the response

  • content (defaults to: {})

    the http request payload



88
89
90
# File 'lib/http_connect.rb', line 88

def get(path, accept, content = {})
  execute HttpConnect::HttpGet.new(path, nil, accept, content)
end

#post(path, content_type, accept, content = {}) ⇒ Object

post(). It helps send an HTTP POST request to create the specified resource

Parameters:

  • path

    the resource path

  • content_type

    the http request content type

  • accept

    the expected mime type of the response

  • content (defaults to: {})

    the http request payload



68
69
70
# File 'lib/http_connect.rb', line 68

def post(path, content_type, accept, content = {})
  execute HttpConnect::HttpPost.new(path, content_type, accept, content)
end

#put(path, content_type, accept, content = {}) ⇒ Object

put(). It helps send an HTTP PUT request to create or modified the specified resource

Parameters:

  • path

    the resource path

  • content_type

    the http request content type

  • accept

    the expected mime type of the response

  • content (defaults to: {})

    the http request payload



78
79
80
# File 'lib/http_connect.rb', line 78

def put(path, content_type, accept, content = {})
  execute HttpConnect::HttpPut.new(path, content_type, accept, content)
end

#set_basic_auth(username, password) ⇒ Object

set_basic_auth()

Parameters:

  • username

    the username credential

  • password

    the password credential



40
41
42
43
# File 'lib/http_connect.rb', line 40

def set_basic_auth(username, password)
  @custom_headers['Authorization'] =
      "Basic #{Base64.encode64(username + ':' + password)}".chomp
end