class Gripst::Gripst

Constants

ParamObj

Attributes

auth_token[R]
tmpdir[R]

Public Class Methods

new() click to toggle source
# File lib/gripst/gripst.rb, line 14
def initialize
  @auth_token = retrieve_auth_token
  @tmpdir = Dir.mktmpdir
end

Public Instance Methods

all_gist_ids() click to toggle source
# File lib/gripst/gripst.rb, line 23
def all_gist_ids
  client.gists.map(&:id)
end
clone(id) click to toggle source
# File lib/gripst/gripst.rb, line 27
def clone(id)
  Git.clone "https://#{auth_token}@gist.github.com/#{id}.git", id, :path => "#{tmpdir}"
  true
rescue => e # TODO figure out what error this is could be and not rescue bare exception
  $stderr.puts "ERROR: git fell down on #{id}"
  $stderr.puts "ERROR: #{e}"
  false
end
initialized?() click to toggle source
# File lib/gripst/gripst.rb, line 19
def initialized?
  !!auth_token
end
run(regex, id) click to toggle source
# File lib/gripst/gripst.rb, line 36
def run(regex, id)
  Find.find("#{tmpdir}/#{id}") do |path|
    param_obj = ParamObj.new(id, path)
    return Find.prune if git_dir? param_obj
    loop_through_lines_of_a_gist(regex, param_obj) if File.file? path
  end if clone id
end

Private Instance Methods

client() click to toggle source
# File lib/gripst/gripst.rb, line 46
def client
  @client ||= Client.login_with_oauth auth_token
end
display_match(param_obj, line) click to toggle source
# File lib/gripst/gripst.rb, line 61
def display_match(param_obj, line)
  puts "#{output_info_string(param_obj)} #{line}"
end
extract_gistfile_name(path) click to toggle source
# File lib/gripst/gripst.rb, line 73
def extract_gistfile_name(path)
  path.split('/')[-1].yellow
end
git_dir?(obj) click to toggle source
# File lib/gripst/gripst.rb, line 65
def git_dir?(obj)
  obj.path == "#{tmpdir}/#{obj.id}/.git"
end
loop_through_lines_of_a_gist(regex, param_obj) click to toggle source
# File lib/gripst/gripst.rb, line 50
def loop_through_lines_of_a_gist(regex, param_obj)
  File.new(param_obj.path).each do |line|
    begin
      display_match(param_obj, line) if /#{regex}/.match line
    rescue ArgumentError
      $stderr.puts "Skipping... #{output_info_string(param_obj)} #{$!}"
      sleep 300
    end
  end
end
output_info_string(param_obj) click to toggle source
# File lib/gripst/gripst.rb, line 69
def output_info_string(param_obj)
  "#{param_obj.id.pink} (#{extract_gistfile_name(param_obj.path)})"
end
retrieve_auth_token() click to toggle source
# File lib/gripst/gripst.rb, line 77
def retrieve_auth_token
  return ENV['GITHUB_USER_ACCESS_TOKEN'] if ENV['GITHUB_USER_ACCESS_TOKEN']
  Client.get_auth_token
end