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,9 +1,8 @@
# frozen_string_literal: true
require 'uri'
require "uri"
class CreateTitle
def self.from_body(body)
title = remove_mentions body
title = remove_urls title
@ -24,11 +23,11 @@ class CreateTitle
private
def self.remove_mentions(text)
text.gsub(/@[\w]*/, '')
text.gsub(/@[\w]*/, "")
end
def self.remove_urls(text)
text.gsub(URI::regexp(['http', 'https', 'mailto', 'ftp', 'ldap', 'ldaps']), '')
text.gsub(URI.regexp(%w[http https mailto ftp ldap ldaps]), "")
end
def self.remove_stray_punctuation(text)
@ -42,7 +41,7 @@ class CreateTitle
end
def self.complete_sentences(text)
/(^.*[\S]{2,}[.!?:]+)\W/.match(text[0...80] + ' ')
/(^.*[\S]{2,}[.!?:]+)\W/.match(text[0...80] + " ")
end
def self.complete_words(text)

View File

@ -1,14 +1,14 @@
# frozen_string_literal: true
require 'yaml'
require 'fileutils'
require_relative 'socialcast_api'
require "yaml"
require "fileutils"
require_relative "socialcast_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
@ -23,8 +23,8 @@ def export_users(page = 1)
users = @api.list_users(page: page)
return if users.empty?
users.each do |user|
File.open("output/users/#{user['id']}.json", 'w') do |f|
puts user['contact_info']['email']
File.open("output/users/#{user["id"]}.json", "w") do |f|
puts user["contact_info"]["email"]
f.write user.to_json
f.close
end
@ -36,12 +36,12 @@ def export_messages(page = 1)
messages = @api.list_messages(page: page)
return if messages.empty?
messages.each do |message|
File.open("output/messages/#{message['id']}.json", 'w') do |f|
title = message['title']
title = message['body'] if title.empty?
File.open("output/messages/#{message["id"]}.json", "w") do |f|
title = message["title"]
title = message["body"] if title.empty?
title = title.split('\n')[0][0..50] unless title.empty?
puts "#{message['id']}: #{title}"
puts "#{message["id"]}: #{title}"
f.write message.to_json
f.close
end
@ -51,9 +51,7 @@ end
def create_dir(path)
path = File.join(__dir__, path)
unless File.directory?(path)
FileUtils.mkdir_p(path)
end
FileUtils.mkdir_p(path) unless File.directory?(path)
end
load_config ARGV.shift

View File

@ -1,12 +1,11 @@
# frozen_string_literal: true
require_relative './socialcast_message.rb'
require_relative './socialcast_user.rb'
require 'set'
require_relative "./socialcast_message.rb"
require_relative "./socialcast_user.rb"
require "set"
require File.expand_path(File.dirname(__FILE__) + "/../base.rb")
class ImportScripts::Socialcast < ImportScripts::Base
MESSAGES_DIR = "output/messages"
USERS_DIR = "output/users"
@ -29,15 +28,13 @@ class ImportScripts::Socialcast < ImportScripts::Base
imported = 0
total = count_files(MESSAGES_DIR)
Dir.foreach(MESSAGES_DIR) do |filename|
next if filename == ('.') || filename == ('..')
next if filename == (".") || filename == ("..")
topics += 1
message_json = File.read MESSAGES_DIR + '/' + filename
message_json = File.read MESSAGES_DIR + "/" + filename
message = SocialcastMessage.new(message_json)
next unless message.title
created_topic = import_topic message.topic
if created_topic
import_posts message.replies, created_topic.topic_id
end
import_posts message.replies, created_topic.topic_id if created_topic
imported += 1
print_status topics, total
end
@ -48,8 +45,8 @@ class ImportScripts::Socialcast < ImportScripts::Base
users = 0
total = count_files(USERS_DIR)
Dir.foreach(USERS_DIR) do |filename|
next if filename == ('.') || filename == ('..')
user_json = File.read USERS_DIR + '/' + filename
next if filename == (".") || filename == ("..")
user_json = File.read USERS_DIR + "/" + filename
user = SocialcastUser.new(user_json).user
create_user user, user[:id]
users += 1
@ -58,7 +55,7 @@ class ImportScripts::Socialcast < ImportScripts::Base
end
def count_files(path)
Dir.foreach(path).select { |f| f != '.' && f != '..' }.count
Dir.foreach(path).select { |f| f != "." && f != ".." }.count
end
def import_topic(topic)
@ -80,9 +77,7 @@ class ImportScripts::Socialcast < 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)
@ -95,9 +90,6 @@ class ImportScripts::Socialcast < ImportScripts::Base
puts new_post.inspect
end
end
end
if __FILE__ == $0
ImportScripts::Socialcast.new.perform
end
ImportScripts::Socialcast.new.perform if __FILE__ == $0

