Class: Thor::InstallGist

Inherits:
Thor
  • Object
show all
Defined in:
lib/thor-install-gist.rb,
lib/thor-install-gist/version.rb

Defined Under Namespace

Classes: ThorTaskNotFound

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Instance Method Details

#gist(gist_id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/thor-install-gist.rb', line 13

def gist(gist_id)
  @gist_id = case gist_id
  when /^\d+$/
    gist_id
  when %r|^https?://gist\.github\.com/\d+|
    URI.parse(gist_id).path.scan(/\d+/).first
  when %r|^gist?:(//)?\d+|
    gist = URI.parse(gist_id)
    gist.opaque || gist.host
  else
    raise "Invalid gist uri scheme: #{gist_id}"
  end

  json = JSON.parse open(api_path).read
  if file_info = json["files"].find {|f| f[0] =~ /(thor|\.rb$)/i }
    raw_url = file_info[1]["raw_url"]
    Thor::Runner.new(["install", raw_url], options).install raw_url
  else
    raise ThorTaskNotFound, "Cannot find thor task file in your gist"
  end
end