Class: MWDictionaryAPI::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/mw_dictionary_api/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, raw_response, api_type: "sd4", response_format: "xml", parser_class: Parsers::ResultParser) ⇒ Result

Returns a new instance of Result.



10
11
12
13
14
15
16
17
18
# File 'lib/mw_dictionary_api/result.rb', line 10

def initialize(term, raw_response, api_type: "sd4", response_format: "xml", parser_class: Parsers::ResultParser)
  @term = term
  @raw_response = raw_response
  @api_type = api_type
  @response_format = response_format
  @parser_class = parser_class
  
  parse!
end

Instance Attribute Details

#api_typeObject (readonly)

Returns the value of attribute api_type.



7
8
9
# File 'lib/mw_dictionary_api/result.rb', line 7

def api_type
  @api_type
end

#entriesObject (readonly)

Returns the value of attribute entries.



7
8
9
# File 'lib/mw_dictionary_api/result.rb', line 7

def entries
  @entries
end

#parser_classObject

Returns the value of attribute parser_class.



6
7
8
# File 'lib/mw_dictionary_api/result.rb', line 6

def parser_class
  @parser_class
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



7
8
9
# File 'lib/mw_dictionary_api/result.rb', line 7

def raw_response
  @raw_response
end

#response_formatObject (readonly)

Returns the value of attribute response_format.



7
8
9
# File 'lib/mw_dictionary_api/result.rb', line 7

def response_format
  @response_format
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



7
8
9
# File 'lib/mw_dictionary_api/result.rb', line 7

def suggestions
  @suggestions
end

#termObject (readonly)

Returns the value of attribute term.



7
8
9
# File 'lib/mw_dictionary_api/result.rb', line 7

def term
  @term
end

Instance Method Details

#parse!Object



20
21
22
23
24
25
# File 'lib/mw_dictionary_api/result.rb', line 20

def parse!
  parser = parser_class.new(api_type: api_type, response_format: response_format)
  attributes = parser.parse(Nokogiri::XML(raw_response))
  @entries = attributes[:entries]
  @suggestions = attributes[:suggestions]
end

#to_hashObject



27
28
29
30
31
32
33
# File 'lib/mw_dictionary_api/result.rb', line 27

def to_hash
  {
    term: term,
    entries: entries,
    suggestions: suggestions
  }
end