Class: Glosbe::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/glosbe/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/glosbe/response.rb', line 3

def body
  @body
end

#okObject (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

#authorsObject



46
47
48
49
50
51
52
53
54
# File 'lib/glosbe/response.rb', line 46

def authors
  @authors ||= begin
    if success?
      body["authors"].map { |data| Glosbe::Author.new(data[1]) }
    else
      []
    end
  end
end

#defineObject



61
62
63
# File 'lib/glosbe/response.rb', line 61

def define
  @define ||= extract_definitions_for(from)
end

#found?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/glosbe/response.rb', line 32

def found?
  success? && results.any?
end

#fromObject



12
13
14
# File 'lib/glosbe/response.rb', line 12

def from
  body["from"]
end

#messageObject



24
25
26
# File 'lib/glosbe/response.rb', line 24

def message
  body["message"]
end

#phraseObject



20
21
22
# File 'lib/glosbe/response.rb', line 20

def phrase
  body["phrase"]
end

#resultsObject



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: authors) }
    else
      []
    end
  end
end

#success?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/glosbe/response.rb', line 28

def success?
  ok? && body["result"] == "ok"
end

#toObject



16
17
18
# File 'lib/glosbe/response.rb', line 16

def to
  body["dest"]
end

#translated_defineObject



65
66
67
# File 'lib/glosbe/response.rb', line 65

def translated_define
  @translated_define ||= extract_definitions_for(to)
end

#translationObject



56
57
58
59
# File 'lib/glosbe/response.rb', line 56

def translation
  result = results.find { |result| result.phrase }
  result.phrase if result
end