dict 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/bin/translate +1 -2
- data/lib/google.rb +19 -0
- data/lib/wiktionary.rb +40 -0
- metadata +25 -5
    
        data/bin/translate
    CHANGED
    
    | @@ -2,14 +2,13 @@ | |
| 2 2 | 
             
            # -*- coding: utf-8 -*-
         | 
| 3 3 |  | 
| 4 4 | 
             
            require 'dict'
         | 
| 5 | 
            +
            require 'slop'
         | 
| 5 6 |  | 
| 6 7 | 
             
            if ARGV.empty?
         | 
| 7 8 | 
             
              puts "Please, enter the word to translate."
         | 
| 8 9 | 
             
              exit
         | 
| 9 10 | 
             
            end
         | 
| 10 11 |  | 
| 11 | 
            -
            puts Dict::Translation.getResponse(ARGV[0])
         | 
| 12 | 
            -
             | 
| 13 12 | 
             
            if ARGV[0] == "status"
         | 
| 14 13 | 
             
              print "Status API: "
         | 
| 15 14 | 
             
              print Dict::Translation.status
         | 
    
        data/lib/google.rb
    ADDED
    
    | @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            require 'net/https'
         | 
| 2 | 
            +
            require 'uri'
         | 
| 3 | 
            +
            require 'json'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            API_KEY = 'AIzaSyB2EQUnNtZycF_xyoyOSm0QJU0dfMGEr44'
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            class GoogleTranslator
         | 
| 8 | 
            +
              def initialize(uri)
         | 
| 9 | 
            +
                @uri = URI(uri)
         | 
| 10 | 
            +
                @req = Net::HTTP::Get.new(@uri.path << "?" << @uri.query)
         | 
| 11 | 
            +
                Net::HTTP.start(@uri.host, @uri.port, :use_ssl => @uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
         | 
| 12 | 
            +
                  p JSON.parse(http.request(@req).body)
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            word = ARGV[0]
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            GoogleTranslator.new("https://www.googleapis.com/language/translate/v2?key=#{API_KEY}&q=#{word}&source=pl&target=en")
         | 
    
        data/lib/wiktionary.rb
    ADDED
    
    | @@ -0,0 +1,40 @@ | |
| 1 | 
            +
            require 'net/http' # requires 'uri'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            class Wiktionary
         | 
| 4 | 
            +
            	WIKI_URL = "http://en.wiktionary.org/wiki/" # #{word}
         | 
| 5 | 
            +
            	def initialize(word)
         | 
| 6 | 
            +
            		if word.empty? then puts "No word given."; exit end
         | 
| 7 | 
            +
            		@word = word
         | 
| 8 | 
            +
            		@uri = URI(URI.escape(WIKI_URL << @word))
         | 
| 9 | 
            +
            	end
         | 
| 10 | 
            +
            	
         | 
| 11 | 
            +
            	def translate
         | 
| 12 | 
            +
            		req = Net::HTTP::Get.new(@uri.path)
         | 
| 13 | 
            +
            		response, array = nil, []
         | 
| 14 | 
            +
            		Net::HTTP.start(@uri.host, @uri.port) do |http|
         | 
| 15 | 
            +
            			response = http.request(req).body
         | 
| 16 | 
            +
            			regexp = Regexp.new('<li>[^<]*<a href="/wiki/([^"]+)"\s+title="\1">\1<\/a>', Regexp::IGNORECASE)
         | 
| 17 | 
            +
            			results = regexp.match(response)
         | 
| 18 | 
            +
            			
         | 
| 19 | 
            +
            			return array if !results # no translation found
         | 
| 20 | 
            +
            			
         | 
| 21 | 
            +
            			array.push results[1]
         | 
| 22 | 
            +
            		end
         | 
| 23 | 
            +
            	end
         | 
| 24 | 
            +
            	
         | 
| 25 | 
            +
            	def uri_debug
         | 
| 26 | 
            +
            		puts "--- DEBUG start ---"
         | 
| 27 | 
            +
            		puts "uri:\t\t" << @uri.to_s
         | 
| 28 | 
            +
            		puts "scheme:\t\t" << @uri.scheme
         | 
| 29 | 
            +
            		puts "host:\t\t" << @uri.host
         | 
| 30 | 
            +
            		puts "query:\t\t" << @uri.query if @uri.query
         | 
| 31 | 
            +
            		puts "path:\t\t" << @uri.path if @uri.path
         | 
| 32 | 
            +
            		puts "fragment:\t" << @uri.fragment if @uri.fragment
         | 
| 33 | 
            +
            		puts "--- DEBUG end ---"
         | 
| 34 | 
            +
            	end
         | 
| 35 | 
            +
            end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
            word = ARGV[0]
         | 
| 38 | 
            +
            word ||= ''
         | 
| 39 | 
            +
            translation = Wiktionary.new word
         | 
| 40 | 
            +
            puts translation.translate.to_s
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: dict
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.4
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -11,17 +11,37 @@ autorequire: | |
| 11 11 | 
             
            bindir: bin
         | 
| 12 12 | 
             
            cert_chain: []
         | 
| 13 13 | 
             
            date: 2012-07-03 00:00:00.000000000 Z
         | 
| 14 | 
            -
            dependencies: | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 14 | 
            +
            dependencies:
         | 
| 15 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 16 | 
            +
              name: slop
         | 
| 17 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 18 | 
            +
                none: false
         | 
| 19 | 
            +
                requirements:
         | 
| 20 | 
            +
                - - ! '>='
         | 
| 21 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 22 | 
            +
                    version: '0'
         | 
| 23 | 
            +
              type: :runtime
         | 
| 24 | 
            +
              prerelease: false
         | 
| 25 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 26 | 
            +
                none: false
         | 
| 27 | 
            +
                requirements:
         | 
| 28 | 
            +
                - - ! '>='
         | 
| 29 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 30 | 
            +
                    version: '0'
         | 
| 31 | 
            +
            description: ! "    Dict is a client of API which you lets you use multiple dictionaries.\n
         | 
| 32 | 
            +
              \   Dict is a OpenSource Project.\n"
         | 
| 33 | 
            +
            email:
         | 
| 34 | 
            +
            - mtczerwinski@gmail.com
         | 
| 17 35 | 
             
            executables:
         | 
| 18 36 | 
             
            - translate
         | 
| 19 37 | 
             
            extensions: []
         | 
| 20 38 | 
             
            extra_rdoc_files: []
         | 
| 21 39 | 
             
            files:
         | 
| 22 40 | 
             
            - lib/dict.rb
         | 
| 41 | 
            +
            - lib/google.rb
         | 
| 42 | 
            +
            - lib/wiktionary.rb
         | 
| 23 43 | 
             
            - bin/translate
         | 
| 24 | 
            -
            homepage: 
         | 
| 44 | 
            +
            homepage: https://github.com/Ragnarson/dict-gem
         | 
| 25 45 | 
             
            licenses: []
         | 
| 26 46 | 
             
            post_install_message: 
         | 
| 27 47 | 
             
            rdoc_options: []
         |