module Nyaaaaa::ModuleMethods
Public Instance Methods
fetch_latest_id()
click to toggle source
# File lib/nyaaaaa.rb, line 65 def fetch_latest_id id = get_latest_id save_latest_id(id) id end
get_index_html()
click to toggle source
# File lib/nyaaaaa.rb, line 18 def get_index_html begin url = Addressable::URI.parse(BASE_URL) charset = nil html = open(url.normalize.to_s) do |f| charset = f.charset f.read end [html, charset] rescue raise RequestError, "Request to #{BASE_URL} has failed." end end
get_latest_id()
click to toggle source
# File lib/nyaaaaa.rb, line 46 def get_latest_id scrape_latest_id(*get_index_html) end
latest_id()
click to toggle source
# File lib/nyaaaaa.rb, line 71 def latest_id unless File.exists? latest_id_cache_path fetch_latest_id end IO.read(latest_id_cache_path).to_i end
latest_id_cache_path()
click to toggle source
# File lib/nyaaaaa.rb, line 54 def latest_id_cache_path File.join var_dir, 'latest_id' end
latest_url()
click to toggle source
# File lib/nyaaaaa.rb, line 82 def latest_url url_with_id latest_id end
random_url()
click to toggle source
# File lib/nyaaaaa.rb, line 86 def random_url url_with_id Random.rand(1..latest_id) end
save_latest_id(id)
click to toggle source
# File lib/nyaaaaa.rb, line 58 def save_latest_id(id) unless File.exists? var_dir FileUtils.mkdir var_dir end IO.write(latest_id_cache_path, id.to_s) end
scrape_latest_id(html, charset)
click to toggle source
# File lib/nyaaaaa.rb, line 33 def scrape_latest_id(html, charset) begin doc = Nokogiri::HTML.parse(html, nil , charset) href = nil doc.xpath(XPATH_LATEST_LINK).each do |node| href = node.attribute('href').value end href.match(/\/neko(\d+)\//)[1].to_i rescue raise ParseError, "Parsing response HTML has failed." end end
url_with_id(id)
click to toggle source
# File lib/nyaaaaa.rb, line 78 def url_with_id(id) BASE_URL + sprintf(PAGE_PATH_TMPL, id) + '/' end
var_dir()
click to toggle source
# File lib/nyaaaaa.rb, line 50 def var_dir File.join GEM_ROOT, 'var' end