Skip to content

Commit 4247f12

Browse files
Emmanuel Hadouxpushmatrix
Emmanuel Hadoux
authored andcommitted
Add a Thor command to install a widget by its Gist Id.
1 parent 339f2e2 commit 4247f12

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

bin/dashing

+28-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'thor'
44
require 'net/http'
55
require 'json'
6+
require 'open-uri'
67

78
class MockScheduler
89
def method_missing(*args)
@@ -55,9 +56,34 @@ module Dashing
5556
send("generate_#{type}".to_sym, name)
5657
rescue NoMethodError => e
5758
puts "Invalid generator. Either use widget, dashboard, or job"
58-
end
59+
end
5960
map "g" => :generate
6061

62+
desc "install GIST_ID", "Installs a new widget from a gist."
63+
def install(gist_id)
64+
public_url = "https://gist.github.com/#{gist_id}"
65+
gist = JSON.parse(open("https://api.github.com/gists/#{gist_id}").read)
66+
67+
gist['files'].each do |filename, contents|
68+
if filename.end_with?(".rb")
69+
create_file File.join(Dir.pwd, 'jobs', filename), contents['content']
70+
elsif filename.end_with?(".coffee", ".html", ".scss")
71+
widget_name = File.basename(filename, '.*')
72+
create_file File.join(Dir.pwd, 'widgets', widget_name, filename), contents['content']
73+
end
74+
end
75+
76+
print set_color("Don't forget to edit the ", :yellow)
77+
print set_color("Gemfile ", :yellow, :bold)
78+
print set_color("and run ", :yellow)
79+
print set_color("bundle install ", :yellow, :bold)
80+
say set_color("if needed. More information for this widget can be found at #{public_url}", :yellow)
81+
82+
rescue OpenURI::HTTPError => e
83+
say set_color("Could not find gist at #{public_url}"), :red
84+
end
85+
map "i" => :install
86+
6187
desc "start", "Starts the server in style!"
6288
method_option :job_path, :desc => "Specify the directory where jobs are stored"
6389
def start(*args)
@@ -76,7 +102,7 @@ module Dashing
76102
f = File.join(Dir.pwd, "jobs", "#{name}.rb")
77103
require f
78104
end
79-
105+
80106
end
81107
end
82108

0 commit comments

Comments
 (0)