Class: Post

Inherits:
Object
  • Object
show all
Defined in:
lib/Post.rb

Defined Under Namespace

Classes: PostInfo

Class Method Summary collapse

Class Method Details

.getPostIDFromPostURLString(postURLString) ⇒ Object



15
16
17
18
19
20
# File 'lib/Post.rb', line 15

def self.getPostIDFromPostURLString(postURLString)
  uri = URI.parse(postURLString)
  postID = uri.path.split('/').last.split('-').last
  
  postID
end

.getPostPathFromPostURLString(postURLString) ⇒ Object



22
23
24
25
26
27
# File 'lib/Post.rb', line 22

def self.getPostPathFromPostURLString(postURLString)
  uri = URI.parse(postURLString)
  postPath = uri.path.split('/').last
  
  URI.decode(postPath)
end

.parsePostContentFromHTML(html) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/Post.rb', line 29

def self.parsePostContentFromHTML(html)
  json = nil
  html.search('script').each do |script|
      match = script.to_s[/(<script>window\.__APOLLO_STATE__ \= ){1}(.*)(<\/script>){1}/,2]
      if !match.nil? && match != ""
          json = JSON.parse(match)
      end
  end

  json
end

.parsePostInfoFromPostContent(content, postID) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/Post.rb', line 50

def self.parsePostInfoFromPostContent(content, postID)
   = PostInfo.new()
  .title = content&.dig("Post:#{postID}", "title")
  .tags = content&.dig("Post:#{postID}", "tags").map{ |tag| tag["__ref"].gsub! 'Tag:', '' }
  
  creatorRef = content&.dig("Post:#{postID}", "creator", "__ref")
  if !creatorRef.nil?
    .creator = content&.dig(creatorRef, "name")
  end

  firstPublishedAt = content&.dig("Post:#{postID}", "firstPublishedAt")
  if !firstPublishedAt.nil?
    .firstPublishedAt = Time.at(0, firstPublishedAt, :millisecond) 
  end

  latestPublishedAt = content&.dig("Post:#{postID}", "latestPublishedAt")
  if !latestPublishedAt.nil?
    .latestPublishedAt = Time.at(0, latestPublishedAt, :millisecond)
  end

  
end

.parsePostParagraphsFromPostContent(content, postID) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/Post.rb', line 41

def self.parsePostParagraphsFromPostContent(content, postID)
  result = content&.dig("Post:#{postID}", "content({\"postMeteringOptions\":null})", "bodyModel", "paragraphs")
  if result.nil?
    nil
  else
    result.map { |paragraph| content[paragraph["__ref"]] }
  end
end