~edwardloveall/scribe

ref: 91687bb68908fd8376503a8bb04fb344bd839c9d scribe/src/actions/articles/show.cr -rw-r--r-- 789 bytes
91687bb6Edward Loveall Add automatic redirect instructions to homepage 7 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
require "json"

class Articles::Show < BrowserAction
  fallback do
    maybe_post_id = post_id(context.request.path)
    case maybe_post_id
    in Monads::Just
      response = client_class.post_data(maybe_post_id.value!)
      page = PageConverter.new.convert(response.data)
      html ShowPage, page: page
    in Monads::Nothing, Monads::Maybe
      html(
        Errors::ShowPage,
        message: "Error parsing the URL",
        status: 500,
      )
    end
  end

  def post_id(request_path : String)
    Monads::Try(Regex::MatchData)
      .new(->{ request_path.match(/([0-9a-f]+)$/i) })
      .to_maybe
      .fmap(->(matches : Regex::MatchData) { matches[1] })
  end

  def client_class
    if Lucky::Env.use_local?
      LocalClient
    else
      MediumClient
    end
  end
end