Skip to content

Instantly share code, notes, and snippets.

@benpickles
Created September 27, 2011 12:11
Show Gist options
  • Save benpickles/1244912 to your computer and use it in GitHub Desktop.
Save benpickles/1244912 to your computer and use it in GitHub Desktop.
Fetch curated list of Github repos sorted by watchers. Each repo is cached for a random time so only the first fetch will have to hit the API N times.
class Repo
REPOS = %w(
benpickles/js-model
benpickles/peity
ismasan/hash_mapper
ismasan/jbundle
markevans/dragonfly
mloughran/api_cache
mloughran/em-hiredis
mloughran/juggler
mloughran/signature
olivernn/davis.js
)
include HTTParty
base_uri 'http://github.com/api/v2/json/repos/show'
format :json
attr_reader :attributes, :id
def self.all
REPOS.map { |id|
begin
new(id).tap { |repo|
repo.fetch
}
rescue APICache::APICacheError
nil
end
}.compact.sort_by { |repo|
repo.watchers
}.reverse
end
def initialize(id)
@id = id
end
def fetch
@attributes = APICache.get(id, :cache => 1.day + rand(5.days), :valid => :forever) do
self.class.get("/#{id}")['repository']
end
end
def method_missing(name)
attributes[name.to_s] || super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment