adn-repostmentions/app/models/post_data.rb
2013-12-12 23:11:55 +01:00

53 lines
1.4 KiB
Ruby

# Contains data of a single ADN post.
class PostData
attr_accessor :post_id
attr_accessor :created_at
def initialize
@post_id = 1
@created_at = Date.new(1970,1,1)
end
# in: array of posts
# returns: class instance with the topmost (= most recent) post from the array
# assumes: the array of posts is sorted in reverse alphabetical order.
def self.most_recent_post(post_array)
post_id = 1
created_at = Time.new(1970,1,1)
post = PostData.new
post_array.each do |line|
line.each do |elem|
case elem[0]
when "id"
post_id = elem[1].to_i
when "created_at"
created_at = Time.parse(elem[1])
else
# (currently) not of interest
end
if post_id > post.post_id then
post.post_id = post_id
post.created_at = created_at
end
end
end
post
end
# in: array of posts with all (recent) mentions
# in: ID of latest repost that has been done already
# out: Array of post ids that haven't been reposted, yet
def self.extract_not_yet_reposted(mentions_array, latest_post_id)
ret = Array.new
mentions_array.each do |line|
line.each do |elem|
# We only care about the post's ID. Everything else: throw away.
if elem[0] == "id" then
cur_id = elem[1].to_i
ret << cur_id if cur_id > latest_post_id
end
end
end
ret
end
end