Class: WordsApi::WordResponse
- Defined in:
- lib/words_api/models/word_response.rb
Overview
This custom type contains the response for word API.
Instance Attribute Summary collapse
-
#frequency ⇒ Float
The frequency of the word usage.
-
#pronunciation ⇒ Object
This model contains pronunciation details of a specific word.
-
#results ⇒ Array[WordDetails]
This field contains detailed information of the word.
-
#syllables ⇒ SyllableDetails
This custom type contains the syllable details for word API.
-
#word ⇒ String
The word that is searched.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Object
Creates an instance of the object from a hash.
-
.names ⇒ Object
A mapping from model property names to API property names.
-
.nullables ⇒ Object
An array for nullable fields.
-
.optionals ⇒ Object
An array for optional fields.
Instance Method Summary collapse
-
#initialize(word = nil, results = nil, pronunciation = SKIP, frequency = SKIP, syllables = SKIP) ⇒ WordResponse
constructor
A new instance of WordResponse.
Methods inherited from BaseModel
Constructor Details
#initialize(word = nil, results = nil, pronunciation = SKIP, frequency = SKIP, syllables = SKIP) ⇒ WordResponse
Returns a new instance of WordResponse.
57 58 59 60 61 62 63 64 |
# File 'lib/words_api/models/word_response.rb', line 57 def initialize(word = nil, results = nil, pronunciation = SKIP, frequency = SKIP, syllables = SKIP) @word = word @results = results @pronunciation = pronunciation unless pronunciation == SKIP @frequency = frequency unless frequency == SKIP @syllables = syllables unless syllables == SKIP end |
Instance Attribute Details
#frequency ⇒ Float
The frequency of the word usage.
26 27 28 |
# File 'lib/words_api/models/word_response.rb', line 26 def frequency @frequency end |
#pronunciation ⇒ Object
This model contains pronunciation details of a specific word.
22 23 24 |
# File 'lib/words_api/models/word_response.rb', line 22 def pronunciation @pronunciation end |
#results ⇒ Array[WordDetails]
This field contains detailed information of the word.
18 19 20 |
# File 'lib/words_api/models/word_response.rb', line 18 def results @results end |
#syllables ⇒ SyllableDetails
This custom type contains the syllable details for word API.
30 31 32 |
# File 'lib/words_api/models/word_response.rb', line 30 def syllables @syllables end |
#word ⇒ String
The word that is searched.
14 15 16 |
# File 'lib/words_api/models/word_response.rb', line 14 def word @word end |
Class Method Details
.from_hash(hash) ⇒ Object
Creates an instance of the object from a hash.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/words_api/models/word_response.rb', line 67 def self.from_hash(hash) return nil unless hash # Extract variables from the hash. word = hash.key?('word') ? hash['word'] : nil # Parameter is an array, so we need to iterate through it results = nil unless hash['results'].nil? results = [] hash['results'].each do |structure| results << (WordDetails.from_hash(structure) if structure) end end results = nil unless hash.key?('results') pronunciation = hash.key?('pronunciation') ? hash['pronunciation'] : SKIP frequency = hash.key?('frequency') ? hash['frequency'] : SKIP syllables = SyllableDetails.from_hash(hash['syllables']) if hash['syllables'] # Create object from extracted values. WordResponse.new(word, results, pronunciation, frequency, syllables) end |
.names ⇒ Object
A mapping from model property names to API property names.
33 34 35 36 37 38 39 40 41 |
# File 'lib/words_api/models/word_response.rb', line 33 def self.names @_hash = {} if @_hash.nil? @_hash['word'] = 'word' @_hash['results'] = 'results' @_hash['pronunciation'] = 'pronunciation' @_hash['frequency'] = 'frequency' @_hash['syllables'] = 'syllables' @_hash end |
.nullables ⇒ Object
An array for nullable fields
53 54 55 |
# File 'lib/words_api/models/word_response.rb', line 53 def self.nullables [] end |
.optionals ⇒ Object
An array for optional fields
44 45 46 47 48 49 50 |
# File 'lib/words_api/models/word_response.rb', line 44 def self.optionals %w[ pronunciation frequency syllables ] end |