Class: Joyent::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/joyent/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, datacenter = Datacenters.default) ⇒ Connection

Returns a new instance of Connection.



3
4
5
6
7
8
9
10
# File 'lib/joyent/connection.rb', line 3

def initialize(username, password, datacenter = Datacenters.default)
  @http_connection = Net::HTTP.new(datacenter, 443)
  @http_connection.use_ssl = true
  @http_connection.verify_mode = OpenSSL::SSL::VERIFY_NONE

  @username = username
  @password = password
end

Instance Method Details

#datacentersObject



50
51
52
# File 'lib/joyent/connection.rb', line 50

def datacenters
  @datacenters ||= Joyent::Datacenters.new(self)
end

#datasetsObject



54
55
56
# File 'lib/joyent/connection.rb', line 54

def datasets
  @datasets ||= Joyent::Datasets.new(self)
end

#execute(http_method, url, data = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/joyent/connection.rb', line 18

def execute(http_method, url, data = {})
  case http_method
  when :get
    request = Net::HTTP::Get.new("/#{@username}#{url}", self.headers)
  when :post
    request = Net::HTTP::Post.new("/#{@username}#{url}", self.headers)
    request.set_form_data(data)
  when :delete
    request = Net::HTTP::Delete.new("/#{@username}#{url}", self.headers)
  else
    raise "HTTP method #{http_method} not supported"
  end

  request.basic_auth(@username, @password)

  response = @http_connection.request(request)

  if response.code =~ /^2/
    if response.body.nil? or response.body.empty?
      nil
    else
      JSON.parse(response.body)
    end
  else
    raise "#{response.code}: #{response.message} - #{response.body}"
  end
end

#headersObject



12
13
14
15
16
# File 'lib/joyent/connection.rb', line 12

def headers
  {
    "X-Api-Version" => Joyent::API_VERSION
  }
end

#keysObject



46
47
48
# File 'lib/joyent/connection.rb', line 46

def keys
  @keys ||= Joyent::Keys.new(self)
end

#machinesObject



62
63
64
# File 'lib/joyent/connection.rb', line 62

def machines
  @machines ||= Joyent::Machines.new(self)
end

#packagesObject



58
59
60
# File 'lib/joyent/connection.rb', line 58

def packages
  @packages ||= Joyent::Packages.new(self)
end