Class: Cleaner

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/gist_cleaner.rb

Instance Method Summary collapse

Constructor Details

#initialize(username, password, otpcode) ⇒ Cleaner

Returns a new instance of Cleaner.



34
35
36
37
38
# File 'lib/gist_cleaner.rb', line 34

def initialize(username, password, otpcode)
	@username = username
	@password = password
	@otpcode = otpcode
end

Instance Method Details

#check_gistsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gist_cleaner.rb', line 40

def check_gists()
  self.class.base_uri 'https://api.github.com'
  self.class.basic_auth @username, @password
  if @otpcode.empty?
  	@gists = self.class.get("/users/#{@username}/gists", headers: {"User-Agent" => @username})

	if !@gists["message"].empty?
		abort @gists["message"]
	end

  	puts @gists
  	return
  	public_gists = @gists.select { |g| g["public"] == true }
	puts "you have #{public_gists.size} public gist"
  else
  	@gists = self.class.get("/users/#{@username}/gists", headers: {"User-Agent" => @username, "X-GitHub-OTP" => @otpcode})
  	public_gists = @gists.select { |g| g["public"] == true }
	puts "you have #{public_gists.size} public gist && #{@gists.size - public_gists.size} private gist"
  end
end

#delete_gistsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/gist_cleaner.rb', line 61

def delete_gists()
 	puts "clean gist 1.private 2.public 3.all. please input mode number: "
	clean_mode = gets.chomp.to_i
	if clean_mode < 1 || clean_mode > 3
		puts "invalid clean mode"
		return
	end
	puts 'enabled two-factor authentication? input optcode OR press the enter to continue'
	@otpcode = gets.chomp
	case clean_mode
	when 1
	  puts "clean private gists"
		i = 0
   	@gists.each do |g|
   		if g["public"] == false
   			self.delete_gists_action(g, i)
     		i = i + 1
   		end
   	end
	when 2
	  puts "clean public gists"
		i = 0
   	@gists.each do |g|
   		if g["public"] == true
   			self.delete_gists_action(g, i)
     		i = i + 1
   		end
   	end
	when 3
	  puts "clean all gists"
	  i = 0
   	@gists.each do |g|
   		self.delete_gists_action(g, i)
     	i = i + 1
   	end
	else
		puts "invalid clean mode"
	end
end

#delete_gists_action(g, i) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/gist_cleaner.rb', line 101

def delete_gists_action(g, i)
	id = g["id"]
	puts "#{i+1}. DELETING: https://gist.github.com/#{@username}/#{id}"
	if @otpcode.empty?
		self.class.delete("/gists/#{id}", headers: {"User-Agent" => @username})
	else
		self.class.delete("/gists/#{id}", headers: {"User-Agent" => @username, "X-GitHub-OTP" => @otpcode})
	end
end