class Glosbe::Response

Attributes

body[R]
ok[R]
ok?[R]

Public Class Methods

new(body, ok:) click to toggle source
# File lib/glosbe/response.rb, line 5
def initialize(body, ok:)
  @body = body || {}
  @ok = !!ok
end

Public Instance Methods

authors() click to toggle source
# 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
define() click to toggle source
# File lib/glosbe/response.rb, line 61
def define
  @define ||= extract_definitions_for(from)
end
found?() click to toggle source
# File lib/glosbe/response.rb, line 32
def found?
  success? && results.any?
end
from() click to toggle source
# File lib/glosbe/response.rb, line 12
def from
  body["from"]
end
message() click to toggle source
# File lib/glosbe/response.rb, line 24
def message
  body["message"]
end
phrase() click to toggle source
# File lib/glosbe/response.rb, line 20
def phrase
  body["phrase"]
end
results() click to toggle source
# 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?() click to toggle source
# File lib/glosbe/response.rb, line 28
def success?
  ok? && body["result"] == "ok"
end
to() click to toggle source
# File lib/glosbe/response.rb, line 16
def to
  body["dest"]
end
translated_define() click to toggle source
# File lib/glosbe/response.rb, line 65
def translated_define
  @translated_define ||= extract_definitions_for(to)
end
translation() click to toggle source
# File lib/glosbe/response.rb, line 56
def translation
  result = results.find { |result| result.phrase }
  result.phrase if result
end

Private Instance Methods

extract_definitions_for(language) click to toggle source
# File lib/glosbe/response.rb, line 71
def extract_definitions_for(language)
  results.map do |result|
    result.meanings.map do |meaning|
      meaning.text if meaning.language == language
    end
  end.flatten.compact
end