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.



13
14
15
16
17
18
19
20
21
# File 'lib/mw_dictionary_api/result.rb', line 13

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.



10
11
12
# File 'lib/mw_dictionary_api/result.rb', line 10

def api_type
  @api_type
end

#entriesObject (readonly)

Returns the value of attribute entries.



10
11
12
# File 'lib/mw_dictionary_api/result.rb', line 10

def entries
  @entries
end

#parser_classObject

Returns the value of attribute parser_class.



9
10
11
# File 'lib/mw_dictionary_api/result.rb', line 9

def parser_class
  @parser_class
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



10
11
12
# File 'lib/mw_dictionary_api/result.rb', line 10

def raw_response
  @raw_response
end

#response_formatObject (readonly)

Returns the value of attribute response_format.



10
11
12
# File 'lib/mw_dictionary_api/result.rb', line 10

def response_format
  @response_format
end

#suggestionsObject (readonly)

Returns the value of attribute suggestions.



10
11
12
# File 'lib/mw_dictionary_api/result.rb', line 10

def suggestions
  @suggestions
end

#termObject (readonly)

Returns the value of attribute term.



10
11
12
# File 'lib/mw_dictionary_api/result.rb', line 10

def term
  @term
end

Instance Method Details

#parse!Object



23
24
25
26
27
28
# File 'lib/mw_dictionary_api/result.rb', line 23

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



30
31
32
33
34
35
36
# File 'lib/mw_dictionary_api/result.rb', line 30

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

#to_jsonObject



38
39
40
# File 'lib/mw_dictionary_api/result.rb', line 38

def to_json
  to_hash.to_json
end