class Pandorabots::API

Constants

BASE_URL
FILE_KIND

Public Class Methods

compile_bot(username, botname, user_key:) click to toggle source
# File lib/pandorabots.rb, line 40
def compile_bot(username, botname, user_key:)
  request_uri = "/bot/#{username}/#{botname}/verify?user_key=#{user_key}"
  get = Net::HTTP::Get.new(URI.escape(request_uri))
  response = https.request(get)
  succeed_compilation?(response)
end
create_bot(username, botname, user_key:) click to toggle source
# File lib/pandorabots.rb, line 14
def create_bot(username, botname, user_key:)
  request_uri = "/bot/#{username}/#{botname}?user_key=#{user_key}"
  put = Net::HTTP::Put.new(URI.escape(request_uri))
  response = https.request(put)
  succeed_creation?(response)
end
delete_bot(username, botname, user_key:) click to toggle source
# File lib/pandorabots.rb, line 21
def delete_bot(username, botname, user_key:)
  request_uri = "/bot/#{username}/#{botname}?user_key=#{user_key}"
  delete = Net::HTTP::Delete.new(URI.escape(request_uri))
  response = https.request(delete)
  succeed_deletion?(response)
end
talk(username, botname, input, sessionid: '', reset: false, trace: false, recent: true, user_key:) click to toggle source
# File lib/pandorabots.rb, line 47
def talk(username, botname, input, sessionid: '', reset: false,
         trace: false, recent: true, user_key:)
  request_uri = "/talk/#{username}/#{botname}?input=#{input}" \
                "&sessionid=#{sessionid}&reset=#{reset}&trace=#{trace}" \
                "&user_key=#{user_key}"
  post = Net::HTTP::Post.new(URI.escape(request_uri))
  response = https.request(post)
  TalkResult.new(response.body) if succeed_talk?(response)
end
upload_file(username, botname, file, file_kind: '', filename: '', user_key:) click to toggle source
# File lib/pandorabots.rb, line 28
def upload_file(username, botname, file,
                file_kind: '', filename: '', user_key:)
  file_kind = file_kind(file) if file_kind.empty?
  filename = filename(file) if filename.empty?
  request_uri = upload_file_uri(username, botname, file_kind,
                                filename, user_key)
  put = Net::HTTP::Put.new(URI.escape(request_uri))
  put.body = file.read
  response = https.request(put)
  succeed_upload?(response)
end

Private Class Methods

file_kind(file) click to toggle source
# File lib/pandorabots.rb, line 70
def file_kind(file)
  extname = File.extname(file).delete('.')
  FILE_KIND[extname.to_sym]
end
filename(file) click to toggle source
# File lib/pandorabots.rb, line 66
def filename(file)
  File.basename(file, '.*')
end
https() click to toggle source
# File lib/pandorabots.rb, line 59
def https
  uri = URI(BASE_URL)
  https = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true
  https
end
need_filename?(file_kind) click to toggle source
# File lib/pandorabots.rb, line 81
def need_filename?(file_kind)
  not ['properties', 'pdefaults'].include?(file_kind)
end
raise_error_400(body) click to toggle source
# File lib/pandorabots.rb, line 131
def raise_error_400(body)
  json = JSON.parse(body)
  case json['message']
  when 'Invalid botname' then raise InvalidBotnameError, body
  when 'precondition failed' then raise PreconditionError, body
  when 'Compile errors encountered' then raise CompilationError, body
  else raise Error, body
  end
end
succeed_compilation?(response) click to toggle source
# File lib/pandorabots.rb, line 113
def succeed_compilation?(response)
  case response.code
  when '200' then true
  when '400' then raise_error_400(response.body)
  when '401' then raise AuthorizationError, response.body
  else raise Error, response.body
  end
end
succeed_creation?(response) click to toggle source
# File lib/pandorabots.rb, line 85
def succeed_creation?(response)
  case response.code
  when '200' then true
  when '400' then raise_error_400(response.body)
  when '401' then raise AuthorizationError, response.body
  when '409' then raise BotExistsError, response.body
  else raise Error, response.body
  end
end
succeed_deletion?(response) click to toggle source
# File lib/pandorabots.rb, line 95
def succeed_deletion?(response)
  case response.code
  when '200' then true
  when '400' then raise_error_400(response.body)
  when '401' then raise AuthorizationError, response.body
  else raise Error, response.body
  end
end
succeed_talk?(response) click to toggle source
# File lib/pandorabots.rb, line 122
def succeed_talk?(response)
  case response.code
  when '200' then true
  when '400' then raise_error_400(response.body)
  when '401' then raise AuthorizationError, response.body
  else raise Error, response.body
  end
end
succeed_upload?(response) click to toggle source
# File lib/pandorabots.rb, line 104
def succeed_upload?(response)
  case response.code
  when '200' then true
  when '400' then raise_error_400(response.body)
  when '401' then raise AuthorizationError, response.body
  else raise Error, response.body
  end
end
upload_file_uri(username, botname, file_kind, filename, user_key) click to toggle source
# File lib/pandorabots.rb, line 75
def upload_file_uri(username, botname, file_kind, filename, user_key)
  request_uri = "/bot/#{username}/#{botname}/#{file_kind}"
  request_uri << "/#{filename}" if need_filename?(file_kind)
  request_uri << "?user_key=#{user_key}"
end