Class: IGist::CLI
- Inherits:
-
Object
- Object
- IGist::CLI
- Defined in:
- lib/igist/cli.rb
Instance Method Summary collapse
- #authorize ⇒ Object
- #clear ⇒ Object
- #index ⇒ Object
-
#initialize(igist = nil) ⇒ CLI
constructor
A new instance of CLI.
- #open_gist(id) ⇒ Object
- #print_gist(id) ⇒ Object
- #run ⇒ Object
- #search(options) ⇒ Object
Constructor Details
Instance Method Details
#authorize ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/igist/cli.rb', line 84 def puts "Authorize igist and fetch access token from Github." print "Github username: " username = $stdin.gets.strip print "Github password: " password = $stdin.noecho(&:gets).strip puts "" @igist.(username, password) puts "Authorized!" end |
#clear ⇒ Object
130 131 132 133 |
# File 'lib/igist/cli.rb', line 130 def clear @igist.clear puts "Your local index files are deleted!" end |
#index ⇒ Object
96 97 98 99 100 101 |
# File 'lib/igist/cli.rb', line 96 def index unless @igist.has_token? puts "Fetching and indexing your gists ..." @igist.index puts "Indexed #{@igist.my_gists.size} gists and #{@igist.starred_gists.size} starred gists!" end |
#open_gist(id) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/igist/cli.rb', line 122 def open_gist(id) gist = @igist.single_gist(id) command = RUBY_PLATFORM =~ /darwin/ ? 'open' : 'firefox' `#{command} #{gist["html_url"]}` rescue => e puts e. end |
#print_gist(id) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/igist/cli.rb', line 115 def print_gist(id) gist = @igist.single_gist(id) gist['files'].each_pair { |file, data| print_gist_file(file, data) } rescue => e puts e. end |
#run ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/igist/cli.rb', line 15 def run = {} opts = OptionParser.new do |opt| opt. = <<-EOS Index and search your gists on github. Note: Run "igist -i" before your first search or if your gists are changed. You can also "igist -i -s KEYWORD" to first index and then search. Usage: igist [-i|-u|-v] igist [-t|-a] -s KEYWORD igist -p ID Options: EOS opt.on("-v", "--version", "Show version") do puts VERSION exit end opt.on("-i", "--index", "Index your gists") do index exit end opt.on("-u", "--auth", "Authorize igist") do exit end opt.on("-s", "--search KEYWORD", "Search your own gists by keyword in description") do |keyword| [:search] = keyword end opt.on("-t", "--starred", "Only your starred gists") do [:starred] = true end opt.on("-a", "--all", "Both your gists and starred gists") do [:all] = true end opt.on("-p", "--print ID", "Print gist content by gist id") do |id| print_gist(id) exit end opt.on("-o", "--open ID", "Open gist in the browser") do |id| open_gist(id) exit end opt.on("--clear", "Remove your gists index data locally") do clear exit end end opts.parse! if [:search] search() end end |
#search(options) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/igist/cli.rb', line 103 def search() keyword = [:search] unless [:starred] print_result("My Own Gists", @igist.search(keyword)) end if [:all] or [:starred] print_result("My Starred Gists", @igist.search_starred(keyword)) end end |