Skip to content

Instantly share code, notes, and snippets.

@yannvery
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save yannvery/cac1694880ecf7dce3fa to your computer and use it in GitHub Desktop.

Select an option

Save yannvery/cac1694880ecf7dce3fa to your computer and use it in GitHub Desktop.
Launch it and search string on your gists
require 'octokit'
require 'open-uri'
class Gist
attr_reader :description, :html_url
def initialize params
@id = params[:id]
@description = params[:description]
@html_url = params[:html_url]
@files = []
params[:files].each do |file|
@files << File.new(filename: file[1].filename, raw_url: file[1].raw_url)
end
end
def files
@files
end
def self.search gists, search
results = []
gists.each do |gist|
results << gist if gist.contains? search
end
results
end
def contains? search
if [email protected]?
return true if @description.include? search
end
@files.each do |file|
return true if file.contains? search
end
false
end
end
class File
def initialize params
@name = params[:filename]
@url = params[:raw_url]
@content =""
get_content
end
def get_content
@content = open(@url){|f| f.read}
end
def contains? search
if [email protected]?
return true if @content.include? search
end
false
end
def content
@content
end
end
print "\rEnter your username : "
username = gets.chomp
print "\rSearch ? "
search = gets.chomp
puts "... Searching ...\r"
Octokit.auto_paginate = true
user = Octokit.user username
gists = Octokit.gists user.login
gg = []
gists.each do |g|
gg << Gist.new(g)
end
results = Gist.search gg, search
results.each do |r|
puts "#{r.description} : #{r.html_url}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment