Class: Glosbe::Response
- Inherits:
-
Object
- Object
- Glosbe::Response
- Defined in:
- lib/glosbe/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#ok ⇒ Object
(also: #ok?)
readonly
Returns the value of attribute ok.
Instance Method Summary collapse
- #authors ⇒ Object
- #define ⇒ Object
- #found? ⇒ Boolean
- #from ⇒ Object
-
#initialize(body, ok:) ⇒ Response
constructor
A new instance of Response.
- #message ⇒ Object
- #phrase ⇒ Object
- #results ⇒ Object
- #success? ⇒ Boolean
- #to ⇒ Object
- #translated_define ⇒ Object
- #translation ⇒ Object
Constructor Details
#initialize(body, ok:) ⇒ Response
Returns a new instance of Response.
5 6 7 8 |
# File 'lib/glosbe/response.rb', line 5 def initialize(body, ok:) @body = body || {} @ok = !!ok end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
3 4 5 |
# File 'lib/glosbe/response.rb', line 3 def body @body end |
#ok ⇒ Object (readonly) Also known as: ok?
Returns the value of attribute ok.
3 4 5 |
# File 'lib/glosbe/response.rb', line 3 def ok @ok end |
Instance Method Details
#authors ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/glosbe/response.rb', line 46 def @authors ||= begin if success? body["authors"].map { |data| Glosbe::Author.new(data[1]) } else [] end end end |
#define ⇒ Object
61 62 63 |
# File 'lib/glosbe/response.rb', line 61 def define @define ||= extract_definitions_for(from) end |
#found? ⇒ Boolean
32 33 34 |
# File 'lib/glosbe/response.rb', line 32 def found? success? && results.any? end |
#from ⇒ Object
12 13 14 |
# File 'lib/glosbe/response.rb', line 12 def from body["from"] end |
#message ⇒ Object
24 25 26 |
# File 'lib/glosbe/response.rb', line 24 def body["message"] end |
#phrase ⇒ Object
20 21 22 |
# File 'lib/glosbe/response.rb', line 20 def phrase body["phrase"] end |
#results ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/glosbe/response.rb', line 36 def results @results ||= begin if success? && body["tuc"].any? body["tuc"].map { |data| Glosbe::Result.new(data, authors: ) } else [] end end end |
#success? ⇒ Boolean
28 29 30 |
# File 'lib/glosbe/response.rb', line 28 def success? ok? && body["result"] == "ok" end |
#to ⇒ Object
16 17 18 |
# File 'lib/glosbe/response.rb', line 16 def to body["dest"] end |
#translated_define ⇒ Object
65 66 67 |
# File 'lib/glosbe/response.rb', line 65 def translated_define @translated_define ||= extract_definitions_for(to) end |
#translation ⇒ Object
56 57 58 59 |
# File 'lib/glosbe/response.rb', line 56 def translation result = results.find { |result| result.phrase } result.phrase if result end |