Module: Slg
- Defined in:
- lib/slg.rb
Overview
This module provide some methods to scrape definitions from Slengo.it.
Constant Summary collapse
- WWW_ROOT =
"https://slengo.it"
Class Method Summary collapse
-
.format_results(results, color = true) ⇒ String
Format results for output.
- .open_cmd ⇒ Object
-
.open_url(term) ⇒ Nil
Open the search URL in the user’s browser.
-
.query(term, opts = {}) ⇒ Array<Hash>
Query the website and return a list of definitions for the provided term.
-
.search_url(term) ⇒ String
Get the search URL to query for a given term.
-
.version ⇒ String
The current gem’s version.
Class Method Details
.format_results(results, color = true) ⇒ String
Format results for output
92 93 94 |
# File 'lib/slg.rb', line 92 def format_results(results, color = true) UD::Formatting.text(results, color) end |
.open_cmd ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/slg.rb', line 96 def open_cmd case RbConfig::CONFIG["host_os"] when /darwin/ "open" when /bsd|linux/ "xdg-open" when /cygwin|mingw|mswin/ "start" end end |
.open_url(term) ⇒ Nil
Open the search URL in the user’s browser
35 36 37 |
# File 'lib/slg.rb', line 35 def open_url(term) system open_cmd, search_url(term) end |
.query(term, opts = {}) ⇒ Array<Hash>
Query the website and return a list of definitions for the provided term. This list may be empty if there’s no result.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/slg.rb', line 45 def query(term, opts = {}) url = search_url(term) text = OpenURI.open_uri url opts = { :count => 1 }.merge(opts || {}) return [] if opts[:count] <= 0 doc = Nokogiri::HTML.parse text doc.css("article.definition-card").map do |elt| examples = elt.css(".word-examples li").map(&:text).map(&:strip) example = examples.join "\n\n" votes = elt.css(".votes-container .v-progress-linear") # Slengo.it uses percentages instead of number of votes upvotes = votes.attr("aria-valuenow").to_s.to_i downvotes = 100 - upvotes header_p = elt.css("header p").first definition = elt.css("div.word-definition").text # Remove 'see also' definition.gsub! /\bCfr\..+/m, "" definition.strip! { # There's no :id nor :author # For some reason the selector 'h1.word-title' work in JS in the # browser but not with Nokogiri. :word => header_p.text.strip, :permalink => url, :definition => definition, :example => example, :upvotes => upvotes, :downvotes => downvotes, # TODO add region as well } end.take opts[:count] end |
.search_url(term) ⇒ String
Get the search URL to query for a given term.
26 27 28 29 |
# File 'lib/slg.rb', line 26 def search_url(term) param = URI.encode_www_form_component(term).gsub(/\+/, "%20") "#{WWW_ROOT}/define/#{param}" end |
.version ⇒ String
Returns the current gem’s version.
16 17 18 |
# File 'lib/slg.rb', line 16 def version "0.0.1" end |