Class: SynonymScrapper::Datamuse
- Defined in:
- lib/synonym_scrapper/datamuse.rb
Overview
Scrapper for datamuse’s API
Constant Summary
Constants inherited from Scrapper
Instance Attribute Summary
Attributes inherited from Scrapper
#base_url, #max_retries, #retries_left
Instance Method Summary collapse
-
#build_call_url(word) ⇒ Object
Build the url to be called using this class’
base_urland aword. -
#initialize ⇒ Datamuse
constructor
Initialize the parent Scrapper Class.
-
#synonyms(word, options = {}) ⇒ Object
Obtain synonyms of a
wordfrom Datamuse.
Methods inherited from Scrapper
Constructor Details
#initialize ⇒ Datamuse
Initialize the parent Scrapper Class
14 15 16 |
# File 'lib/synonym_scrapper/datamuse.rb', line 14 def initialize() super(3, "https://api.datamuse.com/words?v=es&max=40&ml=") end |
Instance Method Details
#build_call_url(word) ⇒ Object
Build the url to be called using this class’ base_url and a word. Returns an url to where word‘s synonyms can be obtained.
22 23 24 |
# File 'lib/synonym_scrapper/datamuse.rb', line 22 def build_call_url(word) URI.parse(URI.escape(base_url + word)) end |
#synonyms(word, options = {}) ⇒ Object
Obtain synonyms of a word from Datamuse.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/synonym_scrapper/datamuse.rb', line 29 def synonyms(word, = {}) response = call(word).read synonyms = Array.new JSON.parse(response).each do |synonym| synonyms.push({ word: synonym["word"], score: synonym["score"] }) end return synonyms end |