Class: PostRetriever

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

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ PostRetriever

Returns a new instance of PostRetriever.



5
6
7
# File 'lib/retriever/post_retriever.rb', line 5

def initialize(parser)
  @parser = parser
end

Instance Method Details

#load(user_id, post_id) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/retriever/post_retriever.rb', line 9

def load(user_id, post_id)
  parsed_url = parse_url(user_id, post_id)

  Post.new(parsed_url["title"],
    parsed_url["content"]["subtitle"],
    normalize_content(parsed_url["content"]["bodyModel"]["paragraphs"]),
    parsed_url["canonicalUrl"])
end

#normalize_content(paragraphs_raw) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/retriever/post_retriever.rb', line 18

def normalize_content(paragraphs_raw)
  body = ""
  paragraphs_raw[1..-1].each do |paragraph|
    body << paragraph["text"] + "\n\n"
  end
  body
end

#parse_url(user_id, post_id) ⇒ Object



26
27
28
# File 'lib/retriever/post_retriever.rb', line 26

def parse_url(user_id, post_id)
  @parser.new("https://www.medium.com/@#{user_id}/#{post_id}?format=json").parse["value"]
end