Skip to content

Instantly share code, notes, and snippets.

@skierkowski
Created October 25, 2014 05:09
Show Gist options
  • Save skierkowski/2259b701be509ba12a6b to your computer and use it in GitHub Desktop.
Save skierkowski/2259b701be509ba12a6b to your computer and use it in GitHub Desktop.
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