class Turbot::API

Constants

VERSION

Public Class Methods

new(params) click to toggle source
# File lib/turbot/api.rb, line 10
def initialize(params)
  @host = params[:host]
  @port = params[:port]
  @scheme = params[:scheme]
  @api_key = params[:api_key] || get_api_key_for_credentials(params[:username], params[:password])['api_key']
end

Public Instance Methods

create_bot(bot_id, config, env = nil) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 52
def create_bot(bot_id, config, env = nil)
  request(:post, '/api/bots', :bot => {:bot_id => bot_id, :config => config, :env => env})
end
create_draft_data(bot_id, batch) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 67
def create_draft_data(bot_id, batch)
  request(:post, "/api/bots/#{bot_id}/draft_data", :batch => batch)
end
destroy_draft_data(bot_id) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 72
def destroy_draft_data(bot_id)
  request(:delete, "/api/bots/#{bot_id}/draft_data")
end
get_api_key() click to toggle source

@return [String] the user's API key

# File lib/turbot/api.rb, line 26
def get_api_key
  get_user['api_key']
end
get_api_key_for_credentials(user, password) click to toggle source

@return [Hash] a hash with a single key “api_key”

# File lib/turbot/api.rb, line 31
def get_api_key_for_credentials(user, password)
  response = request(:get, '/api/user/api_key', {
    :email => user,
    :password => password,
  })

  # For backwards compatibility, this method must return a Hash, not a SuccessResponse.
  JSON.load(response.body)
end
get_user() click to toggle source

@return [Hash] a hash with the user's details

# File lib/turbot/api.rb, line 18
def get_user
  response = request(:get, '/api/user')

  # For backwards compatibility, this method must return a Hash, not a SuccessResponse.
  JSON.load(response.body)
end
list_bots() click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 42
def list_bots
  request(:get, '/api/bots')
end
show_bot(bot_id) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 47
def show_bot(bot_id)
  request(:get, "/api/bots/#{bot_id}")
end
show_manifest(bot_id) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 62
def show_manifest(bot_id)
  request(:get, "/api/bots/#{bot_id}/manifest")
end
start_run(bot_id) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 82
def start_run(bot_id)
  request(:post, "/api/bots/#{bot_id}/run/start")
end
stop_run(bot_id) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 87
def stop_run(bot_id)
  request(:post, "/api/bots/#{bot_id}/run/stop")
end
update_bot(bot_id, config, env = nil) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 57
def update_bot(bot_id, config, env = nil)
  request(:put, "/api/bots/#{bot_id}", :bot => {:config => config, :env => env})
end
update_code(bot_id, archive) click to toggle source

@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]

# File lib/turbot/api.rb, line 77
def update_code(bot_id, archive)
  request(:put, "/api/bots/#{bot_id}/code", {:archive => archive}, false)
end

Private Instance Methods

build_url_and_params(path, params = {}) click to toggle source
# File lib/turbot/api.rb, line 93
def build_url_and_params(path, params = {})
  url = URI::HTTP.build({
    :host => @host,
    :port => @port,
    :scheme => @scheme,
    :path => path.strip,
  }).to_s

  params[:api_key] = @api_key

  [url, params]
end
request(method, path, params = {}, json = true) click to toggle source
# File lib/turbot/api.rb, line 106
def request(method, path, params = {}, json = true)
  url, params = build_url_and_params(path, params)

  begin
    if method == :get || method == :delete
      response = RestClient.send(method, url, :params => params)
    elsif json == false
      response = RestClient.send(method, url, params)
    else
      response = RestClient.send(method, url, JSON.dump(params), :content_type => :json)
    end
    SuccessResponse.new(response)
  rescue RestClient::Exception => e
    FailureResponse.new(e.response)
  end
end