discourse/script/import_scripts/quandora/quandora_api.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-01-27 01:51:23 -05:00
require "base64"
require "json"
class QuandoraApi
attr_accessor :domain, :username, :password
def initialize(domain, username, password)
@domain = domain
@username = username
@password = password
end
def base_url(domain)
"https://#{domain}.quandora.com/m/json"
end
def auth_header(username, password)
encoded = Base64.encode64 "#{username}:#{password}"
{ Authorization: "Basic #{encoded.strip!}" }
end
def list_bases_url
"#{base_url @domain}/kb"
end
def list_questions_url(kb_id, limit)
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
def list_questions(kb_id, limit = nil)
url = list_questions_url(kb_id, limit)
response = request url
response["data"]["result"]
end
def get_question(question_id)
url = "#{base_url @domain}/q/#{question_id}"
response = request url
response["data"]
end
end