Class: WordsApi::WordResponse

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/words_api/models/word_response.rb

Overview

This custom type contains the response for word API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#to_hash, #to_json

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

#frequencyFloat

The frequency of the word usage.

Returns:

  • (Float)


26
27
28
# File 'lib/words_api/models/word_response.rb', line 26

def frequency
  @frequency
end

#pronunciationObject

This model contains pronunciation details of a specific word.

Returns:

  • (Object)


22
23
24
# File 'lib/words_api/models/word_response.rb', line 22

def pronunciation
  @pronunciation
end

#resultsArray[WordDetails]

This field contains detailed information of the word.

Returns:



18
19
20
# File 'lib/words_api/models/word_response.rb', line 18

def results
  @results
end

#syllablesSyllableDetails

This custom type contains the syllable details for word API.

Returns:



30
31
32
# File 'lib/words_api/models/word_response.rb', line 30

def syllables
  @syllables
end

#wordString

The word that is searched.

Returns:

  • (String)


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

.namesObject

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

.nullablesObject

An array for nullable fields



53
54
55
# File 'lib/words_api/models/word_response.rb', line 53

def self.nullables
  []
end

.optionalsObject

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