class Marathon::Client

Attributes

host[RW]

Public Class Methods

new(host) click to toggle source
# File lib/marathon/client.rb, line 7
def initialize(host)
  @host = host
  @uri = URI(host)

  @http = Net::HTTP.new(@uri.host, @uri.port)
  @http.use_ssl = @uri.is_a?(URI::HTTPS)
  @http.start # for keep-alive
end

Public Instance Methods

create_app(definition) click to toggle source
# File lib/marathon/client.rb, line 16
def create_app(definition)
  post('/v2/apps', definition)
end
delete_app(id) click to toggle source
# File lib/marathon/client.rb, line 24
def delete_app(id)
  delete(app_path(id))
end
get_app(id) click to toggle source
# File lib/marathon/client.rb, line 28
def get_app(id)
  get(app_path(id))
end
update_app(id, definition) click to toggle source
# File lib/marathon/client.rb, line 20
def update_app(id, definition)
  put(app_path(id), definition)
end

Private Instance Methods

app_path(id) click to toggle source
# File lib/marathon/client.rb, line 60
def app_path(id)
  # this covers the case when id does not start with /
  File.join('/v2/apps', id)
end
delete(path) click to toggle source
# File lib/marathon/client.rb, line 48
def delete(path)
  request Net::HTTP::Delete.new(path)
end
get(path) click to toggle source
# File lib/marathon/client.rb, line 56
def get(path)
  request Net::HTTP::Get.new(path)
end
handle_response(response) click to toggle source
# File lib/marathon/client.rb, line 80
def handle_response(response)
  case response
    when Net::HTTPSuccess
      parse_response(response)
    else
      raise InvalidResponseError, response
  end
end
parse_response(response) click to toggle source
# File lib/marathon/client.rb, line 89
def parse_response(response)
  case body = response.body
    when String then JSON.parse(body)
  end
end
post(path, body) click to toggle source
# File lib/marathon/client.rb, line 44
def post(path, body)
  request Net::HTTP::Post.new(path), body
end
put(path, body) click to toggle source
# File lib/marathon/client.rb, line 52
def put(path, body)
  request Net::HTTP::Put.new(path), body
end
request(req, body = nil) click to toggle source
# File lib/marathon/client.rb, line 65
def request(req, body = nil)
  req.body = case body
               when String then body
               else JSON.generate(body)
             end if req.request_body_permitted?

  req.content_type = 'application/json'

  if @uri.user || @uri.password
    req.basic_auth(@uri.user, @uri.password)
  end

  handle_response @http.request(req)
end