Module: WebHelper

Defined in:
lib/lyrix.rb

Class Method Summary collapse

Class Method Details

.fetch_lucky_content_for(msg) ⇒ Object

does a google lucky search for msg and returns a readablity parsed output



56
57
58
59
60
# File 'lib/lyrix.rb', line 56

def self.fetch_lucky_content_for(msg)
  url = self.get_google_lucky_url(msg)
  source = self.get_source_from_url(url)
  content = self.get_content_from(source)
end

.get_content_from(source) ⇒ Object

parses source with readibility and returns the content



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lyrix.rb', line 82

def self.get_content_from(source)
  content = Readability::Document.new(source, tags: %w[br div p]).content

  # TODO: refactor blacklist html tag substitution
  blacklisted_tags = [
    '<div>', '<p>', '</div>',  '</p>'
  ]
  blacklisted_tags.each do |tag|
    content.gsub! tag, ''
  end
  content.gsub! '<br>', "\n"
  
  return content
end

.get_google_lucky_url(msg) ⇒ Object

does a google lucky search for msg, returns a URL



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lyrix.rb', line 63

def self.get_google_lucky_url(msg)
  uri=URI.parse "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s"\
        % URI.escape(msg)
  req = Net::HTTP::Get.new(uri.request_uri)
  http = Net::HTTP.new(uri.host)
  http.read_timeout = 5
  http.open_timeout = 5
  res = http.start { |server|
      server.request(req)
  }
  JSON.parse(res.body)['responseData']['results'][0]['url']
end

.get_source_from_url(url) ⇒ Object

visits the url and returns the page’s source



77
78
79
# File 'lib/lyrix.rb', line 77

def self.get_source_from_url(url)
  open(url).read
end