Skip to content

Instantly share code, notes, and snippets.

@JuanitoFatas

JuanitoFatas/square-blog-archive Secret

Last active Feb 4, 2020
Embed
What would you like to do?
Generate Square Blog Archive in markdown
#!/usr/bin/env ruby
# frozen_string_literal: true
# Exit cleanly from an early interrupt
Signal.trap("INT") { abort }
require "nokogiri"
require "http"
class Article
def initialize(article)
@article = article
end
def to_s
%(- #{title_link}\n #{description} - Published in #{category} on #{date}.)
end
private
attr_reader :article, :title_link, :author, :date, :category
def title_link
%([#{title}](https://developer.squareup.com#{url}))
end
def description
article.
css("p[class^='archive-article-module--previewDescription']").
text
end
def title
article.
css("h3[class^='archive-article-module--previewTitle']").
text.
gsub("\u00A0", "") # Remove  
end
def url
article.attributes["href"].value
end
def category
category_date.first
end
def date
category_date.last
end
def category_date
@_category_date ||= begin
article.css("span[class^='archive-article-module--preview']").
text.
split(" | ")
end
end
end
class Page
def initialize(page)
@page = page
@document = Nokogiri::HTML.parse(fetch_page)
end
def to_s
[heading, posts].join("\n\n")
end
def heading
"### [Page #{number}](#{page})"
end
def posts
articles.map do |article|
Article.new(article).to_s
end.join("\n")
end
private
attr_reader :page, :document
def fetch_page
HTTP.follow.get(page).to_s
end
def articles
document.
css("a[href^='/blog/']").
css("a[class^='archive-article-module--preview']")
end
def number
if page.end_with? "archive"
1
else
page.split("/").last
end
end
end
module SquareBlog
URL = "https://developer.squareup.com/blog/archive"
def self.pages
[*"2"..last_page].map do |page_number|
File.join(URL, page_number)
end.prepend(URL)
end
def self.last_page
document = Nokogiri::HTML.parse(HTTP.follow.get(URL).to_s)
document.css("a[class^='blog-archive-module--page-num']").last.text
end
end
File.open("tmp/square-blog.md", "w+") do |file|
SquareBlog.pages.map do |page|
puts "Parsing #{page}"
file.print Page.new(page).to_s + "\n\n"
end
end
puts "Generated in tmp/square-blog.md."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment