Class: CLX::HTTPAdapter
- Inherits:
-
Object
- Object
- CLX::HTTPAdapter
- Defined in:
- lib/clx_api/http_adapter.rb
Overview
HTTP Adapter class for making RESTful HTTP-requests
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Contains the password used for Basic Auth requests.
-
#username ⇒ Object
readonly
Contains the username used for Basic Auth requests.
Instance Method Summary collapse
-
#create_http(uri) ⇒ Net::HTTP
private
Creates a Net:HTTP instance with ssl activated with URI-object.
-
#create_request(method, uri, data = nil) ⇒ Mixed
private
Create Request object by method and uri.
-
#delete(uri) ⇒ HTTP::Response
DELETE-request (Not implemented).
-
#execute(method, uri, data = nil) ⇒ HTTP::Response
private
Execute request.
-
#get(uri) ⇒ HTTP::Response
GET-request.
-
#post(uri, data) ⇒ HTTP::Response
POST-request (Not implemented).
-
#put(uri, data) ⇒ HTTP::Response
PUT-request (Not implemented).
-
#set_auth(username, password) ⇒ Object
Set credentials for basic auth.
Instance Attribute Details
#password ⇒ Object (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 |
#username ⇒ Object (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
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
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)
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
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
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)
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)
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
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 |