Class: Connfu::Provisioning::Base
- Inherits:
-
Object
- Object
- Connfu::Provisioning::Base
- Defined in:
- lib/connfu/provisioning/base.rb
Overview
This class is the responsible of any HTTP request to connFu endpoint
Class Attribute Summary collapse
-
.endpoint ⇒ Object
Enable to configure just once the endpoint.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
valid api_key that authenticates the application.
-
#endpoint ⇒ Object
Connfu endpoint (host:port).
Instance Method Summary collapse
-
#delete(path, params = {}, headers = {}) ⇒ Object
HTTP DELETE request.
-
#get(path, params = {}, headers = {}) ⇒ Object
HTTP GET request.
-
#initialize(api_key, endpoint = nil) ⇒ Base
constructor
Initializer ==== Parameters *
api_keyvalid api_key that authenticates the application *endpointConnfu endpoint (host:port). -
#post(path, body = {}, headers = {}) ⇒ Object
HTTP POST request.
-
#put(path, body = {}, headers = {}) ⇒ Object
HTTP PUT request.
Constructor Details
#initialize(api_key, endpoint = nil) ⇒ Base
Initializer
Parameters
-
api_keyvalid api_key that authenticates the application -
endpointConnfu endpoint (host:port)
23 24 25 26 |
# File 'lib/connfu/provisioning/base.rb', line 23 def initialize(api_key, endpoint = nil) @api_key = api_key @endpoint = endpoint.nil? ? Base.endpoint : endpoint # if no endpoint retrieved, try to use the class one end |
Class Attribute Details
.endpoint ⇒ Object
Enable to configure just once the endpoint
78 79 80 |
# File 'lib/connfu/provisioning/base.rb', line 78 def endpoint @endpoint end |
Instance Attribute Details
#api_key ⇒ Object
valid api_key that authenticates the application
12 13 14 |
# File 'lib/connfu/provisioning/base.rb', line 12 def api_key @api_key end |
#endpoint ⇒ Object
Connfu endpoint (host:port)
15 16 17 |
# File 'lib/connfu/provisioning/base.rb', line 15 def endpoint @endpoint end |
Instance Method Details
#delete(path, params = {}, headers = {}) ⇒ Object
HTTP DELETE request
71 72 73 74 |
# File 'lib/connfu/provisioning/base.rb', line 71 def delete(path, params = {}, headers = {}) headers.merge!(base_headers).merge!({:content_type => :json, :Accept => "application/json"}) RestClient.delete("#{@endpoint}/#{path}?".concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&")), headers) end |
#get(path, params = {}, headers = {}) ⇒ Object
HTTP GET request
31 32 33 34 |
# File 'lib/connfu/provisioning/base.rb', line 31 def get(path, params = {}, headers = {}) headers.merge!(base_headers).merge!({:Accept => "application/json"}) RestClient.get("#{@endpoint}/#{path}?".concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&")), headers) end |
#post(path, body = {}, headers = {}) ⇒ Object
HTTP POST request
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/connfu/provisioning/base.rb', line 39 def post(path, body = {}, headers = {}) headers.merge!(base_headers).merge!({:content_type => :json, :Accept => "application/json"}) RestClient.post("#{@endpoint}/#{path}", ActiveSupport::JSON.encode(body), headers) { |response, request, result| case response.code when 200..201 # else, do the normal stuff if block_given? response.return!(request, result, &Proc.new) else response.return!(request, result) end else if block_given? response.return!(request, result, &Proc.new) else response.return!(request, result) end end } end |
#put(path, body = {}, headers = {}) ⇒ Object
HTTP PUT request
63 64 65 66 |
# File 'lib/connfu/provisioning/base.rb', line 63 def put(path, body = {}, headers = {}) headers.merge!(base_headers).merge!({:content_type => :json, :Accept => "application/json"}) RestClient.put("#{@endpoint}/#{path}", ActiveSupport::JSON.encode(body), headers) end |