Class: Rancher::Shell::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/rancher/shell/api.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  user: nil,
  pass: nil,
  host: 'rancher.example.com',
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Api

Returns a new instance of Api.



14
15
16
# File 'lib/rancher/shell/api.rb', line 14

def initialize options = {}
  @options = DEFAULT_OPTIONS.merge options
end

Instance Method Details

#get(resource, data = nil, headers = {}) ⇒ Object



18
19
20
# File 'lib/rancher/shell/api.rb', line 18

def get resource, data = nil, headers = {}
  request :get, resource, nil, headers
end

#post(resource, data, headers = {}) ⇒ Object



22
23
24
# File 'lib/rancher/shell/api.rb', line 22

def post resource, data, headers = {}
  request :post, resource, data, headers
end

#request(method_name, resource, data, headers) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rancher/shell/api.rb', line 26

def request method_name, resource, data, headers
  uri = URI "https://#{@options[:host]}/v1/#{resource}"
  Net::HTTP.start uri.host, uri.port, use_ssl: true do |http|
    method_class_name = "Net::HTTP::#{method_name.to_s.split('_').map(&:capitalize).join}"
    method_class = Object.const_get method_class_name
    request = method_class.new uri
    request.basic_auth @options[:user], @options[:pass]
    request.set_form_data data if method_name === :post
    ApiResponse.new http.request request
  end
end