Class: IGist::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/igist/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(igist = nil) ⇒ CLI

Returns a new instance of CLI.



11
12
13
# File 'lib/igist/cli.rb', line 11

def initialize(igist=nil)
  @igist = igist || IGist.new
end

Instance Method Details

#authorizeObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/igist/cli.rb', line 84

def authorize
  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.authorize(username, password)
  puts "Authorized!"
end

#clearObject



130
131
132
133
# File 'lib/igist/cli.rb', line 130

def clear
  @igist.clear
  puts "Your local index files are deleted!"
end

#indexObject



96
97
98
99
100
101
# File 'lib/igist/cli.rb', line 96

def index
  authorize 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.message
end


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.message
end

#runObject



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
  options = {}
  opts = OptionParser.new do |opt|

    opt.banner = <<-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
      authorize
      exit
    end

    opt.on("-s", "--search KEYWORD", "Search your own gists by keyword in description") do |keyword|
      options[:search] = keyword
    end

    opt.on("-t", "--starred", "Only your starred gists") do
      options[:starred] = true
    end

    opt.on("-a", "--all", "Both your gists and starred gists") do
      options[: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 options[:search]
    search(options)
  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(options)
  keyword = options[:search]

  unless options[:starred]
    print_result("My Own Gists", @igist.search(keyword))
  end

  if options[:all] or options[:starred]
    print_result("My Starred Gists", @igist.search_starred(keyword))
  end
end