Class: Joyent::Connection
- Inherits:
-
Object
- Object
- Joyent::Connection
- Defined in:
- lib/joyent/connection.rb
Instance Method Summary collapse
- #datacenters ⇒ Object
- #datasets ⇒ Object
- #execute(http_method, url, data = {}) ⇒ Object
- #headers ⇒ Object
-
#initialize(username, password, datacenter = Datacenters.default) ⇒ Connection
constructor
A new instance of Connection.
- #keys ⇒ Object
- #machines ⇒ Object
- #packages ⇒ Object
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
#datacenters ⇒ Object
50 51 52 |
# File 'lib/joyent/connection.rb', line 50 def datacenters @datacenters ||= Joyent::Datacenters.new(self) end |
#datasets ⇒ Object
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.} - #{response.body}" end end |
#headers ⇒ Object
12 13 14 15 16 |
# File 'lib/joyent/connection.rb', line 12 def headers { "X-Api-Version" => Joyent::API_VERSION } end |
#keys ⇒ Object
46 47 48 |
# File 'lib/joyent/connection.rb', line 46 def keys @keys ||= Joyent::Keys.new(self) end |