Created
October 31, 2008 20:34
-
-
Save swdyh/21414 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
class GistID | |
GIST_URL = 'http://gist.github.com/' | |
ALL_LIMIT = 10 | |
def initialize user = get_user | |
@user = (user.nil? || user == :all) ? 'gists' : user | |
end | |
def get_user | |
user = `git config --global github.user`.strip | |
user.empty? ? nil : user | |
end | |
def recent | |
extract_id GIST_URL + @user | |
end | |
def all | |
page_size = pages | |
if page_size > ALL_LIMIT | |
raise 'page limit over' | |
end | |
urls = Array.new(page_size){ |i| i + 1}.map{ |i| "#{GIST_URL}#{@user}?page=#{i}"} | |
urls.map { |i| extract_id(i) }.flatten.uniq | |
end | |
def extract_id url | |
doc = Nokogiri::HTML read(url) | |
doc.css('.file .info a').map { |i| i['href'].sub('/', '') } | |
end | |
def pages | |
url = GIST_URL + @user | |
doc = Nokogiri::HTML read(url) | |
a = doc.css('.pagination a') | |
if a.size < 3 | |
1 | |
else | |
a[a.size - 2].inner_text.to_i | |
end | |
end | |
def read url | |
# puts url | |
open(url) | |
end | |
end | |
# gi = GistID.new | |
# p gi | |
# p gi.recent | |
# p gi.all | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment