Module: G

Defined in:
lib/g-module/version.rb,
lib/g-module.rb

Overview

:nodoc:

Defined Under Namespace

Modules: VERSION

Constant Summary collapse

C =

build-in default values. Use ‘UserPreferences’ to overwrite

{
    :count  => 4,                       # number of results showed
    :indent => 8, :tw => 70,            # indentation and descripton width 
    :goog   => "http://www.google.com", # where to ask?
    :open   => "system 'open \"${url}\"'",  # loads into HTTP browser on Mac OSX
    #:count  => "puts ${count}",
}
HOMEDIR =

try loading user preference from “~/.g” where the windows/mac cross platform home directory is defined as:

(
    ENV["HOME"] or ENV["USERPROFILE"] or 
    (File.directory?("h:/") ? "h:" : "c:")
).gsub(/\\/, "/")
UserPreference =

overwrite ‘C’ defaults with settings from this/your yaml preference file.

File.join(HOMEDIR, ".g")

Class Method Summary collapse

Class Method Details

.call(action, param) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/g-module.rb', line 57

def call(action, param)
    cmd = C[action]
    param.each_pair do |k,v|
        dputs "#{k} --> #{v}"
        cmd = cmd.gsub("\$\{#{k}\}", param[k.to_sym])
    end
    dputs "--#{cmd}--"
    eval cmd
end

.configureObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/g-module.rb', line 131

def configure
    class << C # pimp-my-hash as first seen on why/redhanded 
        def method_missing(m,*a)
            if m.to_s =~ /=$/
                self[$`.to_sym] = a[0]
            elsif a.empty?
                self[m]
            else
                raise NoMethodError, "#{m}"
            end
        end
    end

    # overwrite defaults with user preferences
    c = YAML.load(File.read(C.cfile)) rescue {}
    C.merge!(c) 
    #puts "after: #{C.inspect}"
end

.count(search_tokens) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/g-module.rb', line 81

def count(search_tokens)
    doc = G.query(search_tokens) 
    e = (doc/"table tr td[@nowrap]").find("error in #{self}") do |e| 
        e.inner_text =~ /Results 1 - / 
    end
    count = Integer((e/"b")[2].inner_text.gsub(/,/,'_'))
    puts "#{count} results for #{search_tokens.inspect}"
    count
end

.dputs(*msg) ⇒ Object



55
# File 'lib/g-module.rb', line 55

def dputs *msg; puts *msg if $DEBUG; end

.fight(search_tokens) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/g-module.rb', line 91

def fight(search_tokens)
    left, *right = *search_tokens
    left_count = count([left])
    right_count = count(right)
    puts "warn: superfluous words added on right side" if 1 < right.size 
    puts "#{left} #{left_count} : #{right_count} #{right.join ' '}"
    [left_count, right_count]
end

.index_page(search_tokens) ⇒ Object



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
125
126
127
128
129
# File 'lib/g-module.rb', line 100

def index_page(search_tokens)

    doc = G.query(search_tokens) 
    entries = (doc/"//div[@class='g']").map do |e|
        a = e.at(:a)
        txt = ""
        e.at(:font).children.find do |t| 
            txt << t.inner_text;  
            t.name == "br" rescue false;
        end
        #:txt = e.at(:font).search("*".."br").map {|t| t.inner_text}.join(" "),
        { :title => a.inner_text, :link => a["href"], :txt => txt, }
    end


    entries.each_with_index do |e, i|
        indent = " " * G::C.indent
        puts "\#{\"%d\" % (i+1)}: \"\#{e[:title]}\"\n   URL: \#{e[:link]}\n\n\#{indent}\#{G.reformat_wrapped(e[:txt], G::C.tw).gsub(/\\n/, \"\\n\#{indent}\")}\n    EOT\n    end\n\n    print \"open which? \"\n    idx = Integer($stdin.gets) rescue 1\n    url = entries[idx-1][:link]\n    G.call :open, :url => url\nend\n"

.lucky(search_tokens) ⇒ Object



75
76
77
78
79
# File 'lib/g-module.rb', line 75

def lucky(search_tokens)
    doc = G.query(search_tokens) 
    url = (doc/"div[@class='g'] a").first["href"]
    G.call :open, :url => url
end

.query(words) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/g-module.rb', line 67

def query words
    q = words.map { |w| CGI.escape(w) }.join("+")
    url = "#{C.goog}/search?q=#{q}&num=#{C.count}"
    dputs "G query: #{url}"
    #File.open("/tmp/g", "a") {|fd| fd.write(open(url).read) }
    @doc = Hpricot(open(url).read)
end

.reformat_wrapped(s, width = 78) ⇒ Object

helper: wrap text to width character per line



51
52
53
# File 'lib/g-module.rb', line 51

def reformat_wrapped(s, width=78)
    s.gsub(/\s+/, " ").gsub(/(.{1,#{width}})( |Z)/, "\\1\n")
end