DEV: Apply syntax_tree formatting to script/*

This commit is contained in:
David Taylor
2023-01-07 11:53:14 +00:00
parent ff508d1ae5
commit 436b3b392b
143 changed files with 8905 additions and 7353 deletions

View File

@ -1,25 +1,25 @@
# frozen_string_literal: true
require 'yaml'
require_relative 'quandora_api'
require "yaml"
require_relative "quandora_api"
def load_config(file)
config = YAML::load_file(File.join(__dir__, file))
@domain = config['domain']
@username = config['username']
@password = config['password']
config = YAML.load_file(File.join(__dir__, file))
@domain = config["domain"]
@username = config["username"]
@password = config["password"]
end
def export
api = QuandoraApi.new @domain, @username, @password
bases = api.list_bases
bases.each do |base|
question_list = api.list_questions base['objectId'], 1000
question_list = api.list_questions base["objectId"], 1000
question_list.each do |q|
question_id = q['uid']
question_id = q["uid"]
question = api.get_question question_id
File.open("output/#{question_id}.json", 'w') do |f|
puts question['title']
File.open("output/#{question_id}.json", "w") do |f|
puts question["title"]
f.write question.to_json
f.close
end

View File

@ -1,10 +1,9 @@
# frozen_string_literal: true
require_relative './quandora_question.rb'
require_relative "./quandora_question.rb"
require File.expand_path(File.dirname(__FILE__) + "/../base.rb")
class ImportScripts::Quandora < ImportScripts::Base
JSON_FILES_DIR = "output"
def initialize
@ -12,8 +11,8 @@ class ImportScripts::Quandora < ImportScripts::Base
@system_user = Discourse.system_user
@questions = []
Dir.foreach(JSON_FILES_DIR) do |filename|
next if filename == ('.') || filename == ('..')
question = File.read JSON_FILES_DIR + '/' + filename
next if filename == (".") || filename == ("..")
question = File.read JSON_FILES_DIR + "/" + filename
@questions << question
end
end
@ -33,9 +32,7 @@ class ImportScripts::Quandora < ImportScripts::Base
q = QuandoraQuestion.new question
import_users q.users
created_topic = import_topic q.topic
if created_topic
import_posts q.replies, created_topic.topic_id
end
import_posts q.replies, created_topic.topic_id if created_topic
topics += 1
print_status topics, total
end
@ -43,9 +40,7 @@ class ImportScripts::Quandora < ImportScripts::Base
end
def import_users(users)
users.each do |user|
create_user user, user[:id]
end
users.each { |user| create_user user, user[:id] }
end
def import_topic(topic)
@ -54,7 +49,7 @@ class ImportScripts::Quandora < ImportScripts::Base
post = Post.find(post_id) # already imported this topic
else
topic[:user_id] = user_id_from_imported_user_id(topic[:author_id]) || -1
topic[:category] = 'quandora-import'
topic[:category] = "quandora-import"
post = create_post(topic, topic[:id])
@ -68,9 +63,7 @@ class ImportScripts::Quandora < ImportScripts::Base
end
def import_posts(posts, topic_id)
posts.each do |post|
import_post post, topic_id
end
posts.each { |post| import_post post, topic_id }
end
def import_post(post, topic_id)
@ -91,6 +84,4 @@ class ImportScripts::Quandora < ImportScripts::Base
end
end
if __FILE__ == $0
ImportScripts::Quandora.new.perform
end
ImportScripts::Quandora.new.perform if __FILE__ == $0

View File

@ -1,10 +1,9 @@
# frozen_string_literal: true
require 'base64'
require 'json'
require "base64"
require "json"
class QuandoraApi
attr_accessor :domain, :username, :password
def initialize(domain, username, password)
@ -38,18 +37,18 @@ class QuandoraApi
def list_bases
response = request list_bases_url
response['data']
response["data"]
end
def list_questions(kb_id, limit = nil)
url = list_questions_url(kb_id, limit)
response = request url
response['data']['result']
response["data"]["result"]
end
def get_question(question_id)
url = "#{base_url @domain}/q/#{question_id}"
response = request url
response['data']
response["data"]
end
end

View File

@ -1,28 +1,27 @@
# frozen_string_literal: true
require 'json'
require 'cgi'
require 'time'
require "json"
require "cgi"
require "time"
class QuandoraQuestion
def initialize(question_json)
@question = JSON.parse question_json
end
def topic
topic = {}
topic[:id] = @question['uid']
topic[:author_id] = @question['author']['uid']
topic[:title] = unescape @question['title']
topic[:raw] = unescape @question['content']
topic[:created_at] = Time.parse @question['created']
topic[:id] = @question["uid"]
topic[:author_id] = @question["author"]["uid"]
topic[:title] = unescape @question["title"]
topic[:raw] = unescape @question["content"]
topic[:created_at] = Time.parse @question["created"]
topic
end
def users
users = {}
user = user_from_author @question['author']
user = user_from_author @question["author"]
users[user[:id]] = user
replies.each do |reply|
user = user_from_author reply[:author]
@ -32,12 +31,12 @@ class QuandoraQuestion
end
def user_from_author(author)
email = author['email']
email = "#{author['uid']}@noemail.com" unless email
email = author["email"]
email = "#{author["uid"]}@noemail.com" unless email
user = {}
user[:id] = author['uid']
user[:name] = "#{author['firstName']} #{author['lastName']}"
user[:id] = author["uid"]
user[:name] = "#{author["firstName"]} #{author["lastName"]}"
user[:email] = email
user[:staged] = true
user
@ -45,26 +44,20 @@ class QuandoraQuestion
def replies
posts = []
answers = @question['answersList']
comments = @question['comments']
comments.each_with_index do |comment, i|
posts << post_from_comment(comment, i, @question)
end
answers = @question["answersList"]
comments = @question["comments"]
comments.each_with_index { |comment, i| posts << post_from_comment(comment, i, @question) }
answers.each do |answer|
posts << post_from_answer(answer)
comments = answer['comments']
comments.each_with_index do |comment, i|
posts << post_from_comment(comment, i, answer)
end
comments = answer["comments"]
comments.each_with_index { |comment, i| posts << post_from_comment(comment, i, answer) }
end
order_replies posts
end
def order_replies(posts)
posts = posts.sort_by { |p| p[:created_at] }
posts.each_with_index do |p, i|
p[:post_number] = i + 2
end
posts.each_with_index { |p, i| p[:post_number] = i + 2 }
posts.each do |p|
parent = posts.select { |pp| pp[:id] == p[:parent_id] }
p[:reply_to_post_number] = parent[0][:post_number] if parent.size > 0
@ -74,35 +67,35 @@ class QuandoraQuestion
def post_from_answer(answer)
post = {}
post[:id] = answer['uid']
post[:parent_id] = @question['uid']
post[:author] = answer['author']
post[:author_id] = answer['author']['uid']
post[:raw] = unescape answer['content']
post[:created_at] = Time.parse answer['created']
post[:id] = answer["uid"]
post[:parent_id] = @question["uid"]
post[:author] = answer["author"]
post[:author_id] = answer["author"]["uid"]
post[:raw] = unescape answer["content"]
post[:created_at] = Time.parse answer["created"]
post
end
def post_from_comment(comment, index, parent)
if comment['created']
created_at = Time.parse comment['created']
if comment["created"]
created_at = Time.parse comment["created"]
else
created_at = Time.parse parent['created']
created_at = Time.parse parent["created"]
end
parent_id = parent['uid']
parent_id = "#{parent['uid']}-#{index - 1}" if index > 0
parent_id = parent["uid"]
parent_id = "#{parent["uid"]}-#{index - 1}" if index > 0
post = {}
id = "#{parent['uid']}-#{index}"
id = "#{parent["uid"]}-#{index}"
post[:id] = id
post[:parent_id] = parent_id
post[:author] = comment['author']
post[:author_id] = comment['author']['uid']
post[:raw] = unescape comment['text']
post[:author] = comment["author"]
post[:author_id] = comment["author"]["uid"]
post[:raw] = unescape comment["text"]
post[:created_at] = created_at
post
end
private
private
def unescape(html)
return nil unless html

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true
BASES = '{
# frozen_string_literal: true
BASES =
'{
"type" : "kbase",
"data" : [ {
"objectId" : "90b1ccf3-35aa-4d6f-848e-e7c122d92c58",
@ -9,7 +10,8 @@
} ]
}'
QUESTIONS = '{
QUESTIONS =
'{
"type": "question-search-result",
"data": {
"totalSize": 445,
@ -50,7 +52,8 @@
}
}'
QUESTION = '{
QUESTION =
'{
"type" : "question",
"data" : {
"uid" : "de20ed0a-5fe5-48a5-9c14-d854f9af99f1",

View File

@ -1,21 +1,20 @@
# frozen_string_literal: true
require 'minitest/autorun'
require 'yaml'
require_relative '../quandora_api.rb'
require_relative './test_data.rb'
require "minitest/autorun"
require "yaml"
require_relative "../quandora_api.rb"
require_relative "./test_data.rb"
class TestQuandoraApi < Minitest::Test
DEBUG = false
def initialize(args)
config = YAML::load_file(File.join(__dir__, 'config.yml'))
@domain = config['domain']
@username = config['username']
@password = config['password']
@kb_id = config['kb_id']
@question_id = config['question_id']
config = YAML.load_file(File.join(__dir__, "config.yml"))
@domain = config["domain"]
@username = config["username"]
@password = config["password"]
@kb_id = config["kb_id"]
@question_id = config["question_id"]
super args
end
@ -30,19 +29,19 @@ class TestQuandoraApi < Minitest::Test
end
def test_base_url
assert_equal 'https://mydomain.quandora.com/m/json', @quandora.base_url('mydomain')
assert_equal "https://mydomain.quandora.com/m/json", @quandora.base_url("mydomain")
end
def test_auth_header
user = 'Aladdin'
password = 'open sesame'
user = "Aladdin"
password = "open sesame"
auth_header = @quandora.auth_header user, password
assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth_header[:Authorization]
assert_equal "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", auth_header[:Authorization]
end
def test_list_bases_element_has_expected_structure
element = @quandora.list_bases[0]
expected = JSON.parse(BASES)['data'][0]
expected = JSON.parse(BASES)["data"][0]
debug element
check_keys expected, element
end
@ -50,24 +49,24 @@ class TestQuandoraApi < Minitest::Test
def test_list_questions_has_expected_structure
response = @quandora.list_questions @kb_id, 1
debug response
check_keys JSON.parse(QUESTIONS)['data']['result'][0], response[0]
check_keys JSON.parse(QUESTIONS)["data"]["result"][0], response[0]
end
def test_get_question_has_expected_structure
question = @quandora.get_question @question_id
expected = JSON.parse(QUESTION)['data']
expected = JSON.parse(QUESTION)["data"]
check_keys expected, question
expected_comment = expected['comments'][0]
actual_comment = question['comments'][0]
expected_comment = expected["comments"][0]
actual_comment = question["comments"][0]
check_keys expected_comment, actual_comment
expected_answer = expected['answersList'][1]
actual_answer = question['answersList'][0]
expected_answer = expected["answersList"][1]
actual_answer = question["answersList"][0]
check_keys expected_answer, actual_answer
expected_answer_comment = expected_answer['comments'][0]
actual_answer_comment = actual_answer['comments'][0]
expected_answer_comment = expected_answer["comments"][0]
actual_answer_comment = actual_answer["comments"][0]
check_keys expected_answer_comment, actual_answer_comment
end
@ -75,18 +74,16 @@ class TestQuandoraApi < Minitest::Test
def check_keys(expected, actual)
msg = "### caller[0]:\nKey not found in actual keys: #{actual.keys}\n"
expected.keys.each do |k|
assert (actual.keys.include? k), "#{k}"
end
expected.keys.each { |k| assert (actual.keys.include? k), "#{k}" }
end
def debug(message, show = false)
if show || DEBUG
puts '### ' + caller[0]
puts ''
puts "### " + caller[0]
puts ""
puts message
puts ''
puts ''
puts ""
puts ""
end
end
end

View File

@ -1,47 +1,46 @@
# frozen_string_literal: true
require 'minitest/autorun'
require 'cgi'
require 'time'
require_relative '../quandora_question.rb'
require_relative './test_data.rb'
require "minitest/autorun"
require "cgi"
require "time"
require_relative "../quandora_question.rb"
require_relative "./test_data.rb"
class TestQuandoraQuestion < Minitest::Test
def setup
@data = JSON.parse(QUESTION)['data']
@data = JSON.parse(QUESTION)["data"]
@question = QuandoraQuestion.new @data.to_json
end
def test_topic
topic = @question.topic
assert_equal @data['uid'], topic[:id]
assert_equal @data['author']['uid'], topic[:author_id]
assert_equal unescape(@data['title']), topic[:title]
assert_equal unescape(@data['content']), topic[:raw]
assert_equal Time.parse(@data['created']), topic[:created_at]
assert_equal @data["uid"], topic[:id]
assert_equal @data["author"]["uid"], topic[:author_id]
assert_equal unescape(@data["title"]), topic[:title]
assert_equal unescape(@data["content"]), topic[:raw]
assert_equal Time.parse(@data["created"]), topic[:created_at]
end
def test_user_from_author
author = {}
author['uid'] = 'uid'
author['firstName'] = 'Joe'
author['lastName'] = 'Schmoe'
author['email'] = 'joe.schmoe@mydomain.com'
author["uid"] = "uid"
author["firstName"] = "Joe"
author["lastName"] = "Schmoe"
author["email"] = "joe.schmoe@mydomain.com"
user = @question.user_from_author author
assert_equal 'uid', user[:id]
assert_equal 'Joe Schmoe', user[:name]
assert_equal 'joe.schmoe@mydomain.com', user[:email]
assert_equal "uid", user[:id]
assert_equal "Joe Schmoe", user[:name]
assert_equal "joe.schmoe@mydomain.com", user[:email]
assert_equal true, user[:staged]
end
def test_user_from_author_with_no_email
author = {}
author['uid'] = 'foo'
author["uid"] = "foo"
user = @question.user_from_author author
assert_equal 'foo@noemail.com', user[:email]
assert_equal "foo@noemail.com", user[:email]
end
def test_replies
@ -57,77 +56,77 @@ class TestQuandoraQuestion < Minitest::Test
assert_equal nil, replies[2][:reply_to_post_number]
assert_equal 4, replies[3][:reply_to_post_number]
assert_equal 3, replies[4][:reply_to_post_number]
assert_equal '2013-01-07 04:59:56 UTC', replies[0][:created_at].to_s
assert_equal '2013-01-08 16:49:32 UTC', replies[1][:created_at].to_s
assert_equal '2016-01-20 15:38:55 UTC', replies[2][:created_at].to_s
assert_equal '2016-01-21 15:38:55 UTC', replies[3][:created_at].to_s
assert_equal '2016-01-22 15:38:55 UTC', replies[4][:created_at].to_s
assert_equal "2013-01-07 04:59:56 UTC", replies[0][:created_at].to_s
assert_equal "2013-01-08 16:49:32 UTC", replies[1][:created_at].to_s
assert_equal "2016-01-20 15:38:55 UTC", replies[2][:created_at].to_s
assert_equal "2016-01-21 15:38:55 UTC", replies[3][:created_at].to_s
assert_equal "2016-01-22 15:38:55 UTC", replies[4][:created_at].to_s
end
def test_post_from_answer
answer = {}
answer['uid'] = 'uid'
answer['content'] = 'content'
answer['created'] = '2013-01-06T18:24:54.62Z'
answer['author'] = { 'uid' => 'auid' }
answer["uid"] = "uid"
answer["content"] = "content"
answer["created"] = "2013-01-06T18:24:54.62Z"
answer["author"] = { "uid" => "auid" }
post = @question.post_from_answer answer
assert_equal 'uid', post[:id]
assert_equal "uid", post[:id]
assert_equal @question.topic[:id], post[:parent_id]
assert_equal answer['author'], post[:author]
assert_equal 'auid', post[:author_id]
assert_equal 'content', post[:raw]
assert_equal Time.parse('2013-01-06T18:24:54.62Z'), post[:created_at]
assert_equal answer["author"], post[:author]
assert_equal "auid", post[:author_id]
assert_equal "content", post[:raw]
assert_equal Time.parse("2013-01-06T18:24:54.62Z"), post[:created_at]
end
def test_post_from_comment
comment = {}
comment['text'] = 'text'
comment['created'] = '2013-01-06T18:24:54.62Z'
comment['author'] = { 'uid' => 'auid' }
parent = { 'uid' => 'parent-uid' }
comment["text"] = "text"
comment["created"] = "2013-01-06T18:24:54.62Z"
comment["author"] = { "uid" => "auid" }
parent = { "uid" => "parent-uid" }
post = @question.post_from_comment comment, 0, parent
assert_equal 'parent-uid-0', post[:id]
assert_equal 'parent-uid', post[:parent_id]
assert_equal comment['author'], post[:author]
assert_equal 'auid', post[:author_id]
assert_equal 'text', post[:raw]
assert_equal Time.parse('2013-01-06T18:24:54.62Z'), post[:created_at]
assert_equal "parent-uid-0", post[:id]
assert_equal "parent-uid", post[:parent_id]
assert_equal comment["author"], post[:author]
assert_equal "auid", post[:author_id]
assert_equal "text", post[:raw]
assert_equal Time.parse("2013-01-06T18:24:54.62Z"), post[:created_at]
end
def test_post_from_comment_uses_parent_created_if_necessary
comment = {}
comment['author'] = { 'uid' => 'auid' }
parent = { 'created' => '2013-01-06T18:24:54.62Z' }
comment["author"] = { "uid" => "auid" }
parent = { "created" => "2013-01-06T18:24:54.62Z" }
post = @question.post_from_comment comment, 0, parent
assert_equal Time.parse('2013-01-06T18:24:54.62Z'), post[:created_at]
assert_equal Time.parse("2013-01-06T18:24:54.62Z"), post[:created_at]
end
def test_post_from_comment_uses_previous_comment_as_parent
comment = {}
comment['author'] = { 'uid' => 'auid' }
parent = { 'uid' => 'parent-uid', 'created' => '2013-01-06T18:24:54.62Z' }
comment["author"] = { "uid" => "auid" }
parent = { "uid" => "parent-uid", "created" => "2013-01-06T18:24:54.62Z" }
post = @question.post_from_comment comment, 1, parent
assert_equal 'parent-uid-1', post[:id]
assert_equal 'parent-uid-0', post[:parent_id]
assert_equal Time.parse('2013-01-06T18:24:54.62Z'), post[:created_at]
assert_equal "parent-uid-1", post[:id]
assert_equal "parent-uid-0", post[:parent_id]
assert_equal Time.parse("2013-01-06T18:24:54.62Z"), post[:created_at]
end
def test_users
users = @question.users
assert_equal 5, users.size
assert_equal 'Ida Inquisitive', users[0][:name]
assert_equal 'Harry Helpful', users[1][:name]
assert_equal 'Sam Smarty-Pants', users[2][:name]
assert_equal 'Greta Greatful', users[3][:name]
assert_equal 'Eddy Excited', users[4][:name]
assert_equal "Ida Inquisitive", users[0][:name]
assert_equal "Harry Helpful", users[1][:name]
assert_equal "Sam Smarty-Pants", users[2][:name]
assert_equal "Greta Greatful", users[3][:name]
assert_equal "Eddy Excited", users[4][:name]
end
private