Class: SL::DefinitionSearch
- Inherits:
-
Object
- Object
- SL::DefinitionSearch
- Defined in:
- lib/searchlink/searches/definition.rb
Overview
Dictionary Definition Search
Class Method Summary collapse
-
.define(terms) ⇒ Array
Searches for a definition of the given terms.
-
.search(_, search_terms, link_text) ⇒ Array
Searches for a definition of the given terms.
-
.settings ⇒ Hash
Returns a hash of settings for the search.
Class Method Details
.define(terms) ⇒ Array
Searches for a definition of the given terms
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/searchlink/searches/definition.rb', line 50 def define(terms) def_url = "https://www.wordnik.com/words/#{terms.url_encode}" curl = TTY::Which.which('curl') body = `#{curl} -sSL '#{def_url}'` if body =~ /id="define"/ first_definition = body.match(%r{(?mi)(?:id="define"[\s\S]*?<li>)([\s\S]*?)</li>})[1] parts = first_definition.match(%r{<abbr title="partOfSpeech">(.*?)</abbr> (.*?)$}) return [def_url, "(#{parts[1]}) #{parts[2]}".gsub(%r{</?.*?>}, '').strip] end false rescue StandardError false end |
.search(_, search_terms, link_text) ⇒ Array
Searches for a definition of the given terms
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/searchlink/searches/definition.rb', line 31 def search(_, search_terms, link_text) fix = SL.spell(search_terms) if fix && search_terms.downcase != fix.downcase SL.add_error('Spelling', "Spelling altered for '#{search_terms}' to '#{fix}'") search_terms = fix link_text = fix end url, title = define(search_terms) url ? [url, title, link_text] : [false, false, link_text] end |
.settings ⇒ Hash
Returns a hash of settings for the search
11 12 13 14 15 16 17 18 19 |
# File 'lib/searchlink/searches/definition.rb', line 11 def settings { trigger: 'def(?:ine)?', searches: [ ['def', 'Dictionary Definition'], ['define', nil] ] } end |