Class: Connfu::Provisioning::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/connfu/provisioning/base.rb

Overview

This class is the responsible of any HTTP request to connFu endpoint

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, endpoint = nil) ⇒ Base

Initializer

Parameters

  • api_key valid api_key that authenticates the application

  • endpoint Connfu 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

.endpointObject

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_keyObject

valid api_key that authenticates the application



12
13
14
# File 'lib/connfu/provisioning/base.rb', line 12

def api_key
  @api_key
end

#endpointObject

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