Class: GoogleWebTranslate::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/google_web_translate/result.rb

Overview

Translation results

Constant Summary collapse

DATA_INDICES =
{
  translation: [0, 0, 0], # dt:t
  alternatives: [5, 0, 2], # dt:at
  dictionary: [1], # dt: bd
  synonyms: [11], # dt:ss
  definitions: [12, 0], # dt:md
  examples: [13, 0], # dt:ex
  see_also: [14, 0], # dt:rw
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Result

Returns a new instance of Result.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/google_web_translate/result.rb', line 19

def initialize(data)
  @raw = data
  @keys = []
  @properties = {}

  DATA_INDICES.each do |key, indices|
    indices = indices.dup
    extract_data(key, *indices)
  end

  @alternatives = @alternatives.collect { |i| i[0] } if @alternatives
  @keys.each { |key| @properties[key] = instance_variable_get("@#{key}") }
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



4
5
6
# File 'lib/google_web_translate/result.rb', line 4

def raw
  @raw
end

Instance Method Details

#to_hObject



33
34
35
# File 'lib/google_web_translate/result.rb', line 33

def to_h
  @properties
end