Class: Cabal::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
# File 'lib/cabal/client.rb', line 8

def initialize(options = {})
  @api_base = "#{options[:api_base]}/v2"
  @access_key = options[:access_key]
  @secret_key = options[:secret_key]
  @connector = Faraday.new(url: api_base) do |faraday|
    faraday.request  :url_encoded
    faraday.adapter  Faraday.default_adapter
  end
end

Instance Attribute Details

#api_baseObject (readonly)

Returns the value of attribute api_base.



6
7
8
# File 'lib/cabal/client.rb', line 6

def api_base
  @api_base
end

Instance Method Details

#get(endpoint, authenticated = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cabal/client.rb', line 26

def get(endpoint, authenticated = nil)
    response = connector.get {|req|
      req.url endpoint
      req.headers['Content-Type'] = 'application/json'
      req.headers['Accept'] = 'application/json'
      req.headers['Authorization'] = authorization
    }
    case response.status
    when 200
      Oj.load(response.body)
    when 404
      raise "Cabal::Client::NotFoundError"
    when 401
      raise "Cabal::Client::UnauthorizedError"
    end
end

#private_key(cluster_name) ⇒ Object



18
19
20
# File 'lib/cabal/client.rb', line 18

def private_key(cluster_name)
  get("private-key/#{URI.encode(cluster_name)}")['private_ssh_key']
end

#public_key(cluster_name) ⇒ Object



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

def public_key(cluster_name)
  get("key/#{URI.encode(cluster_name)}")['public_ssh_key']
end