Class: GrepPage::Parser
- Inherits:
-
Object
- Object
- GrepPage::Parser
- Defined in:
- lib/grepg/parser.rb
Class Method Summary collapse
Instance Method Summary collapse
- #filter_cheats(cheats, search_term, search_operator) ⇒ Object
- #filter_topics(topics, topic_name = '') ⇒ Object
- #get_all_topics(user) ⇒ Object
- #get_cheats(user, sheet_id) ⇒ Object
-
#initialize(args) ⇒ Parser
constructor
A new instance of Parser.
- #process_args ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize(args) ⇒ Parser
Returns a new instance of Parser.
8 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 63 64 65 66 67 |
# File 'lib/grepg/parser.rb', line 8 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 => false, :short => "-t" opt :search, "text to search", :type => :string, :required => false, :short => "-s" opt :search_operator, "One of: OR, AND", :type => :string, :default => default_config['search_operator'], :short => "-o" opt :colorize, "colorize output", :type => :boolean, :default => default_config['colorize'], :short => "-c" version "grepg version #{GrepPage::VERSION}" <<-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] @search_operator = (@opts[:search_operator] || "and").upcase.to_sym @colorize = @opts[:colorize].nil? ? true : @opts[:colorize] end |
Class Method Details
.default_file_name ⇒ Object
74 75 76 77 |
# File 'lib/grepg/parser.rb', line 74 def self.default_file_name file = ENV['HOME'] + '/.grepg.yml' File.exist?(file) ? file : nil end |
.get_default_config ⇒ Object
69 70 71 72 |
# File 'lib/grepg/parser.rb', line 69 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, search_operator) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/grepg/parser.rb', line 93 def filter_cheats(cheats, search_term, search_operator) cheats.select do |cheat| search_term.split(' ').map do |token| text_to_search = [ cheat[:description], cheat[:command] ].join(' ').downcase text_to_search.include? token.downcase end.reduce {|a,b| search_operator == :AND ? a && b : a || b} end end |
#filter_topics(topics, topic_name = '') ⇒ Object
83 84 85 86 87 |
# File 'lib/grepg/parser.rb', line 83 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
79 80 81 |
# File 'lib/grepg/parser.rb', line 79 def get_all_topics(user) GrepPage::API.sheets(user) end |
#get_cheats(user, sheet_id) ⇒ Object
89 90 91 |
# File 'lib/grepg/parser.rb', line 89 def get_cheats(user, sheet_id) GrepPage::API.cheats(user, sheet_id) end |
#process_args ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/grepg/parser.rb', line 104 def process_args headers = ["User: #{@user}"] headers << "Topic: #{@topic}" if @topic if(@search_term) label = "Search-Term: #{@search_term}" label += "(#{@search_operator})" if @search_term.split(' ').count > 1 headers << label end puts headers.join(", ") begin topics = get_all_topics(@user) rescue RestClient::ResourceNotFound raise GrepPage::NotFoundError, "Unable to find user" end unless @topic # No topic specified so show all topics puts "Available Topics => " puts topics.map{|t| t[:name]} return 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 GrepPage::NotFoundError, "Unable to find topic" end cheats = get_cheats(@user, topic[:id]) cheats = filter_cheats(cheats, @search_term, @search_operator) if @search_term GrepPage::Formatter.cheat_rows(cheats, @search_term, @colorize) end |
#run! ⇒ Object
140 141 142 143 144 145 146 |
# File 'lib/grepg/parser.rb', line 140 def run! begin process_args rescue GrepPage::NotFoundError => ex abort "Error: #{ex.}" end end |