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.



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
# 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 :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



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

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

.get_default_configObject



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

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



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

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



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

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



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

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

#get_cheats(user, sheet_id) ⇒ Object



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

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

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



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

def process_args(user, topic, search_term, colorize)
  headers = ["User: #{user}"]
  headers << "Topic: #{topic}" if topic
  headers << "Search-Term: #{search_term}" if search_term
  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) if search_term

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

#run!Object



126
127
128
129
130
131
132
# File 'lib/grepg/parser.rb', line 126

def run!
  begin
    process_args(@user, @topic, @search_term, @colorize)
  rescue GrepPage::NotFoundError => ex
    abort "Error: #{ex.message}"
  end
end