discourse/lib/oneboxer/gist_onebox.rb

30 lines
702 B
Ruby
Raw Normal View History

2013-02-05 13:16:51 -06:00
require_dependency 'oneboxer/handlebars_onebox'
module Oneboxer
class GistOnebox < HandlebarsOnebox
matcher /^https?:\/\/gist\.github\.com/
favicon 'github.png'
2013-02-25 10:42:20 -06:00
def translate_url
2013-02-11 20:45:08 -06:00
m = @url.match(/gist\.github\.com\/([^\/]+\/)?(?<id>[0-9a-f]+)/mi)
2013-02-05 13:16:51 -06:00
return "https://api.github.com/gists/#{m[:id]}" if m
end
def parse(data)
parsed = JSON.parse(data)
2013-04-25 03:00:02 -05:00
desc = parsed['description']
if desc.length > 120
desc = desc[0..120]
desc << "..."
end
result = {files: [], title: desc}
2013-02-05 13:16:51 -06:00
parsed['files'].each do |filename, attrs|
result[:files] << {filename: filename}.merge!(attrs)
end
2013-02-25 10:42:20 -06:00
result
2013-02-05 13:16:51 -06:00
end
end
end