Sha256: 8f74b7f3eb013f44e17264cb8bba71e2516648ad9f115947041c2437b5f37aec

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

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

# It is a base class for classes fetching results from Web dictionaries.

require 'open-uri'
require 'dict/result'

module Dict
  class Dictionary

    def initialize(word)
      check_arguments(word)
      @word = downcase_word(word)
      @result = Dict::Result.new(@word)
    end
    
    # Returns an instance of URI::HTTP class.
    def uri(url, word = nil)
      word == nil ? URI(URI.escape(url)) : URI(URI.escape(url + word.downcase.tr(' ', '_')))
    end

    # Checks if word was given correctly.
    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
    
    private
    # Returns a word with all downcased letters, including polish
    def downcase_word(word)
      word.downcase.gsub(/[ĄĆĘŁŃÓŚŹŻ]/, 'Ą' => 'ą', 'Ć' => 'ć', 'Ę' => 'ę', 'Ł' => 'ł', 'Ń' => 'ń', 'Ó' => 'ó', 'Ś' => 'ś', 'Ź' => 'ź', 'Ż' => 'ż')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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