module Slg

This module provide some methods to scrape definitions from Slengo.it.

Constants

WWW_ROOT

Public Class Methods

format_results(results, color = true) click to toggle source

Format results for output @param results [Array] this must be an array of results, as returned by

+Slg.query+.

@param color [Boolean] colored output @return [String]

# File lib/slg.rb, line 86
def format_results(results, color = true)
  Defcli.format_results(results, color)
end
open_url(term) click to toggle source

Open the search URL in the user's browser @param term [String] the term to search for. It must be a string, spaces

are allowed.

@return [Nil]

# File lib/slg.rb, line 30
def open_url(term)
  Defcli.open_in_browser search_url(term)
end
query(term, opts = {}) click to toggle source

Query the website and return a list of definitions for the provided term. This list may be empty if there's no result. @param term [String] the term to search for @param opts [Hash] options. This is used by the command-line tool.

+:count+ is the maximum number of results to return

@return [Array<Hash>]

# File lib/slg.rb, line 40
def query(term, opts = {})
  url = search_url(term)
  text = Defcli.read_url 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 do |ex|
      ex.text.strip
    end

    votes = elt.css(".votes-container .v-progress-linear")
    # Slengo.it uses percentages instead of number of votes
    ratio = votes.attr("aria-valuenow").to_s.to_f / 100.0

    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,
      :examples => examples,
      :ratio => ratio,
      # TODO add region as well
    }

  end.take opts[:count]
end
search_url(term) click to toggle source

Get the search URL to query for a given term. @param term [String] the term to search for. It must be a string, spaces

are allowed.

@return [String]

# File lib/slg.rb, line 21
def search_url(term)
  param = URI.encode_www_form_component(term).gsub(/\+/, "%20")
  "#{WWW_ROOT}/define/#{param}"
end
version() click to toggle source

@return [String] the current gem's version

# File lib/slg.rb, line 11
def version
  "0.0.2"
end