Class: Ruri::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/ruri/search.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

BASE_URL =
"http://doc.ruby-lang.org"
SEARCH_URL =
"#{BASE_URL}/ja/search/version:%s/query:%s/"
DEFAULT_VERSION =
'1.9.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.last_queryObject

Returns the value of attribute last_query.



15
16
17
# File 'lib/ruri/search.rb', line 15

def last_query
  @last_query
end

.last_resultObject

Returns the value of attribute last_result.



15
16
17
# File 'lib/ruri/search.rb', line 15

def last_result
  @last_result
end

Class Method Details

.open(uri) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/ruri/search.rb', line 25

def open(uri)
  puts "\e[34m=> #{uri}\e[0m"
  if web_page_cache.key?(uri)
    web_page_cache[uri]
  else
    web_page_cache[uri] = Kernel.open(uri).read
  end
end

.open_reference(result) ⇒ Object



61
62
63
64
# File 'lib/ruri/search.rb', line 61

def open_reference(result)
  return nil unless result
  parse_reference(open(result.url))
end

.parse_reference(html) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruri/search.rb', line 66

def parse_reference(html)
  doc = Nokogiri::HTML(html)
  doc.css('title').remove
  first_p = doc.css('p').first
  first_p.content = "\e[34m#{first_p.content.gsub("\n", " ").strip}\e[0m\n"
  doc.css('h1').each {|e| e.content = "#{e.content.strip}\n#{'=' * 80}\n"}
  doc.css('h2').each {|e| e.content = "#{e.content.strip}\n#{'-' * 80}\n"}
  doc.css('h3').each {|e| e.content = "### #{e.content.strip}\n"}
  doc.css('h4').each {|e| e.content = "#### #{e.content.strip}\n"}
  doc.css('dt.method-heading').each {|e| e.content = "\e[36m#{e.content.strip}\e[0m\n"}
  doc.css('dd').each {|e| e.content = "\n    #{e.content.strip.gsub("\n", "\n    ")}\n"}
  doc.css('pre').each {|e| e.content = "\n#{e.content.strip}\n"}
  doc.content.gsub(/\n[\n\s]*\n/, "\n\n").strip
end

.parse_search_result(html) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruri/search.rb', line 46

def parse_search_result(html)
  result = []
  doc = Nokogiri::HTML(html)
  doc.css('.entries').each do |dl|
    [
      dl.css('.entry-name .signature'),
      dl.css('.entry-document .entry-link a'),
      dl.css('.entry-summary')
    ].transpose.each do |name, link, summary|
      result << Result.new(name.content.strip, BASE_URL + link[:href], summary.content.strip)
    end
  end
  result
end

.search(query, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruri/search.rb', line 34

def search(query, options = {})
  options = {:version => DEFAULT_VERSION, :memory => false}.merge(options)
  url = SEARCH_URL % [options[:version], ERB::Util.url_encode(query)]
  content = open(url)
  result = parse_search_result(content)
  if options[:memory]
    self.last_query = query
    self.last_result = result
  end
  result
end

.web_page_cacheObject



21
22
23
# File 'lib/ruri/search.rb', line 21

def web_page_cache
  @web_page_cache ||= {}
end