yatran 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yatran.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Igor Fedoronchuk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Yatran
2
+
3
+ Ruby unofficial client for Yandex translation engine
4
+
5
+
6
+ Examples
7
+
8
+
9
+ 1.9.2p320 :001 > require 'yatran'
10
+ => true
11
+ 1.9.2p320 :002 > "Hello world".lang
12
+ => "en"
13
+ 1.9.2p320 :003 > "Hello world".language
14
+ => "en"
15
+ 1.9.2p320 :004 > "Hello world".en_ru
16
+ => "Привет, мир!"
17
+ 1.9.2p320 :005 > "Hello world".en_ru.ru_fr
18
+ => "Bonjour tout le monde!"
19
+ 1.9.2p320 :006 > "Hello world".en_ru.ru_it
20
+ => "Ciao, mondo!"
21
+ 1.9.2p320 :007 > "Hello world".en_ru.ru_it.language
22
+ => "it"
23
+ 1.9.2p320 :008 > Yatran::LANGUAGES_TRANSLATIONS
24
+ => ["ru-en", "ru-pl", "ru-uk", "ru-de", "ru-fr", "ru-es", "ru-it", "ru-tr", "en-ru", "en-uk", "en-tr", "pl-ru", "uk-ru", "uk-en", "de-ru", "fr-ru", "es-ru", "it-ru", "tr-ru", "tr-en"]
25
+ 1.9.2p320 :009 > "Hello world".respond_to?("en_ru")
26
+ => true
27
+ 1.9.2p320 :010 > "Hello world".respond_to?("from_en_to_ru")
28
+ => true
29
+ 1.9.2p320 :011 > "Hello world".respond_to?("to_ru_from_en")
30
+ => true
31
+
32
+
33
+
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ gem 'yatran'
40
+
41
+ And then execute:
42
+
43
+ $ bundle
44
+
45
+ Or install it yourself as:
46
+
47
+ $ gem install yatran
48
+
49
+ ## Usage
50
+
51
+ Terms of use (free access, up to 10,000 request a day or 1,000,000 requested characters)
52
+ At the moment it supports the following lang pairs:
53
+ http://translate.yandex.net/api/v1/tr/getLangs
54
+
55
+ More info
56
+
57
+ English version: http://translate.yandex.net/tr-url/en+ru.ru/api.yandex.ru/translate/ (tranlated automatically)
58
+ Russian version: http://api.yandex.ru/translate/
59
+
60
+
61
+ ## Contributing
62
+
63
+ 1. Fork it
64
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
65
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
66
+ 4. Push to the branch (`git push origin my-new-feature`)
67
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ require "bundler/gem_tasks"
4
+ Bundler::GemHelper.install_tasks
data/lib/yatran/api.rb ADDED
@@ -0,0 +1,49 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+
25
+
26
+
27
+ module Yatran
28
+
29
+ class API
30
+
31
+ def self.request(method, args = {})
32
+ url = URI.parse("#{YATRAN_URL}#{method}")
33
+ response = Net::HTTP.post_form(url, args).body
34
+ begin
35
+ response = JSON(response)
36
+ rescue
37
+ raise Error response
38
+ end
39
+ if response['code']
40
+ code = response['code'].to_i
41
+ if ERROR_CODES.key?(code)
42
+ raise Error.new(ERROR_CODES[code])
43
+ end
44
+
45
+ end
46
+ response
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,32 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module Yatran
24
+
25
+ module Detectable
26
+
27
+ def language
28
+ @yatran_lang ||= API.request('detect', {:text=>self})['lang']
29
+ end
30
+ alias_method 'lang', 'language'
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ module Yatran
25
+
26
+ ERROR_CODES = {413 => :ERR_TEXT_TOO_LONG, 422 =>:ERR_UNPROCESSABLE_TEXT, 501 => :ERR_LANG_NOT_SUPPORTED }
27
+
28
+ class Error < StandardError
29
+ end
30
+
31
+ end
@@ -0,0 +1,25 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module Yatran
24
+ LANGUAGES_TRANSLATIONS = API.request('getLangs')['dirs']
25
+ end
@@ -0,0 +1,86 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ module Yatran
25
+
26
+ module Translatable
27
+
28
+
29
+ def translate(translation_direction)
30
+ #URI::encode(
31
+ API.request('translate', {:lang=> translation_direction, :text=>self})['text'][0]
32
+
33
+ end
34
+
35
+
36
+
37
+ # Overrides @method_missing@ to define and call dynamic translation methods.
38
+ #
39
+ # @private
40
+ #
41
+ def method_missing(method, *args, &block)
42
+
43
+ translation = translation_direction(method.to_s)
44
+
45
+ if translation.nil?
46
+ super(method, *args, &block)
47
+ else
48
+ self.class.send(:define_method, method.to_s) { translate translation}
49
+ translate translation
50
+ end
51
+
52
+ end
53
+
54
+
55
+
56
+ # Overrides @respond_to?@ to use the dynamic translation methods.
57
+ #
58
+ # @private
59
+
60
+ def respond_to?(method, include_private = false)
61
+ return true unless translation_direction(method.to_s).nil?
62
+ super(method, include_private)
63
+ end
64
+
65
+
66
+
67
+ def translation_direction(method)
68
+
69
+ case method
70
+ when /^to_(.*)_from_(.*)$/
71
+ method = "#{$2}-#{$1}"
72
+ when /^from_(.*)_to_(.*)$/
73
+ method = "#{$1}-#{$2}"
74
+
75
+ else
76
+ method["_"] = "-"
77
+ end
78
+
79
+ method = nil unless LANGUAGES_TRANSLATIONS.include? method
80
+ method
81
+
82
+ end
83
+
84
+
85
+ end
86
+ end
data/lib/yatran/url.rb ADDED
@@ -0,0 +1,25 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module Yatran
24
+ YATRAN_URL = "http://translate.yandex.net/api/v1/tr.json/"
25
+ end
@@ -0,0 +1,25 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module Yatran
24
+ VERSION = "0.0.1"
25
+ end
data/lib/yatran.rb ADDED
@@ -0,0 +1,41 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ require 'json'
24
+ require 'net/http'
25
+ require 'uri'
26
+
27
+ require "yatran/version"
28
+ require "yatran/error"
29
+ require "yatran/url"
30
+ require "yatran/api"
31
+ require "yatran/languages"
32
+
33
+
34
+
35
+
36
+ require "yatran/translatable"
37
+ require "yatran/detectable"
38
+
39
+ String.send :include, Yatran::Translatable
40
+ String.send :include, Yatran::Detectable
41
+
data/yatran.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require File.expand_path('../lib/yatran/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.authors = ["Igor"]
7
+ gem.email = ["fedoronchuk@gmail.com"]
8
+ gem.description = %q{unofficial http://api.yandex.ru/translate API Client for ruby }
9
+ gem.summary = %q{ automatic translations and language detecting }
10
+ gem.homepage = "https://github.com/Fivell/yatran"
11
+
12
+ gem.files = `git ls-files`.split($\)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "yatran"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = Yatran::VERSION
18
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yatran
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'unofficial http://api.yandex.ru/translate API Client for ruby '
15
+ email:
16
+ - fedoronchuk@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/yatran.rb
27
+ - lib/yatran/api.rb
28
+ - lib/yatran/detectable.rb
29
+ - lib/yatran/error.rb
30
+ - lib/yatran/languages.rb
31
+ - lib/yatran/translatable.rb
32
+ - lib/yatran/url.rb
33
+ - lib/yatran/version.rb
34
+ - yatran.gemspec
35
+ homepage: https://github.com/Fivell/yatran
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: automatic translations and language detecting
59
+ test_files: []