Skip to content

Instantly share code, notes, and snippets.

@jaredbeck
Created January 7, 2018 05:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredbeck/7e8ec4213c72bb3742ae83f99c211342 to your computer and use it in GitHub Desktop.
Save jaredbeck/7e8ec4213c72bb3742ae83f99c211342 to your computer and use it in GitHub Desktop.
the redirect art/game
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile(true) do
ruby '2.4.2'
source 'https://rubygems.org'
gem 'capybara'
gem 'faraday'
end
module Game
class CLI
URL_PATTERN = %r(permanent-redirect.xyz/pages)
TIME_PATTERN = 'This redirect was created at'
def initialize(n)
@n = n
end
def run
loop do
sleep_time = rand(1..3)
puts format('Sleep %d ..', sleep_time)
sleep sleep_time
puts url
response = Faraday.get(url)
page = Capybara::Node::Simple.new(response.body)
time = redirect_creation_time(page)
n2 = next_n(page)
puts format('%20d %30s %20d', @n, time, n2)
@n = n2
end
end
private
def next_n(page)
page.
find_all('a').
to_a.
map { |a| a['href'] }.
first { |href| href.match?(URL_PATTERN)}.
split('/').
last
end
def redirect_creation_time(page)
page.find('p', text: TIME_PATTERN).text.to_s.split(TIME_PATTERN).last
end
def url
format('https://permanent-redirect.xyz/pages/%d', @n)
end
end
end
Game::CLI.new(*ARGV).run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment