~edwardloveall/scribe

ref: 1dcded9153c482553538fdf9a4f80e7d9361b19c scribe/src/classes/page_converter.cr -rw-r--r-- 1.3 KiB
1dcded91Edward Loveall Update changelog to mention no DATABASE_URL 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class PageConverter
  def convert(data : PostResponse::Data) : Page
    title, content = title_and_content(data)
    author = data.post.creator
    created_at = Time.unix_ms(data.post.createdAt)
    gist_store = gist_store(content)
    Page.new(
      title: title,
      author: author,
      created_at: Time.unix_ms(data.post.createdAt),
      nodes: ParagraphConverter.new.convert(content, gist_store)
    )
  end

  def title_and_content(data : PostResponse::Data) : {String, Array(PostResponse::Paragraph)}
    title = data.post.title
    paragraphs = data.post.content.bodyModel.paragraphs
    non_content_paragraphs = paragraphs.reject { |para| para.text == title }
    {title, non_content_paragraphs}
  end

  private def gist_store(paragraphs) : GistStore | RateLimitedGistStore
    store = GistStore.new
    gist_urls = GistScanner.new(paragraphs).scan
    gist_responses = gist_urls.map do |url|
      params = GistParams.extract_from_url(url)
      response = GithubClient.get_gist_response(params.id)
      if response.is_a?(GithubClient::RateLimitedResponse)
        return RateLimitedGistStore.new
      end
      JSON.parse(response.data.body)["files"].as_h.values.map do |json_any|
        store.store_gist_file(params.id, GistFile.from_json(json_any.to_json))
      end
    end
    store
  end
end