Class: Thes

Inherits:
Object
  • Object
show all
Defined in:
lib/thes.rb,
lib/thes/version.rb

Constant Summary collapse

BASE_URL =
'http://www.thesaurus.com/browse/'.freeze
VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Thes

Returns a new instance of Thes.



12
13
14
# File 'lib/thes.rb', line 12

def initialize(query)
  @query = query
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/thes.rb', line 16

def call
  url     = URI.join(BASE_URL, CGI.escape(@query))
  html    = Nokogiri::HTML.parse(open(url))
  filters = html.search('.filters')

  tables = filters.map do |filter|
    type = filter.search('em.txt').text
    desc = filter.search('em.txt ~ strong').text

    groups = filter.search('.relevancy-block ul > li > a').group_by do |el|
      el.attr('data-category').match(/relevant-(\d+)/)[1].to_i
    end

    columns = groups.values.map do |els|
      els.map { |el| el.search('span.text').text }
    end

    columns.each(&:sort!)

    max_size = columns.max_by(&:size).size
    columns.each { |group| group.fill('', group.size, max_size - group.size) }

    rows = columns.transpose

    Terminal::Table.new(
      title: "#{type}: #{desc}",
      rows: rows,
      style: { width: 80 }
    )
  end

  tables.join("\n")
end