Module: GistNuke
Constant Summary collapse
- BASE_URL =
"https://api.github.com/"
- AUTH_EXT =
"authorizations"
Instance Method Summary collapse
- #construct_hydra(range) ⇒ Object
- #delete_range(numbers = []) ⇒ Object
- #gather_pages(range) ⇒ Object
- #get_github_token(credentials = {}) ⇒ Object
- #just_keys(gists = []) ⇒ Object
- #load_gists(page_number = 1) ⇒ Object
- #login ⇒ Object
- #save_auth_token(token) ⇒ Object
- #token_from_file ⇒ Object
Instance Method Details
#construct_hydra(range) ⇒ Object
102 103 104 105 106 107 |
# File 'lib/gist_nuke.rb', line 102 def construct_hydra(range) t = token_from_file @batch = Typhoeus::Hydra.new range.map { |gist_id| @batch.queue(Typhoeus::Request.new("#{BASE_URL}gists/#{gist_id}?access_token=#{t}", method: :delete))} end |
#delete_range(numbers = []) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/gist_nuke.rb', line 93 def delete_range(numbers = []) numbers = numbers.map { |num| num.to_i } range = (numbers[0]..numbers[-1]) list = gather_pages(range) list[range] construct_hydra(list[range]) @batch.run end |
#gather_pages(range) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gist_nuke.rb', line 79 def gather_pages(range) keys = [] page_number = 1 last_page = false until keys.length >= range.last || last_page to_append = just_keys(load_gists(page_number)) last_page = to_append.empty? keys += to_append page_number += 1 end keys end |
#get_github_token(credentials = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/gist_nuke.rb', line 29 def get_github_token(credentials = {}) uri = URI(BASE_URL + AUTH_EXT) req = Net::HTTP::Post.new(uri.path) req.content_type = "application/json" req.body = JSON.dump({ scopes: [:gist], note: "Batch delete your gists, you know you want to." }) req.basic_auth(credentials[:username], credentials[:password]) res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http| http.request(req) end JSON.parse(res.body)['token'] end |
#just_keys(gists = []) ⇒ Object
75 76 77 |
# File 'lib/gist_nuke.rb', line 75 def just_keys(gists = []) gists.map { |g| g['id'] } end |
#load_gists(page_number = 1) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/gist_nuke.rb', line 62 def load_gists(page_number = 1) token = token_from_file uri = URI("#{BASE_URL}gists?access_token=#{token}&page=#{page_number}") Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| request = Net::HTTP::Get.new uri.request_uri response = http.request(request) JSON.parse(response.body) end end |
#login ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gist_nuke.rb', line 12 def login credentials = {} puts "Let's get an Oauth Token from GitHub" print "Enter your username: " credentials[:username] = $stdin.gets.chomp print "Enter your password: " credentials[:password] = begin `stty -echo` rescue nil $stdin.gets.chomp ensure `stty echo` rescue nil end puts "" token = get_github_token(credentials) save_auth_token(token) end |
#save_auth_token(token) ⇒ Object
56 57 58 59 60 |
# File 'lib/gist_nuke.rb', line 56 def save_auth_token(token) File.open(".gist_nuke", "w+") do |file| file.write(token) end end |
#token_from_file ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/gist_nuke.rb', line 48 def token_from_file if File.exists?('.gist_nuke') File.read('.gist_nuke') else false end end |