Sha256: 47eb2174409cc61d80d4ef78f6e1f1f9998de0e3e4e9f90bd02b1cf99c3c3dae

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module GoogleWebTranslate
  class Result
    attr_reader :raw

    attr_reader :translation
    attr_reader :alternatives
    attr_reader :dictionary
    attr_reader :examples

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

      extract_data(:translation, 0, 0, 0) # dt:t
      extract_data(:alternatives, 5, 0, 2) # dt:at
      extract_data(:dictionary, 1) # dt: bd
      extract_data(:synonyms, 11) # dt:ss
      extract_data(:definitions, 12, 0) # dt:md
      extract_data(:examples, 13, 0) # dt:ex
      extract_data(:see_also, 14, 0) # dt:rw

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

    def to_h
      @properties
    end

    private

    def extract_data(name, *indices)
      value = array_value(@raw, *indices)
      return if value.nil?
      instance_variable_set("@#{name}", value)
      @keys.push(name)
    end

    def array_value(array, *indices)
      return nil if array.nil?
      index = indices.shift
      value = array[index]
      return value if indices.empty?
      array_value(value, *indices)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google_web_translate-0.1.0 lib/google_web_translate/result.rb