View File

@ -1,10 +1,9 @@
# frozen_string_literal: true
require 'base64'
require 'json'
require "base64"
require "json"
class SocialcastApi
attr_accessor :domain, :username, :password
def initialize(domain, username, password)
@ -29,12 +28,12 @@ class SocialcastApi
def list_users(opts = {})
page = opts[:page] ? opts[:page] : 1
response = request "#{base_url}/users?page=#{page}"
response['users'].sort { |u| u['id'] }
response["users"].sort { |u| u["id"] }
end
def list_messages(opts = {})
page = opts[:page] ? opts[:page] : 1
response = request "#{base_url}/messages?page=#{page}"
response['messages'].sort { |m| m['id'] }
response["messages"].sort { |m| m["id"] }
end
end

View File

@ -1,24 +1,23 @@
# frozen_string_literal: true
require 'json'
require 'cgi'
require 'time'
require_relative 'create_title.rb'
require "json"
require "cgi"
require "time"
require_relative "create_title.rb"
class SocialcastMessage
DEFAULT_CATEGORY = "Socialcast Import"
DEFAULT_TAG = "socialcast-import"
TAGS_AND_CATEGORIES = {
"somegroupname" => {
category: "Apple Stems",
tags: ["waxy", "tough"]
tags: %w[waxy tough],
},
"someothergroupname" => {
category: "Orange Peels",
tags: ["oily"]
}
}
tags: ["oily"],
},
}
def initialize(message_json)
@parsed_json = JSON.parse message_json
@ -26,18 +25,18 @@ class SocialcastMessage
def topic
topic = {}
topic[:id] = @parsed_json['id']
topic[:author_id] = @parsed_json['user']['id']
topic[:id] = @parsed_json["id"]
topic[:author_id] = @parsed_json["user"]["id"]
topic[:title] = title
topic[:raw] = @parsed_json['body']
topic[:created_at] = Time.parse @parsed_json['created_at']
topic[:raw] = @parsed_json["body"]
topic[:created_at] = Time.parse @parsed_json["created_at"]
topic[:tags] = tags
topic[:category] = category
topic
end
def title
CreateTitle.from_body @parsed_json['body']
CreateTitle.from_body @parsed_json["body"]
end
def tags
@ -55,39 +54,37 @@ class SocialcastMessage
def category
category = DEFAULT_CATEGORY
if group && TAGS_AND_CATEGORIES[group]
category = TAGS_AND_CATEGORIES[group][:category]
end
category = TAGS_AND_CATEGORIES[group][:category] if group && TAGS_AND_CATEGORIES[group]
category
end
def group
@parsed_json['group']['groupname'].downcase if @parsed_json['group'] && @parsed_json['group']['groupname']
if @parsed_json["group"] && @parsed_json["group"]["groupname"]
@parsed_json["group"]["groupname"].downcase
end
end
def url
@parsed_json['url']
@parsed_json["url"]
end
def message_type
@parsed_json['message_type']
@parsed_json["message_type"]
end
def replies
posts = []
comments = @parsed_json['comments']
comments.each do |comment|
posts << post_from_comment(comment)
end
comments = @parsed_json["comments"]
comments.each { |comment| posts << post_from_comment(comment) }
posts
end
def post_from_comment(comment)
post = {}
post[:id] = comment['id']
post[:author_id] = comment['user']['id']
post[:raw] = comment['text']
post[:created_at] = Time.parse comment['created_at']
post[:id] = comment["id"]
post[:author_id] = comment["user"]["id"]
post[:raw] = comment["text"]
post[:created_at] = Time.parse comment["created_at"]
post
end

View File

@ -1,26 +1,24 @@
# frozen_string_literal: true
require 'json'
require 'cgi'
require 'time'
require "json"
require "cgi"
require "time"
class SocialcastUser
def initialize(user_json)
@parsed_json = JSON.parse user_json
end
def user
email = @parsed_json['contact_info']['email']
email = "#{@parsed_json['id']}@noemail.com" unless email
email = @parsed_json["contact_info"]["email"]
email = "#{@parsed_json["id"]}@noemail.com" unless email
user = {}
user[:id] = @parsed_json['id']
user[:name] = @parsed_json['name']
user[:username] = @parsed_json['username']
user[:id] = @parsed_json["id"]
user[:name] = @parsed_json["name"]
user[:username] = @parsed_json["username"]
user[:email] = email
user[:staged] = true
user
end
end

View File

