hermes-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 641902ce8949b32a7215d788dd4b09d4048c9a9254200a13494303a8da673eb4
4
+ data.tar.gz: e97c423aabe0d3ee6d4a72377553db8a764baacc0d5e6fed7e0c8c83ed625497
5
+ SHA512:
6
+ metadata.gz: 02340e4f170170d08230d3432cea10e8df5e2c650c6535cc97add84dcd4bf178eb1fb6ee94a330ec75db5915be15172f89f5ef1698eecf984409c3e8ca7fd37a
7
+ data.tar.gz: 693a4ff2ea986bec345530ae498e2c21e1f3a544f773b046f146beee3c58b16207b7b5f924e148a3751f2a532e44845fbf7cbd04453558d2634d2b9021028902
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,28 @@
1
+ #AllCops:
2
+ # Include:
3
+ # - "**/*.gemspec"
4
+ # - "**/*.podspec"
5
+ # - "**/*.jbuilder"
6
+ # - "**/*.rake"
7
+ # - "**/*.opal"
8
+ # - "**/Gemfile"
9
+ # - "**/Rakefile"
10
+ # - "**/Capfile"
11
+ # - "**/Guardfile"
12
+ # - "**/Podfile"
13
+ # - "**/Thorfile"
14
+ # - "**/Vagrantfile"
15
+ # - "**/Berksfile"
16
+ # - "**/Cheffile"
17
+ # - "**/Vagabondfile"
18
+ # Exclude:
19
+ # - "vendor/**/*"
20
+ # - "**/schema.rb"
21
+
22
+ Metrics/LineLength:
23
+ Max: 120
24
+
25
+ Metrics/BlockLength:
26
+ Exclude:
27
+ - "hermes-ruby.gemspec"
28
+ - "spec/**/*.rb"
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ hermes-ruby
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.1
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in hermes-ruby.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Boris Drazhzhov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # HermesRuby
2
+
3
+ Allows to translate text from/to different languages using open API.
4
+
5
+ Yandex Translate API is supported only.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hermes-ruby'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hermes-ruby
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'hermes-ruby'
27
+
28
+ # Create Yandex Translate client
29
+ # API Key can be obtained here https://translate.yandex.com/developers/keys
30
+ client = HermesRuby::Yandex.new('you_api_key')
31
+
32
+ # List of supported languages
33
+ client.get_langs
34
+
35
+ # Attempt to detect text language
36
+ client.detect('Hello!', [:en, :de])
37
+
38
+ # Text translation
39
+ client.translate('Hello!', 'en-ru')
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'hermes-ruby/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'hermes-ruby'
9
+ spec.version = HermesRuby::VERSION
10
+ spec.authors = ['Boris Drazhzhov']
11
+ spec.email = ['bdrazhzhov@gmail.com']
12
+
13
+ spec.summary = 'Allows to translate text from/to different languages using open API.'
14
+ spec.description = 'It uses translation API of Google, Microsoft, Yandex and other ones for translation. '\
15
+ 'You can chose one of them and use. Unfortunately Yandex API is supported only.'
16
+ spec.homepage = 'https://github.com/bdrazhzhov/hermes-ruby'
17
+ spec.license = 'MIT'
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.16'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0.58.0'
29
+ spec.add_dependency 'querystring', '~> 0.1.0'
30
+ spec.add_dependency 'rest-client', '~> 2.0'
31
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hermes-ruby/version'
4
+ require 'hermes-ruby/yandex'
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HermesRuby
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rest-client'
4
+ require 'querystring'
5
+ require 'json'
6
+
7
+ module HermesRuby
8
+ # HermesRuby::Yandex class needs to be used
9
+ # for making calls to the Yandex Translator API.
10
+ class Yandex
11
+ # @param api_key - can be obtained at https://translate.yandex.com/developers/keys.
12
+ def initialize(api_key)
13
+ @base_url = 'https://translate.yandex.net/api/v1.5/tr.json'
14
+ @api_key = api_key
15
+ end
16
+
17
+ # Gets a list of translation directions supported by the service.
18
+ # @param ui_lang - the language code that specifies a response language.
19
+ def get_langs(ui_lang = 'en')
20
+ call_api({ ui: ui_lang }, 'getLangs')
21
+ end
22
+
23
+ # Detects the language of the specified text.
24
+ # @param text - the text to detect the language for.
25
+ # @param hint - array of symbols containing language codes.
26
+ # of the most likely languages, for example [:en, :de].
27
+ def detect(text, hint = nil)
28
+ query_obj = { text: text }
29
+ query_obj[:hint] = hint if hint
30
+ result = call_api(query_obj, 'detect')
31
+
32
+ result['lang']
33
+ end
34
+
35
+ # Translates text to the specified language.
36
+ # @param text - the text to translate.
37
+ # @param lang - the translation direction.
38
+ # You can set it in either of the following ways:
39
+ # - As a pair of language codes separated by a hyphen ("from"-"to").
40
+ # For example, en-ru indicates translating from English to Russian.
41
+ # - As the target language code (for example, ru).
42
+ # In this case, the service tries to detect the source language automatically.
43
+ # Default value: 'en'.
44
+ # @param format - text format.
45
+ # Possible values:
46
+ # - plain - Text without markup (default value).
47
+ # - html - Text in HTML format.
48
+ def translate(text, lang = 'en', format = 'plain')
49
+ query_obj = { text: text, lang: lang, format: format }
50
+ result = call_api(query_obj, 'translate')['text']
51
+
52
+ result.first
53
+ end
54
+
55
+ # Makes calls to Yandex Translator API.
56
+ # @param query_obj - a set of key value pairs containing params passing to the API.
57
+ # @param method_name - an API method name that should be called,
58
+ # for example, 'detect', 'translate'.
59
+ def call_api(query_obj, method_name)
60
+ query_obj[:key] = @api_key
61
+ query_string = QueryString.stringify(query_obj)
62
+ headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
63
+
64
+ begin
65
+ result = RestClient.post("#{@base_url}/#{method_name}", query_string, headers: headers)
66
+ return JSON.parse(result.body)
67
+ rescue RestClient::RequestFailed => e
68
+ raise e.response.body
69
+ end
70
+ end
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hermes-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Boris Drazhzhov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.58.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.58.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: querystring
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.1.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rest-client
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ description: It uses translation API of Google, Microsoft, Yandex and other ones for
98
+ translation. You can chose one of them and use. Unfortunately Yandex API is supported
99
+ only.
100
+ email:
101
+ - bdrazhzhov@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".rubocop.yml"
109
+ - ".ruby-gemset"
110
+ - ".ruby-version"
111
+ - ".travis.yml"
112
+ - Gemfile
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - hermes-ruby.gemspec
117
+ - lib/hermes-ruby.rb
118
+ - lib/hermes-ruby/version.rb
119
+ - lib/hermes-ruby/yandex.rb
120
+ homepage: https://github.com/bdrazhzhov/hermes-ruby
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.7.7
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Allows to translate text from/to different languages using open API.
144
+ test_files: []