Class: Gisty

Inherits:
Object
  • Object
show all
Defined in:
lib/gisty.rb

Defined Under Namespace

Classes: InvalidFileException, PostFailureException, UnsetAuthInfoException

Constant Summary collapse

VERSION =
'0.0.13'
GIST_URL =
'http://gist.github.com/'
GISTY_URL =
'http://github.com/swdyh/gisty/tree/master'
AA =

‘figlet -f contributed/bdffonts/clb8x8.flf gisty`.gsub(’#‘, ’m’)

"          mm                mm             \n                            mm             \nmmmmmm  mmmm      mmmmm   mmmmmm   mm  mm  \n mm   mm    mm     mm         mm     mm  mm  \n mm   mm    mm      mmmm      mm     mm  mm  \nmmmmmm    mm         mm     mm      mmmmm  \n    mm  mmmmmm   mmmmm       mmm       mm  \nmmmmm                               mmmm   \n"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, login = nil, token = nil) ⇒ Gisty

Returns a new instance of Gisty.



37
38
39
40
41
42
43
# File 'lib/gisty.rb', line 37

def initialize path,  = nil, token = nil
  @auth = ( && token) ? { 'login' => , 'token' => token } : auth
  raise UnsetAuthInfoException if @auth['login'].nil? || @auth['token'].nil?
  @auth_query = "login=#{@auth['login']}&token=#{@auth['token']}"
  @dir  = Pathname.pwd.realpath.join path
  FileUtils.mkdir_p @dir unless @dir.exist?
end

Class Method Details

.extract(url) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/gisty.rb', line 27

def self.extract url
  doc = Nokogiri::HTML open(url)
  {
    :id => url.split('/').last,
    :author => doc.css('#owner a').inner_text,
    :files => doc.css('.meta .info').map { |i| i.inner_text.strip },
    :clone => doc.css('a[rel="#git-clone"]').inner_text,
  }
end

.extract_ids(str) ⇒ Object



22
23
24
25
# File 'lib/gisty.rb', line 22

def self.extract_ids str
  doc = Nokogiri::HTML str
  doc.css('.file .info a').map { |i| i['href'].sub('/', '') }
end

Instance Method Details

#authObject



145
146
147
148
149
# File 'lib/gisty.rb', line 145

def auth
  user  = `git config --global github.user`.strip
  token = `git config --global github.token`.strip
  user.empty? ? {} : { 'login' => user, 'token' => token }
end

#build_params(paths) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/gisty.rb', line 161

def build_params paths
  list = (Array === paths ? paths : [paths]).map { |i| Pathname.new i }
  raise InvalidFileException if list.any?{ |i| !i.file? }

  params = {}
  list.each_with_index do |i, index|
    params["file_ext[gistfile#{index + 1}]"] = i.extname
    params["file_name[gistfile#{index + 1}]"] = i.basename.to_s
    params["file_contents[gistfile#{index + 1}]"] = IO.read(i)
  end
  params
end

#clone(id) ⇒ Object



69
70
71
72
73
74
# File 'lib/gisty.rb', line 69

def clone id
  FileUtils.cd @dir do
    c = "git clone [email protected]:#{id}.git"
    Kernel.system c
  end
end

#create(paths, opt = { :private => false }) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/gisty.rb', line 174

def create paths, opt = { :private => false }
  params = build_params paths
  if opt[:private]
    params['private'] = 'on'
  end
  post params.merge(auth)
end

#delete(id) ⇒ Object



92
93
94
# File 'lib/gisty.rb', line 92

def delete id
  FileUtils.rm_rf @dir.join(id) if @dir.join(id).exist?
end

#listObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/gisty.rb', line 76

def list
  dirs = Pathname.glob(@dir.to_s + '/*').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_idsObject



87
88
89
90
# File 'lib/gisty.rb', line 87

def local_ids
  dirs = Pathname.glob(@dir.to_s + '/*')
  dirs.map { |i| i.basename.to_s }
end

#map_pagesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gisty.rb', line 51

def map_pages
  result = []
  base_url = GIST_URL.sub(/\/$/, '')
  path = "/mine?page=1"
  loop do
    url = base_url + path + "&#{@auth_query}"
    page = open(url).read
    result << yield(url, page)
    path = next_link page
    break unless path
  end
  result
end


45
46
47
48
49
# File 'lib/gisty.rb', line 45

def next_link str
  doc = Nokogiri::HTML str
  a = doc.at('.pagination a[hotkey="l"]')
  a ? a['href'] : nil
end

#post(params) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/gisty.rb', line 151

def post params
  url = URI.parse('http://gist.github.com/gists')
  res = Net::HTTP.post_form(url, params)
  if res['Location']
    res['Location']
  else
    raise PostFailureException, res.inspect
  end
end

#pull_allObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/gisty.rb', line 131

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_idsObject



65
66
67
# File 'lib/gisty.rb', line 65

def remote_ids
  map_pages { |url, page| Gisty.extract_ids page }.flatten.uniq.sort
end

#sync(delete = false) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gisty.rb', line 96

def sync delete = false
  remote = remote_ids
  local  = local_ids

  if delete
    (local - remote).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
    ids = remote
  else
    ids = (remote + local).uniq
  end

  FileUtils.cd @dir do
    ids.each do |id|
      if File.exist? id
#           FileUtils.cd id do
#             c = "git pull"
#             Kernel.system c
#           end
      else
        c = "git clone [email protected]:#{id}.git"
        Kernel.system c
      end
    end
  end
end