From fff60d6f9fa9baf1bb87ced8713b744ea0c6ed5a Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Fri, 21 Jun 2024 13:28:34 +0530 Subject: Posts Handlers Changes * Handles Empty posts * Move Strapi baseurl to ENV --- src/indrajith-dev-crystal.cr | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src') 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 += " -
  • -

    #{attributes["post_title"]}

    -
  • " + puts data.size + if data.size == 0 + "
  • No Posts Yet
  • " + else + html_string = "" + data.each do |item| + attributes = item["attributes"] + # puts attributes["post_title"] + html_string += " +
  • +

    #{attributes["post_title"]}

    +
  • " + 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"] -- cgit v1.2.3