Class: Synonymous::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/synonymous/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word, response) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/synonymous/response.rb', line 7

def initialize(word, response)
  data = JSON.parse(response.body)

  if data.all? { |entry| entry.is_a?(Hash) }
    entries = data.map { |result| Entry.new(result) }
    @entries, suggestions = entries.partition { |entry| entry.headword.to_s == word }
    @suggestions = suggestions.map { |entry| entry.headword.to_s }
  else
    @entries = []
    @suggestions = data
  end
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



5
6
7
# File 'lib/synonymous/response.rb', line 5

def entries
  @entries
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



5
6
7
# File 'lib/synonymous/response.rb', line 5

def suggestions
  @suggestions
end

Instance Method Details

#success?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/synonymous/response.rb', line 20

def success?
  entries.any?
end