Class: GrepPage::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/grepg/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
14
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
# File 'lib/grepg/parser.rb', line 9

def initialize(args)
  default_config = self.class.get_default_config
  parser = Trollop::Parser.new do
    opt :user,
      "username",
      :type => :string,
      :default => default_config['user'],
      :short => "-u"
    opt :topic,
      "topic",
      :type => :string,
      :required => true,
      :short => "-t"
    opt :search,
      "text to search",
      :type => :string,
      :required => false,
      :short => "-s"
    opt :colorize,
      "colorize output",
      :type => :boolean,
      :default => default_config['colorize'],
      :short => "-c"
    version "grepg version #{GrepPage::VERSION}"
    banner <<-EOS

Usage:
  grepg -u user_name -t topic_name [-s search_term]

Examples:
  grepg -u evidanary -t css
  greppg -u evidanary -t css -s color

Defaults:
  To set defaults, create a file in ~/.grepg.yml with
  user: test
  colorize: true
    EOS
  end

  @opts = Trollop::with_standard_exception_handling parser do
    opts = parser.parse args
    # User is a required field, manually handling validation because
    # we fallback on defaults from the file
    raise Trollop::CommandlineError,
      "Missing --user parameter. e.g. --user evidanary" unless opts[:user]
    opts
  end

  @user = @opts[:user]
  @topic = @opts[:topic]
  @search_term = @opts[:search]
  @colorize = @opts[:colorize]
end

Class Method Details

.default_file_nameObject



69
70
71
72
# File 'lib/grepg/parser.rb', line 69

def self.default_file_name
  file = ENV['HOME'] + '/.grepg.yml'
  File.exist?(file) ? file : nil
end

.get_default_configObject



64
65
66
67
# File 'lib/grepg/parser.rb', line 64

def self.get_default_config
  file = self.default_file_name
  file ? YAML.load(IO.read(file)) : {}
end

Instance Method Details

#filter_cheats(cheats, search_term) ⇒ Object



88
89
90
91
92
93
# File 'lib/grepg/parser.rb', line 88

def filter_cheats(cheats, search_term)
  cheats.select do |cheat|
    (cheat[:description].downcase[search_term.downcase] ||
     cheat[:command].downcase[search_term.downcase]) != nil
  end
end

#filter_topics(topics, topic_name = '') ⇒ Object



78
79
80
81
82
# File 'lib/grepg/parser.rb', line 78

def filter_topics(topics, topic_name = '')
  sheet = topics.find{|topic| topic[:name].downcase == topic_name.downcase}
  sheet = topics.find{|topic| topic[:name].downcase[topic_name.downcase]} unless sheet
  sheet
end

#get_all_topics(user) ⇒ Object



74
75
76
# File 'lib/grepg/parser.rb', line 74

def get_all_topics(user)
  GrepPage::API.sheets(user)
end

#get_cheats(user, sheet_id) ⇒ Object



84
85
86
# File 'lib/grepg/parser.rb', line 84

def get_cheats(user, sheet_id)
  GrepPage::API.cheats(user, sheet_id)
end

#process_args(user, topic, search_term, colorize) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/grepg/parser.rb', line 95

def process_args(user, topic, search_term, colorize)
  headers = ["User: #{user}", "Topic: #{topic}"]
  headers << "Search-Term: #{search_term}" if search_term
  puts headers.join(", ")

  begin
    topics = get_all_topics(user)
  rescue RestClient::ResourceNotFound
    puts "That username does not exist"
    raise "Unable to find user"
  end

  topic = filter_topics(topics, topic)
  if topic.nil? || topic.empty?
    puts "Can't find that topic. Choose one of the following"
    puts topics.map{|t| t[:name]}
    raise "Unable to find topic"
  end

  cheats = get_cheats(user, topic[:id])
  cheats = filter_cheats(cheats, search_term) if search_term

  GrepPage::Formatter.cheat_rows(cheats, search_term, colorize)
end

#run!Object



120
121
122
# File 'lib/grepg/parser.rb', line 120

def run!
  process_args(@user, @topic, @search_term, @colorize)
end