Class: CLX::HTTPAdapter

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

Overview

HTTP Adapter class for making RESTful HTTP-requests

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#passwordObject (readonly)

Contains the password used for Basic Auth requests



14
15
16
# File 'lib/clx_api/http_adapter.rb', line 14

def password
  @password
end

#usernameObject (readonly)

Contains the username used for Basic Auth requests



10
11
12
# File 'lib/clx_api/http_adapter.rb', line 10

def username
  @username
end

Instance Method Details

#create_http(uri) ⇒ Net::HTTP (private)

Creates a Net:HTTP instance with ssl activated with URI-object

Parameters:

  • uri (URI)

Returns:

  • (Net::HTTP)

    http



118
119
120
121
122
123
124
125
# File 'lib/clx_api/http_adapter.rb', line 118

def create_http(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  if uri.instance_of? URI::HTTPS
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  return http
end

#create_request(method, uri, data = nil) ⇒ Mixed (private)

Create Request object by method and uri

Examples:

request = create_request("GET", 'http://some.url')

Parameters:

  • method (String)

    HTTP

  • uri (URI)
  • data (Hash) (defaults to: nil)

Returns:

  • (Mixed)

    request Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete

Raises:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/clx_api/http_adapter.rb', line 96

def create_request(method, uri, data = nil)
  method = method.upcase
  if(method == 'GET')
    return Net::HTTP::Get.new(uri)
  elsif(method == 'POST')
    request = Net::HTTP::Post.new(uri)
    request.body = data.to_json
    return request
  elsif(method == 'PUT')
    request = Net::HTTP::Put.new(uri)
    request.body = data.to_json
    return request
  elsif(method == 'DELETE')
    return Net::HTTP::Delete.new(uri)
  else
    raise CLXException, 'Unknown HTTP method'
  end
end

#delete(uri) ⇒ HTTP::Response

DELETE-request

(Not implemented)

Parameters:

  • uri (URI)

    Ruby URI object with full API URL

Returns:

  • (HTTP::Response)


64
65
66
# File 'lib/clx_api/http_adapter.rb', line 64

def delete(uri)
  return execute('delete', uri)
end

#execute(method, uri, data = nil) ⇒ HTTP::Response (private)

Execute request

Parameters:

  • method (String)
  • uri (String)

    Full API-path

  • data (Hash) (defaults to: nil)

Returns:

  • (HTTP::Response)

    result

Raises:



77
78
79
80
81
82
83
84
# File 'lib/clx_api/http_adapter.rb', line 77

def execute(method, uri, data = nil)
  raise CLXException, 'uri must be instance of URI' unless uri.instance_of?(URI::HTTPS) || uri.instance_of?(URI::HTTP)
  request = create_request(method, uri, data)
  request.basic_auth(@username, @password)
  http = create_http(uri);

  return http.request(request)
end

#get(uri) ⇒ HTTP::Response

GET-request

Parameters:

  • uri (URI)

    Ruby URI object with full API URL

Returns:

  • (HTTP::Response)


33
34
35
# File 'lib/clx_api/http_adapter.rb', line 33

def get(uri)
  return execute('get', uri)
end

#post(uri, data) ⇒ HTTP::Response

POST-request

(Not implemented)

Parameters:

  • uri (URI)

    Ruby URI object with full API URL

  • data (Hash)

    POST-data

Returns:

  • (HTTP::Response)


44
45
46
# File 'lib/clx_api/http_adapter.rb', line 44

def post(uri, data)
  return execute('post', uri, data)
end

#put(uri, data) ⇒ HTTP::Response

PUT-request

(Not implemented)

Parameters:

  • uri (URI)

    Ruby URI object with full API URL

  • data (Hash)

    PUT-data

Returns:

  • (HTTP::Response)


55
56
57
# File 'lib/clx_api/http_adapter.rb', line 55

def put(uri, data)
  return execute('put', uri, data)
end

#set_auth(username, password) ⇒ Object

Set credentials for basic auth

Parameters:

  • username (String)
  • password (String)

Raises:



19
20
21
22
23
24
25
26
27
# File 'lib/clx_api/http_adapter.rb', line 19

def set_auth(username, password)
  raise CLXException, 'Username must be a string' unless username.is_a? String
  raise CLXException, 'Password must be a string' unless password.is_a? String
  raise CLXException, 'Username can not be an empty string' unless username.length > 0
  raise CLXException, 'Password can not be an empty string' unless password.length > 0
  
  @username = username
  @password = password
end