class Gisty
Constants
- AA
`figlet -f contributed/bdffonts/clb8x8.flf gisty`.gsub('#', 'm')
- COMMAND_PATH
- GISTY_URL
- GIST_API_URL
- GIST_URI
- USER_AGENT
- VERSION
Public Class Methods
new(path, login = nil, token = nil, opt = {})
click to toggle source
# File lib/gisty.rb 26 def initialize path, login = nil, token = nil, opt = {} 27 if login.class == Hash 28 opt = login 29 end 30 if opt[:access_token] && opt[:access_token].size > 0 31 @access_token = opt[:access_token].strip 32 else 33 raise UnsetAuthInfoException 34 end 35 @dir = Pathname.pwd.realpath.join(File.expand_path(path)) 36 FileUtils.mkdir_p @dir unless @dir.exist? 37 @ssl_ca = opt[:ssl_ca] 38 @ssl_verify = case opt[:ssl_verify] 39 when :none, /none/i, OpenSSL::SSL::VERIFY_NONE 40 OpenSSL::SSL::VERIFY_NONE 41 else 42 OpenSSL::SSL::VERIFY_PEER 43 end 44 @api_url = opt[:api_url] || GIST_API_URL 45 @base_uri = opt[:base_uri] || GIST_URI 46 end
parse_link(link)
click to toggle source
# File lib/gisty.rb 89 def self.parse_link link 90 return nil if link.nil? 91 link.split(', ').inject({}) do |r, i| 92 url, rel = i.split '; ' 93 r[rel.gsub(/^rel=/, '').gsub('"', '').to_sym] = url.gsub(/[<>]/, '').strip 94 r 95 end 96 end
Public Instance Methods
all_mygists(&block)
click to toggle source
# File lib/gisty.rb 48 def all_mygists &block 49 r = [] 50 opt = {} 51 limit = 30 52 limit.times do 53 tmp = mygists opt 54 r << tmp[:content] 55 56 if block 57 tmp[:content].each {|i| block.call i } 58 end 59 60 if tmp[:link][:next] 61 opt[:url] = tmp[:link][:next] 62 else 63 break 64 end 65 end 66 r.flatten 67 end
build_params(paths)
click to toggle source
# File lib/gisty.rb 206 def build_params paths 207 list = (Array === paths ? paths : [paths]).map { |i| Pathname.new i } 208 raise InvalidFileException if list.any?{ |i| !i.file? } 209 210 params = {} 211 params['files'] = {} 212 list.each_with_index do |i, index| 213 params['files'][i.basename.to_s] = { 'content' => IO.read(i) } 214 end 215 params 216 end
clone(id)
click to toggle source
# File lib/gisty.rb 102 def clone id 103 FileUtils.cd @dir do 104 c = "git clone git@#{@base_uri}:#{id}.git" 105 Kernel.system c 106 end 107 end
create(paths, opt = { :private => false })
click to toggle source
# File lib/gisty.rb 218 def create paths, opt = { :private => false } 219 params = build_params paths 220 if opt[:private] 221 params['public'] = false 222 else 223 params['public'] = true 224 end 225 post params 226 end
delete(id)
click to toggle source
# File lib/gisty.rb 129 def delete id 130 FileUtils.rm_rf @dir.join(id) if @dir.join(id).exist? 131 end
list()
click to toggle source
# File lib/gisty.rb 109 def list 110 dirs = local_gist_directories.map do |i| 111 [i.basename.to_s, Pathname.glob(i.to_s + '/*').map { |i| i.basename.to_s }] 112 end 113 re_pub = /^\d+$/ 114 pub = dirs.select { |i| re_pub.match(i.first) }.sort_by { |i| i.first.to_i }.reverse 115 pri = dirs.select { |i| !re_pub.match(i.first) }.sort_by { |i| i.first } 116 { :public => pub, :private => pri } 117 end
local_gist_directories()
click to toggle source
# File lib/gisty.rb 123 def local_gist_directories 124 Pathname.glob(@dir.to_s + '/*').select do |i| 125 i.directory? && !i.to_s.match(/^_/) && i.basename.to_s != 'commands' 126 end 127 end
local_ids()
click to toggle source
# File lib/gisty.rb 119 def local_ids 120 local_gist_directories.map {|i| i.basename.to_s } 121 end
mygists(opt = {})
click to toggle source
# File lib/gisty.rb 69 def mygists opt = {} 70 url = opt[:url] || @api_url 71 open_uri_opt = { 'User-Agent' => USER_AGENT, 'Authorization' => "token #{@access_token}"} 72 if @ssl_ca && OpenURI::Options.key?(:ssl_ca_cer) 73 open_uri_opt[:ssl_ca_cert] = @ssl_ca 74 end 75 if @ssl_verify && OpenURI::Options.key?(:ssl_verify_mode) 76 open_uri_opt[:ssl_verify_mode] = @ssl_verify 77 end 78 proxy = URI.parse(url.to_s).find_proxy 79 80 if proxy && proxy.user && OpenURI::Options.key?(:proxy_http_basic_authentication) 81 open_uri_opt[:proxy_http_basic_authentication] = [proxy, proxy.user, proxy.password] 82 end 83 84 OpenURI.open_uri(url, open_uri_opt) do |f| 85 { :content => JSON.parse(f.read), :link => Gisty.parse_link(f.meta['link']) || {} } 86 end 87 end
post(params)
click to toggle source
# File lib/gisty.rb 174 def post params 175 url = URI.parse(@api_url) 176 req = Net::HTTP::Post.new(url.path) 177 req.set_content_type('application/json') 178 req['User-Agent'] = USER_AGENT 179 req['Authorization'] = "token #{@access_token}" 180 req.body = params.to_json 181 if ENV['https_proxy'] 182 proxy_uri = URI.parse(ENV['https_proxy']) 183 if proxy_uri.user && proxy_uri.password 184 https = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password).new(url.host, url.port) 185 else 186 https = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new(url.host, url.port) 187 end 188 else 189 https = Net::HTTP.new(url.host, url.port) 190 end 191 https.use_ssl = true 192 https.verify_mode = @ssl_verify 193 https.verify_depth = 5 194 if @ssl_ca 195 https.ca_file = @ssl_ca 196 end 197 res = https.start {|http| http.request(req) } 198 case res 199 when Net::HTTPSuccess, Net::HTTPRedirection 200 res['Location'] 201 else 202 raise PostFailureException, res.inspect 203 end 204 end
pull_all()
click to toggle source
# File lib/gisty.rb 160 def pull_all 161 ids = local_ids 162 FileUtils.cd @dir do 163 ids.each do |id| 164 if File.exist? id 165 FileUtils.cd id do 166 c = "git pull" 167 Kernel.system c 168 end 169 end 170 end 171 end 172 end
remote_ids()
click to toggle source
# File lib/gisty.rb 98 def remote_ids 99 all_mygists.map { |i| i['id'] }.sort 100 end
sync(delete = false)
click to toggle source
# File lib/gisty.rb 133 def sync delete = false 134 local = local_ids 135 FileUtils.cd @dir do 136 r = all_mygists do |gist| 137 unless File.exists? gist['id'] 138 c = "git clone git@#{@base_uri}:#{gist['id']}.git" 139 Kernel.system c 140 end 141 local -= [gist['id']] 142 end 143 144 if local.size > 0 && delete 145 local.each do |id| 146 print "delete #{id}? [y/n]" 147 confirm = $stdin.gets.strip 148 if confirm == 'y' || confirm == 'yes' 149 puts "delete #{id}" 150 delete id 151 else 152 puts "skip #{id}" 153 end 154 end 155 end 156 open('meta.json', 'w') { |f| f.write JSON.pretty_generate(r) } 157 end 158 end