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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
require "kemal"
require "crest"
require "json"
require "dotenv"
require "./utils/utils"
require "time"
Dotenv.load
TOKEN = ENV["TOKEN"]
STRAPI_URL = ENV["STRAPI_URL"]
module Indrajith::Dev::Crystal
VERSION = "0.1.0"
macro page_renderer(filename, title)
page_title = {{title}}
published_at=""
render "src/views/#{{{filename}}}.ecr", "src/views/layout.ecr"
end
macro blog_list_renderer(blog_content)
page_title = "Blog"
published_at=""
blog_lists = {{blog_content}}
render "src/views/blog.ecr", "src/views/layout.ecr"
end
macro render_404
page_renderer "404", "The VOID"
end
macro send_404(context)
context.response.status_code = 404
context.response.content_type = "text/plain"
context.response.print "Not Found"
end
def self.htmx_request?(context)
context.request.headers.has_key?("HX-Request") ||
context.request.headers.has_key?("Hx-Request") ||
context.request.headers.has_key?("hx-request")
end
get "/" do |context|
context.response.content_type = "text/html"
page_renderer "home", "Home"
end
get "/about" do |context|
context.response.content_type = "text/html"
page_renderer "about", "About "
end
get "/timeline" do |context|
context.response.content_type = "text/html"
page_renderer "timeline", "Timeline"
end
get "/contact" do |context|
context.response.content_type = "text/html"
page_renderer "contact", "Contact"
end
get "/blog" do |context|
begin
site = Crest::Resource.new("#{STRAPI_URL}")
response = site.get("/api/posts/group-by-date",
headers: {"Authorization" => "Bearer #{TOKEN}"}
)
post_groups = JSON.parse(response.body)
html_string = ""
post_groups.as_h.each do |date, posts|
published_time = Time.parse(date, "%Y-%m-%d", Time::Location.local)
html_string += "<div>
<h3 class='date-based'>#{format_date(published_time)}</h3>
<ul class='post-lists'>
"
posts.as_a.each do |post|
post_title = post["post_title"]
post_slug = post["slug"]
html_string += "
<li>
<a href='/post/#{post_slug}'>#{post_title}</a>
</li>
"
end
html_string += "</ul>
</div>
"
end
content = html_string
context.response.content_type = "text/html"
blog_list_renderer html_string
rescue ex
if ex.responds_to?(:response)
puts ex.response
end
blog_list_renderer "No Recent Posts"
end
end
get "/posts" do |context|
begin
site = Crest::Resource.new("#{STRAPI_URL}")
response = site.get("/api/posts",
params: {
"fields[0]" => "post_title",
"fields[1]" => "slug",
"pagination[pageSize]" => 3,
},
headers: {"Authorization" => "Bearer #{TOKEN}"}
)
json_data = JSON.parse(response.body)
data = json_data["data"].as_a
if data.size == 0
"<li>No Posts Yet</li>"
else
html_string = ""
data.each do |item|
attributes = item["attributes"]
html_string += "
<li>
<p><a href='/post/#{attributes["slug"]}'>#{attributes["post_title"]}</a></p>
</li>"
end
context.response.print html_string
end
rescue ex
if ex.responds_to?(:response)
puts ex.response
end
send_404 context
end
end
get "/post/:slug" do |context|
begin
slug = context.params.url["slug"]
site = Crest::Resource.new("#{STRAPI_URL}")
response = site.get("/api/posts",
params: {
"filters[slug][$eq]" => "#{slug}",
},
headers: {"Authorization" => "Bearer #{TOKEN}"}
)
json_data = JSON.parse(response.body)
data = json_data["data"].as_a
post_item = data[0]
post_attribute = post_item["attributes"]
content = post_attribute["post_content"]
published_raw_time = post_attribute["publishedAt"].as_s
published_time = Time.parse(published_raw_time, "%Y-%m-%dT%H:%M:%S.%3NZ", Time::Location.local)
published_at = format_date(published_time)
page_title = post_attribute["post_title"]
render "src/views/layout.ecr"
rescue ex
if ex.responds_to?(:response)
puts ex.response
end
send_404 context
end
end
error 404 do |context|
context.response.content_type = "text/html"
render_404
end
Kemal.run
end
|