Created
October 25, 2014 05:09
-
-
Save skierkowski/2259b701be509ba12a6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rest-client' | |
require 'json' | |
## usage: | |
# Tutum.configure do |c| | |
# c.username = '' | |
# c.api_key = '' | |
# end | |
# | |
# c = Tutum::Client.new | |
module Tutum | |
class << self | |
attr_accessor :configuration | |
end | |
def self.configure | |
self.configuration ||= Configuration.new | |
yield(configuration) | |
end | |
class Client | |
def service_logs(service_id) | |
get(['service',service_id,'logs'])['logs'] | |
end | |
def service_state(service_id) | |
get(['service',service_id])['state'] | |
end | |
def service(service_id) | |
get ['service',service_id] | |
end | |
def service_create(params={}) | |
post ['service'], params | |
end | |
def service_destroy(service_id) | |
delete ['service', service_id] | |
end | |
def service_start(service_id) | |
post ['service',service_id,'start'] | |
end | |
def service_stop(service_id) | |
post ['service',service_id,'stop'] | |
end | |
private | |
def get(path=[]) | |
JSON.parse(RestClient.get(url(path),headers)) | |
end | |
def post(path=[], content={}) | |
JSON.parse(RestClient.post(url(path),content.to_json,headers)) | |
end | |
def delete(path=[]) | |
JSON.parse(RestClient.post(url(path),headers)) | |
end | |
def put(path=[], content={}) | |
JSON.parse(RestClient.put(url(path),content.to_json,headers)) | |
end | |
def url(path=[]) | |
"https://dashboard.tutum.co/api/v1/#{File.join(path + [''])}" | |
end | |
def headers | |
{ | |
'Authorization'=>"ApiKey #{Tutum.configuration.username}:#{Tutum.configuration.api_key}", | |
'Accept' => 'application/json', | |
'Content-Type' => 'application/json' | |
} | |
end | |
end | |
class Configuration | |
attr_accessor :username, :api_key | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment