Class: Aiuto

Inherits:
Object
  • Object
show all
Includes:
Aiuto_Helper
Defined in:
lib/aiuto.rb,
lib/klass.rb

Constant Summary

Constants included from Aiuto_Helper

Aiuto_Helper::LIST

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Aiuto

Returns a new instance of Aiuto.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/klass.rb', line 4

def initialize(obj)
  case obj
  when String
    @obj = obj
  when Method
    @obj = obj.receiver + "#" + obj.name
  when Class, Module
    @obj = obj.to_s
  else
    @obj = obj.to_s
    @obj = @obj.class.to_s if @obj.to_s[0..1] == "<#"
  end
end

Class Method Details

.cli(*args) ⇒ Object



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
# File 'lib/aiuto.rb', line 17

def self.cli(*args)
  args = args[0]
  case args[0]
  when "--help"
    print "===== AIUTO HELP ======\n"
    print "Welcome to Aiuto. Simply write \"aiuto Ruby is awesome\" and I will do a search for you on Google on some Ruby blog or website.\n"
    print "Keep in mind that Google permits a maximum search of 32 words (OR is not counted as a word).\n\n"
    print "-f, --filter \t Change filter followed by \"blog\", \"git\", \"docs\", \"social\", \"utility\", \"rails\", \"beginner\", or \"advanced\".\n"
    print "\t Like this: \"aiuto 'rack queue' -f blog git docs.\n\n"
    print "-no, --no-filter \t Use plain google\n\n"
    print "-r, --no-ruby \t Remove word ruby\n\n"
    print "--list \t List of the used libraries\n\n"
    print "Aiuto means Help in Italian! :D\n\n"
    print "========================\n"
  when "--list"
    print "========= AIUTO LIST ===========\n"
    Aiuto_Helper::LIST.each do |k,v|
      next if [:all, :no, :main].include?(k)
      print "#{k.to_s.upcase}: \n"
      to_table(v)
    end
    print "================================\n"
  else
    words = []
    blogs = []
    to_blog = false
    to_word = true
    no = false
    ruby_word = true
    args.each do |word|
      case word
      when "-f", "--filter"
        to_blog = true
        to_word = false
      when "-no", "--no-filter"
        to_blog = false
        to_word = false
        no = true
      when "-r", "--no-ruby"
        ruby_word = false
      else
        if to_word
          words << word
        elsif to_blog
          blogs << word
        end
      end
    end
    blogs = [:main] if blogs.empty?
    words = words.join(" ")
    blogs = blogs.map(&:to_sym)
    Aiuto.new(words).google(type: blogs, no: no, ruby: ruby_word)
  end
end

.to_table(arr, splice = 3) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/aiuto.rb', line 6

def self.to_table(arr, splice=3)
  l = []
  arr = arr.map{|a| a += ", "}
  arr[-1][-1] = ""
  arr[-1][-1] = ""
  arr = arr.each_slice(splice).to_a
  arr.each{|a| a[0] = "   " + a[0]}
  arr.each{|r|r.each_with_index{|f,i| l[i] = [l[i]||0, f.length].max}}
  arr.each{|r|r.each_with_index{|f,i| print "#{f.ljust l[i]} "};puts ""}
end

Instance Method Details

#google(type: [], no: false, ruby: true) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/klass.rb', line 18

def google(type: [], no: false, ruby: true)
  sites = []
  type = [type] unless type.is_a?(Array)
  type = [] if no
  type.each do |t|
    sites |= LIST[t.to_sym]
  end
  sites = sites.map{|s| "site:"+s}.join(" OR ")
  url = "www.google.com/search?q="+sites
  url += " ruby" if ruby
  url += " "+@obj.gsub("#", "%23")
  Launchy.open(url)
end