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