~edwardloveall/scribe

ref: f05a12a880cbb1399925d9dc12317f6f5ce8a285 scribe/src/actions/articles/show.cr -rw-r--r-- 829 bytes
f05a12a8Edward Loveall Add support for missing posts a month 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
38
39
40
require "json"

class Articles::Show < BrowserAction
  fallback do
    post_id = ArticleIdParser.parse(context.request)
    case post_id
    in Monads::Just
      response = client_class.post_data(post_id.value!)
      page = PageConverter.new.convert(response.data.post)
      render_page(page)
    in Monads::Nothing, Monads::Maybe
      html(
        Errors::ParseErrorPage,
        message: "Error parsing the URL",
        status_code: 422,
        original_resource: request.resource,
      )
    end
  end

  def render_page(page : Page)
    html ShowPage, page: page
  end

  def render_page(page : MissingPage)
    raise MissingPageError.new
  end

  def client_class
    if use_local?
      LocalClient
    else
      MediumClient
    end
  end

  def use_local?
    ENV.fetch("USE_LOCAL", "false") == "true"
  end
end