Sha256: 6834409bed1342a8dc19f8d918a3c21294e2383aa818e33dd0e111396e8268f1

Contents?: true

Size: 927 Bytes

Versions: 1

Compression:

Stored size: 927 Bytes

Contents

# -*- coding: utf-8 -*-

require 'open-uri'

module Dict
  class Dictionary
    attr_accessor :translations, :examples
    def initialize(word, url)
      check_arguments(word)
      @translations = []
      @examples = []
      @uri = URI(URI.escape(url + word.downcase.tr(' ', '_')))
    end

    def make_hash_results(arr)
      hash = arr.each_slice(2).inject({}) do |h, (key, value)|
        if h.has_key?(key)
          h[key].push(value) ; h
        else
          h[key] = [value] ; h
        end
      end
      @translations, @examples = hash.keys, hash.values
      hash
    end

    def check_arguments(word)
      raise ArgumentError.new("No given word") if word.empty?
    end

    def self.message
      'There\'s no such dictionary in database.'
    end

    class ConnectError < Exception
      attr_reader :original
      def initialize(original = $!)
        @original = original
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dict-0.2.1 lib/dict/dictionary.rb