2016-05-26 14:54:38 +08:00
|
|
|
require 'json'
|
|
|
|
require 'cgi'
|
|
|
|
require 'time'
|
|
|
|
require_relative 'create_title.rb'
|
|
|
|
|
|
|
|
class SocialcastMessage
|
|
|
|
|
2017-02-26 20:54:07 +08:00
|
|
|
DEFAULT_CATEGORY = "Socialcast Import"
|
|
|
|
DEFAULT_TAG = "socialcast-import"
|
|
|
|
TAGS_AND_CATEGORIES = {
|
|
|
|
"somegroupname" => {
|
|
|
|
category: "Apple Stems",
|
|
|
|
tags: ["waxy", "tough"]
|
|
|
|
},
|
|
|
|
"someothergroupname" => {
|
|
|
|
category: "Orange Peels",
|
|
|
|
tags: ["oily"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def initialize(message_json)
|
|
|
|
@parsed_json = JSON.parse message_json
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def topic
|
|
|
|
topic = {}
|
|
|
|
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[:tags] = tags
|
|
|
|
topic[:category] = category
|
|
|
|
topic
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def title
|
|
|
|
CreateTitle.from_body @parsed_json['body']
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def tags
|
|
|
|
tags = []
|
|
|
|
if group
|
|
|
|
if TAGS_AND_CATEGORIES[group]
|
|
|
|
tags = TAGS_AND_CATEGORIES[group][:tags]
|
|
|
|
else
|
|
|
|
tags << group
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tags << DEFAULT_TAG
|
|
|
|
tags
|
|
|
|
end
|
2017-02-26 20:54:07 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def category
|
|
|
|
category = DEFAULT_CATEGORY
|
|
|
|
if group && TAGS_AND_CATEGORIES[group]
|
|
|
|
category = TAGS_AND_CATEGORIES[group][:category]
|
|
|
|
end
|
|
|
|
category
|
|
|
|
end
|
2017-02-26 20:54:07 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def group
|
|
|
|
@parsed_json['group']['groupname'].downcase if @parsed_json['group'] && @parsed_json['group']['groupname']
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def url
|
|
|
|
@parsed_json['url']
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def message_type
|
|
|
|
@parsed_json['message_type']
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def replies
|
|
|
|
posts = []
|
|
|
|
comments = @parsed_json['comments']
|
|
|
|
comments.each do |comment|
|
|
|
|
posts << post_from_comment(comment)
|
|
|
|
end
|
|
|
|
posts
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
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
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
private
|
2016-05-26 14:54:38 +08:00
|
|
|
|
2018-06-07 13:28:18 +08:00
|
|
|
def unescape(html)
|
|
|
|
return nil unless html
|
|
|
|
CGI.unescapeHTML html
|
|
|
|
end
|
2016-05-26 14:54:38 +08:00
|
|
|
end
|