class Turbot::API
Constants
- VERSION
Public Class Methods
# 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
@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
@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
@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
@return [String] the user's API
key
# File lib/turbot/api.rb, line 26 def get_api_key get_user['api_key'] end
@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
@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
@return [Turbot::API::SuccessResponse, Turbot::API::FailureResponse]
# File lib/turbot/api.rb, line 42 def list_bots request(:get, '/api/bots') end
@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
@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
@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
@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
@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
@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
# 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
# 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