Class: SynonymScrapper::Datamuse

Inherits:
Scrapper
  • Object
show all
Defined in:
lib/synonym_scrapper/datamuse.rb

Overview

Scrapper for datamuse’s API

Constant Summary

Constants inherited from Scrapper

Scrapper::USER_AGENTS

Instance Attribute Summary

Attributes inherited from Scrapper

#base_url, #max_retries, #retries_left

Instance Method Summary collapse

Methods inherited from Scrapper

#call, #retry_call

Constructor Details

#initializeDatamuse

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, options = {})
	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