discourse/script/import_scripts/quandora/quandora_api.rb

54 lines
1.1 KiB
Ruby
Raw Normal View History

2016-01-27 01:51:23 -05:00
require 'base64'
require 'json'
class QuandoraApi
attr_accessor :domain, :username, :password
2017-07-27 21:20:09 -04:00
def initialize(domain, username, password)
2016-01-27 01:51:23 -05:00
@domain = domain
@username = username
@password = password
end
2017-07-27 21:20:09 -04:00
def base_url(domain)
2016-01-27 01:51:23 -05:00
"https://#{domain}.quandora.com/m/json"
end
2017-07-27 21:20:09 -04:00
def auth_header(username, password)
2016-01-27 01:51:23 -05:00
encoded = Base64.encode64 "#{username}:#{password}"
2017-07-27 21:20:09 -04:00
{ Authorization: "Basic #{encoded.strip!}" }
2016-01-27 01:51:23 -05:00
end
def list_bases_url
"#{base_url @domain}/kb"
end
2017-07-27 21:20:09 -04:00
def list_questions_url(kb_id, limit)
2016-01-27 01:51:23 -05:00
url = "#{base_url @domain}/kb/#{kb_id}/list"
url = "#{url}?l=#{limit}" if limit
url
end
def request(url)
JSON.parse(Excon.get(url, headers: auth_header(@username, @password)))
2016-01-27 01:51:23 -05:00
end
2016-01-27 01:51:23 -05:00
def list_bases
response = request list_bases_url
response['data']
end
2017-07-27 21:20:09 -04:00
def list_questions(kb_id, limit = nil)
2016-01-27 01:51:23 -05:00
url = list_questions_url(kb_id, limit)
response = request url
response['data']['result']
end
2017-07-27 21:20:09 -04:00
def get_question(question_id)
2016-01-27 01:51:23 -05:00
url = "#{base_url @domain}/q/#{question_id}"
response = request url
response['data']
end
end