11月182013
0
例えば https://gist.github.com/shokai/6667948 の場合、
https://gist.github.com/shokai/6667948.json という風に後ろに.jsonを付けるとメタデータが読める。
JSON内のfiles配列にファイル名が列挙されている。
ファイルの実体を取得するにはraw/ファイル名にアクセスすればいい
https://gist.github.com/shokai/6667948/raw/MainActivity.scala
raw/(hash)/ファイル名だとgitのバージョンハッシュを指定できる。何も無しだとHEADになる。
Rubyで書くとこんな感じ
require 'json' require 'httparty' class Gist attr_reader :data, :code def initialize(url) res = HTTParty.get "#{url}.json" raise "#{url} get error (code:#{res.code})" unless res.code == 200 @data = JSON.parse res.body code_file = @data['files'].find{|i| i =~ /.+\.rb$/ } res2 = HTTParty.get "#{url}/raw/#{code_file}" raise "#{url} get error (code:#{res2.code})" unless res2.code == 200 @code = res2.body end end
gist = Gist.new 'https://gist.github.com/shokai/6667948' puts gist.code
設定ファイルにgistのURL書いたらプラグインとして読み込んでくれるようなのに使える。