Skip to content

Instantly share code, notes, and snippets.

@prepor
Created November 10, 2010 19:25
Show Gist options
  • Save prepor/671358 to your computer and use it in GitHub Desktop.
Save prepor/671358 to your computer and use it in GitHub Desktop.
Gist auto fetcher and requirer
require 'pathname'
require 'open-uri'
def require_gist(number, file, options = {})
AutoGist::Gist.new(number, file, options).setup
end
# TODO Add logs
# TODO Reload gists (especially without ref)
module AutoGist
HUB_URL = 'https://gist.github.com/raw'
def self.path
@@gists_path ||= begin
path = Pathname(File.expand_path(Dir.pwd)) + '.autogist'
path.mkpath
path
end
end
class Gist
attr_accessor :number, :file, :options, :ref
def initialize(number, file, options = {})
@number, @file, @options = number, file, options
@ref = options[:ref] || 'last'
AutoGist.path
end
def setup
path = AutoGist.path + "#{number}_#{ref}_#{file}"
unless path.exist?
puts "AutoGist: #{raw_url} -> #{path}"
path.open('w') do |f|
f.flock File::LOCK_EX
f.puts open(raw_url).read
end
end
require path.to_s
end
def raw_url
"#{HUB_URL}/#{number}#{ref == 'last' ? '' : '/' + ref}/#{file}"
end
end
end
require 'autogist'
require_gist 603225, "em_chain.rb"
# https://gist.github.com/raw/603225/em_chain.rb -> /Local/path/.autogist/603225_last_em_chain.rb
chain = EMChain.new
puts 'yeee'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment