|
| 1 | +require_relative "../utils/hackerspub/client" |
| 2 | + |
| 3 | +module Builders |
| 4 | + class HackerspubPostsBuilder < SiteBuilder |
| 5 | + def build |
| 6 | + return if ENV["BRIDGETOWN__DISABLE_BUILDERS"] == "true" |
| 7 | + return if ENV["HACKERSPUB_DISABLE"].to_s == "1" |
| 8 | + |
| 9 | + handle = ENV.fetch("HACKERSPUB_HANDLE", "@kodingwarrior") |
| 10 | + endpoint = ENV.fetch( |
| 11 | + "HACKERSPUB_GRAPHQL_URL", |
| 12 | + Utils::Hackerspub::Client::DEFAULT_ENDPOINT |
| 13 | + ) |
| 14 | + base_url = ENV.fetch("HACKERSPUB_BASE_URL", "https://hackers.pub") |
| 15 | + |
| 16 | + client = Utils::Hackerspub::Client.new( |
| 17 | + handle:, |
| 18 | + endpoint:, |
| 19 | + base_url:, |
| 20 | + ) |
| 21 | + |
| 22 | + posts = client.fetch_posts |
| 23 | + actor = client.fetch_actor_bio |
| 24 | + |
| 25 | + site.data["hackerspub_actor"] = actor if actor |
| 26 | + site.data["hackerspub_handle"] = handle |
| 27 | + |
| 28 | + visible_posts = posts.select do |post| |
| 29 | + visibility = post[:visibility].to_s.downcase |
| 30 | + visibility.empty? || visibility == "public" |
| 31 | + end |
| 32 | + |
| 33 | + unique_posts = visible_posts.uniq { |post| [post[:year], post[:encoded_slug] || post[:slug]] } |
| 34 | + |
| 35 | + unique_posts.sort_by { |post| post[:published_at] || Time.at(0) }.each do |post| |
| 36 | + path_slug = post[:encoded_slug] || post[:slug] |
| 37 | + add_resource :hackerspub_posts, "#{post[:year]}/#{path_slug}.html" do |
| 38 | + layout "hackerspub_post" |
| 39 | + title post[:name].to_s.strip.empty? ? post[:slug] : post[:name] |
| 40 | + date post[:published_at] if post[:published_at] |
| 41 | + published post[:published_raw] |
| 42 | + year post[:year] |
| 43 | + slug post[:slug] |
| 44 | + encoded_slug post[:encoded_slug] |
| 45 | + language post[:language] |
| 46 | + summary post[:summary] |
| 47 | + original_url post[:url] |
| 48 | + visibility post[:visibility] |
| 49 | + content post[:content] |
| 50 | + permalink "/hackerspub/#{post[:year]}/#{path_slug}/" |
| 51 | + end |
| 52 | + end |
| 53 | + rescue Utils::Hackerspub::Error => e |
| 54 | + Bridgetown.logger.error("HackerspubPostsBuilder:", e.message) |
| 55 | + return |
| 56 | + rescue StandardError => e |
| 57 | + Bridgetown.logger.error( |
| 58 | + "HackerspubPostsBuilder:", |
| 59 | + "Unexpected error while building Hackerspub posts — #{e.class}: #{e.message}" |
| 60 | + ) |
| 61 | + Bridgetown.logger.debug(e.backtrace.join("\n")) if Bridgetown.logger.debug? |
| 62 | + return |
| 63 | + end |
| 64 | + end |
| 65 | +end |
0 commit comments