diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/indrajith-dev-crystal.cr | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/indrajith-dev-crystal.cr b/src/indrajith-dev-crystal.cr index 74fecc2..f5ddabd 100644 --- a/src/indrajith-dev-crystal.cr +++ b/src/indrajith-dev-crystal.cr @@ -6,6 +6,7 @@ require "dotenv" Dotenv.load TOKEN = ENV["TOKEN"] +STRAPI_URL = ENV["STRAPI_URL"] module Indrajith::Dev::Crystal VERSION = "0.1.0" @@ -42,7 +43,7 @@ module Indrajith::Dev::Crystal get "/posts" do |context| begin - site = Crest::Resource.new("http://localhost:1337") + site = Crest::Resource.new("#{STRAPI_URL}") response = site.get("/api/posts", params: { @@ -55,16 +56,21 @@ module Indrajith::Dev::Crystal json_data = JSON.parse(response.body) data = json_data["data"].as_a - html_string = "" - data.each do |item| - attributes = item["attributes"] - # puts attributes["post_title"] - html_string += " - <li> - <p><a href='/post/#{attributes["slug"]}'>#{attributes["post_title"]}</a></p> - </li>" + puts data.size + if data.size == 0 + "<li>No Posts Yet</li>" + else + html_string = "" + data.each do |item| + attributes = item["attributes"] + # puts attributes["post_title"] + html_string += " + <li> + <p><a href='/post/#{attributes["slug"]}'>#{attributes["post_title"]}</a></p> + </li>" + end + html_string end - html_string rescue ex : Crest::NotFound puts ex.response render_404 @@ -85,7 +91,6 @@ module Indrajith::Dev::Crystal 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"] |