@ -1,26 +1,28 @@
# frozen_string_literal: true
require 'minitest/autorun'
require_relative '../create_title.rb'
require "minitest/autorun"
require_relative "../create_title.rb"
class TestCreateTitle < Minitest::Test
def test_create_title_1
body = "@GreatCheerThreading \nWhere can I find information on how GCTS stacks up against the competition? What are the key differentiators?"
body =
"@GreatCheerThreading \nWhere can I find information on how GCTS stacks up against the competition? What are the key differentiators?"
expected = "Where can I find information on how GCTS stacks up against the competition?"
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_2
body = "GCTS in 200 stores across town. How many threads per inch would you guess? @GreatCheerThreading"
body =
"GCTS in 200 stores across town. How many threads per inch would you guess? @GreatCheerThreading"
expected = "GCTS in 200 stores across town. How many threads per inch would you guess?"
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_3
body = "gFabric Sheets 1.2 now has Great Cheer Threads, letting you feel the softness running through the cotton fibers."
body =
"gFabric Sheets 1.2 now has Great Cheer Threads, letting you feel the softness running through the cotton fibers."
expected = "gFabric Sheets 1.2 now has Great Cheer Threads, letting you feel the softness..."
title = CreateTitle.from_body body
assert_equal(expected, title)
@ -34,49 +36,56 @@ class TestCreateTitle < Minitest::Test
end
def test_create_title_5
body = "One sentence. Two sentence. Three sentence. Four is going to go on and on for more words than we want."
body =
"One sentence. Two sentence. Three sentence. Four is going to go on and on for more words than we want."
expected = "One sentence. Two sentence. Three sentence."
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_6
body = "Anyone know of any invite codes for www.greatcheer.io (the Great Cheer v2 site)?\n\n//cc @RD @GreatCheerThreading"
body =
"Anyone know of any invite codes for www.greatcheer.io (the Great Cheer v2 site)?\n\n//cc @RD @GreatCheerThreading"
expected = "Anyone know of any invite codes for www.greatcheer.io (the Great Cheer v2 site)?"
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_6b
body = "Anyone know of any invite codes for www.greatcheer.io (the Great Cheer v2 site of yore)?\n\n//cc @RD @GreatCheerThreading"
body =
"Anyone know of any invite codes for www.greatcheer.io (the Great Cheer v2 site of yore)?\n\n//cc @RD @GreatCheerThreading"
expected = "Anyone know of any invite codes for www.greatcheer.io (the Great Cheer v2 site..."
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_6c
body = "Anyone know of any invite codes for www.greatcheer.io?! (the Great Cheer v2 site of yore)?\n\n//cc @RD @GreatCheerThreading"
body =
"Anyone know of any invite codes for www.greatcheer.io?! (the Great Cheer v2 site of yore)?\n\n//cc @RD @GreatCheerThreading"
expected = "Anyone know of any invite codes for www.greatcheer.io?!"
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_7
body = "@GreatCheerThreading \n\nDoes anyone know what the plan is to move to denser 1.2 threads for GCTS?\n\nI have a customer interested in the higher thread counts offered in 1.2."
body =
"@GreatCheerThreading \n\nDoes anyone know what the plan is to move to denser 1.2 threads for GCTS?\n\nI have a customer interested in the higher thread counts offered in 1.2."
expected = "Does anyone know what the plan is to move to denser 1.2 threads for GCTS?"
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_8
body = "@GreatCheerThreading @FabricWeavingWorldwide \n\nI was just chatting with a customer, after receiving this email:\n\n\"Ours is more of a ‘conceptual’ question. We have too much fiber"
body =
"@GreatCheerThreading @FabricWeavingWorldwide \n\nI was just chatting with a customer, after receiving this email:\n\n\"Ours is more of a ‘conceptual’ question. We have too much fiber"
expected = "I was just chatting with a customer, after receiving this email:"
title = CreateTitle.from_body body
assert_equal(expected, title)
end
def test_create_title_9
body = "Hi,\n\nDoes anyone have a PPT deck on whats new in cotton (around 10 or so slides) nothing to detailed as per what we have in the current 1.x version?\n\nI am not after a what's coming in cotton 2"
body =
"Hi,\n\nDoes anyone have a PPT deck on whats new in cotton (around 10 or so slides) nothing to detailed as per what we have in the current 1.x version?\n\nI am not after a what's coming in cotton 2"
expected = "Does anyone have a PPT deck on whats new in cotton (around 10 or so slides)..."
title = CreateTitle.from_body body
assert_equal(expected, title)
@ -90,7 +99,8 @@ class TestCreateTitle < Minitest::Test
end
def test_create_title_11
body = "Hi Guys,\nI'm working with #gtcs and one of the things we're playing with is TC. What better tool to demo and use than our own \nhttps://greatcheerthreading.com/themostthreads/cool-stuff\n\nThis used to work great in 2013,"
body =
"Hi Guys,\nI'm working with #gtcs and one of the things we're playing with is TC. What better tool to demo and use than our own \nhttps://greatcheerthreading.com/themostthreads/cool-stuff\n\nThis used to work great in 2013,"
expected = "I'm working with #gtcs and one of the things we're playing with is TC."
title = CreateTitle.from_body body
assert_equal(expected, title)
@ -104,10 +114,10 @@ class TestCreateTitle < Minitest::Test
end
def test_create_title_13
body = "Embroidered TC ... http://blogs.greatcheerthreading.com/thread/embroidering-the-threads-is-just-the-beginning\n@SoftStuff @TightWeave and team hopefully can share their thoughts on this recent post."
body =
"Embroidered TC ... http://blogs.greatcheerthreading.com/thread/embroidering-the-threads-is-just-the-beginning\n@SoftStuff @TightWeave and team hopefully can share their thoughts on this recent post."
expected = "and team hopefully can share their thoughts on this recent post."
title = CreateTitle.from_body body
assert_equal(expected, title)
end
end

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true
USERS = '{
USERS =
'{
"users": [
{
"contact_info": {
@ -1082,7 +1083,8 @@ USERS = '{
]
}'
MESSAGES = '{
MESSAGES =
'{
"messages": [
{
"id": 426,
@ -5429,7 +5431,8 @@ MESSAGES = '{
"messages_next_page": 2
}'
MESSAGES_PG_2 = '{
MESSAGES_PG_2 =
'{
"messages": [
{
"id": 386,

View File

@ -1,21 +1,20 @@
# frozen_string_literal: true
require 'minitest/autorun'
require 'yaml'
require_relative '../socialcast_api.rb'
require_relative './test_data.rb'
require "minitest/autorun"
require "yaml"
require_relative "../socialcast_api.rb"
require_relative "./test_data.rb"
class TestSocialcastApi < Minitest::Test
DEBUG = false
def initialize(args)
config = YAML::load_file(File.join(__dir__, 'config.ex.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.ex.yml"))
@domain = config["domain"]
@username = config["username"]
@password = config["password"]
@kb_id = config["kb_id"]
@question_id = config["question_id"]
super args
end
@ -30,18 +29,18 @@ class TestSocialcastApi < Minitest::Test
end
def test_base_url
assert_equal 'https://demo.socialcast.com/api', @socialcast.base_url
assert_equal "https://demo.socialcast.com/api", @socialcast.base_url
end
def test_headers
headers = @socialcast.headers
assert_equal 'Basic ZW1pbHlAc29jaWFsY2FzdC5jb206ZGVtbw==', headers[:Authorization]
assert_equal 'application/json', headers[:Accept]
assert_equal "Basic ZW1pbHlAc29jaWFsY2FzdC5jb206ZGVtbw==", headers[:Authorization]
assert_equal "application/json", headers[:Accept]
end
def test_list_users
users = @socialcast.list_users
expected = JSON.parse(USERS)['users'].sort { |u| u['id'] }
expected = JSON.parse(USERS)["users"].sort { |u| u["id"] }
assert_equal 15, users.size
assert_equal expected[0], users[0]
end
@ -53,14 +52,14 @@ class TestSocialcastApi < Minitest::Test
def test_list_messages
messages = @socialcast.list_messages
expected = JSON.parse(MESSAGES)['messages'].sort { |m| m['id'] }
expected = JSON.parse(MESSAGES)["messages"].sort { |m| m["id"] }
assert_equal 20, messages.size
check_keys expected[0], messages[0]
end
def test_messages_next_page
messages = @socialcast.list_messages(page: 2)
expected = JSON.parse(MESSAGES_PG_2)['messages'].sort { |m| m['id'] }
expected = JSON.parse(MESSAGES_PG_2)["messages"].sort { |m| m["id"] }
assert_equal 20, messages.size
check_keys expected[0], messages[0]
end
@ -69,18 +68,16 @@ class TestSocialcastApi < 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,8 +1,8 @@
# frozen_string_literal: true
require_relative './socialcast_message.rb'
require_relative './socialcast_user.rb'
require 'set'
require_relative "./socialcast_message.rb"
require_relative "./socialcast_user.rb"
require "set"
require File.expand_path(File.dirname(__FILE__) + "/../base.rb")
MESSAGES_DIR = "output/messages"
@ -11,8 +11,8 @@ def titles
topics = 0
total = count_files(MESSAGES_DIR)
Dir.foreach(MESSAGES_DIR) do |filename|
next if filename == ('.') || filename == ('..')
message_json = File.read MESSAGES_DIR + '/' + filename
next if filename == (".") || filename == ("..")
message_json = File.read MESSAGES_DIR + "/" + filename
message = SocialcastMessage.new(message_json)
next unless message.title
#puts "#{filename}, #{message.replies.size}, #{message.topic[:raw].size}, #{message.message_type}, #{message.title}"
@ -23,7 +23,7 @@ def titles
end
def count_files(path)
Dir.foreach(path).select { |f| f != '.' && f != '..' }.count
Dir.foreach(path).select { |f| f != "." && f != ".." }.count
end
titles