Last active
November 26, 2015 20:45
-
-
Save rye/d3830a24062d85fb5723 to your computer and use it in GitHub Desktop.
Drink from the Monoid firehose! (Downloads the latest Monoid release)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'uri' | |
require 'net/http' | |
module Monoid | |
class Site | |
attr_accessor :base_uri | |
def initialize(base_uri = URI.parse('https://larsenwork.com')) | |
@base_uri = base_uri | |
end | |
def release_url | |
parse_release_javascript[:monoid_url] | |
end | |
protected | |
def release_file_url | |
URI.join(@base_uri.to_s, '/monoid/js/release.js') | |
end | |
def release_javascript | |
data = nil | |
Net::HTTP.start(@base_uri.host) do |http| | |
response = http.get(release_file_url.path) | |
data = response.body | |
end | |
data | |
end | |
def parse_release_javascript | |
{ monoid_url: release_javascript.match(/MONOID_URL\ =\ \'(?<monoid_url>\S+)\'/)[:monoid_url] } | |
end | |
end | |
module Downloader | |
TRACKING_SYMBOLS = [:tight, :half_tight, :normal, :half_loose, :loose] | |
LINE_HEIGHT_SYMBOLS = [:extra_small, :small, :medium, :large, :extra_large] | |
LIGATURES = DOLLAR = ZERO = ONE = L = [true, false] | |
def self.filename(tracking: :normal, line_height: :medium, dollar: false, zero: false, one: false, l: false, ligatures: true) | |
"Monoid%s%s%s%s%s%s%s.zip" % [tracking_string(tracking), | |
line_height_string(line_height), | |
dollar_string(dollar), | |
zero_string(zero), | |
one_string(one), | |
l_string(l), | |
ligatures_string(ligatures)] | |
end | |
def self.urls(site) | |
release_url = site.release_url | |
TRACKING_SYMBOLS.map do |tracking| | |
LINE_HEIGHT_SYMBOLS.map do |line_height| | |
DOLLAR.map do |dollar| | |
ZERO.map do |zero| | |
ONE.map do |one| | |
L.map do |l| | |
LIGATURES.map do |ligatures| | |
release_url + | |
self.filename(tracking: tracking, | |
line_height: line_height, | |
dollar: dollar, | |
zero: zero, | |
one: one, | |
l: l, | |
ligatures: ligatures) | |
end | |
end | |
end | |
end | |
end | |
end | |
end.flatten | |
end | |
def self.download(url) | |
data = nil | |
uri = URI.parse(url) | |
Monoid::Logger.log('downloader', 1, "Downloading #{uri.to_s} to #{File.basename(uri.path)}") | |
Net::HTTP.start(uri.host) do |http| | |
response = http.get(uri.path) | |
data = response.body | |
end | |
filename = File.basename(uri.path) | |
open(filename, 'wb') do |io| | |
io.write data | |
end | |
data | |
end | |
def self.download_urls(site) | |
urls(site).each do |url| | |
download(url) | |
end | |
end | |
protected | |
def self.tracking_string(tracking_symbol) | |
case tracking_symbol | |
when :tight | |
'-Tight' | |
when :half_tight | |
'-HalfTight' | |
when :half_loose | |
'-HalfLoose' | |
when :loose | |
'-Loose' | |
else | |
'' | |
end | |
end | |
def self.line_height_string(line_height_symbol) | |
case line_height_symbol | |
when :extra_small | |
'-XtraSmall' | |
when :small | |
'-Small' | |
when :large | |
'-Large' | |
when :extra_large | |
'-XtraLarge' | |
else | |
'' | |
end | |
end | |
def self.dollar_string(dollar) | |
dollar ? '-Dollar' : '' | |
end | |
def self.zero_string(zero) | |
zero ? '-0' : '' | |
end | |
def self.one_string(one) | |
one ? '-1' : '' | |
end | |
def self.l_string(l) | |
l ? '-l' : '' | |
end | |
def self.ligatures_string(ligatures) | |
ligatures ? '' : '-NoCalt' | |
end | |
end | |
module Logger | |
INDENT = ' ' | |
def self.log(tag = nil, indent = 0, message) | |
puts "#{"[%s]" % tag if tag}#{INDENT * indent} #{message}" | |
end | |
end | |
end | |
site = Monoid::Site.new | |
Monoid::Logger.log('debug', "Site url: #{site.release_url}") | |
Monoid::Logger.log('downloader', "Generated #{Monoid::Downloader.urls(site).count} urls!") | |
Monoid::Downloader.download_urls(site) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment