google_translater 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Guardfile +16 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/google_translater.gemspec +32 -0
- data/lib/google_translater/translater.rb +132 -0
- data/lib/google_translater/version.rb +3 -0
- data/lib/google_translater.rb +5 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3cd812f92216cbc564e0dd52a34f789dca9ef822
|
4
|
+
data.tar.gz: 250d4f5ae8b357dbf068056c38bff9f924ce9fe4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c6372f09cc01d2875339f0e9ebdaad46f9984ddb85d37434a2b64e22b694712d0b1262edeae04b7cde81829f24c53664af5dc8e8d14bdc2245aebf38bfff6d0e
|
7
|
+
data.tar.gz: 8068889d1f1414b889ec6b190a8a34f9b3022d1d1cb51c833add31b64c41229abe90c51f98041438e3ffe339a539431ce2e4af68ab85801ebe0a80e4401c31e6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
guard :rspec, cmd: 'rspec' do
|
2
|
+
# watch /lib/ files
|
3
|
+
watch(%r{^lib/(.+).rb$}) do |m|
|
4
|
+
"spec/#{m[1]}_spec.rb"
|
5
|
+
end
|
6
|
+
|
7
|
+
# watch /spec/ files
|
8
|
+
watch(%r{^spec/(.+).rb$}) do |m|
|
9
|
+
"spec/#{m[1]}.rb"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
guard :rubocop, all_on_start: false, notification: true do
|
14
|
+
watch(%r{^lib/(.+).rb$})
|
15
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
16
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Terry S
|
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,41 @@
|
|
1
|
+
# GoogleTranslater
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/google_translater`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'google_translater'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install google_translater
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/google_translater.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "google_translater"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'google_translater/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'google_translater'
|
8
|
+
spec.version = GoogleTranslater::VERSION
|
9
|
+
spec.authors = ["Terry S"]
|
10
|
+
spec.email = ["itsterry@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Provides a Ruby object to translate phrases using Google}
|
13
|
+
spec.description = %q{Provides a Ruby object to translate phrases using Google}
|
14
|
+
spec.homepage = "https://github.com/morsedigital/google_translater"
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.13'
|
25
|
+
spec.add_development_dependency 'guard'
|
26
|
+
spec.add_development_dependency 'guard-rspec'
|
27
|
+
spec.add_development_dependency 'guard-rubocop'
|
28
|
+
spec.add_development_dependency 'pry-byebug'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
|
+
spec.add_development_dependency 'rspec-nc'
|
32
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
|
3
|
+
# Translater
|
4
|
+
class Translater # rubocop:disable Metrics/ClassLength
|
5
|
+
class << self
|
6
|
+
attr_reader :from_language
|
7
|
+
attr_reader :input
|
8
|
+
attr_reader :to_language
|
9
|
+
|
10
|
+
def google_base_url
|
11
|
+
'https://translate.googleapis.com/translate_a/single'
|
12
|
+
end
|
13
|
+
|
14
|
+
def google_params
|
15
|
+
"client=gtx&sl=#{from_language}&tl=#{to_language}&dt=t&q=#{input}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def google_response
|
19
|
+
uri = URI google_url
|
20
|
+
Net::HTTP.get_response uri
|
21
|
+
end
|
22
|
+
|
23
|
+
def google_url
|
24
|
+
[google_base_url, google_params].join('?')
|
25
|
+
end
|
26
|
+
|
27
|
+
def languages_match?
|
28
|
+
to_language == from_language
|
29
|
+
end
|
30
|
+
|
31
|
+
def parsed_google_response
|
32
|
+
google_response.body.gsub(/\[|\]|"/, '').split(',').first
|
33
|
+
end
|
34
|
+
|
35
|
+
def recognized?(language)
|
36
|
+
language_map.key?(language)
|
37
|
+
end
|
38
|
+
|
39
|
+
def translate(input, from_language, to_language)
|
40
|
+
@input = input
|
41
|
+
@from_language = from_language.to_sym
|
42
|
+
@to_language = to_language.to_sym
|
43
|
+
return input if languages_match?
|
44
|
+
return unknown_languages_message if unknown_languages?
|
45
|
+
translated_input
|
46
|
+
end
|
47
|
+
|
48
|
+
def translated_input
|
49
|
+
parsed_google_response
|
50
|
+
end
|
51
|
+
|
52
|
+
def unknown_languages?
|
53
|
+
return false if recognized?(from_language) && recognized?(to_language)
|
54
|
+
result = recognized?(from_language) ? [] : [from_language]
|
55
|
+
result << to_language unless recognized?(to_language)
|
56
|
+
end
|
57
|
+
|
58
|
+
def unknown_languages_message
|
59
|
+
"I don't recognize #{unknown_languages.join(' or ')}"
|
60
|
+
end
|
61
|
+
|
62
|
+
# Putting this at the bottom to keep it out of the way
|
63
|
+
def language_map # rubocop:disable Metrics/MethodLength
|
64
|
+
{
|
65
|
+
:af => 'Afrikaans',
|
66
|
+
:sq => 'Albanian',
|
67
|
+
:ar => 'Arabic',
|
68
|
+
:az => 'Azerbaijani',
|
69
|
+
:eu => 'Basque',
|
70
|
+
:bn => 'Bengali',
|
71
|
+
:be => 'Belarusian',
|
72
|
+
:bg => 'Bulgarian',
|
73
|
+
:ca => 'Catalan',
|
74
|
+
:'zh-CN' => 'Chinese Simplified',
|
75
|
+
:'zh-TW' => 'Chinese Traditional',
|
76
|
+
:hr => 'Croatian',
|
77
|
+
:cs => 'Czech',
|
78
|
+
:da => 'Danish',
|
79
|
+
:nl => 'Dutch',
|
80
|
+
:en => 'English',
|
81
|
+
:eo => 'Esperanto',
|
82
|
+
:et => 'Estonian',
|
83
|
+
:tl => 'Filipino',
|
84
|
+
:fi => 'Finnish',
|
85
|
+
:fr => 'French',
|
86
|
+
:gl => 'Galician',
|
87
|
+
:ka => 'Georgian',
|
88
|
+
:de => 'German',
|
89
|
+
:el => 'Greek',
|
90
|
+
:gu => 'Gujarati',
|
91
|
+
:ht => 'Haitian Creole',
|
92
|
+
:iw => 'Hebrew',
|
93
|
+
:hi => 'Hindi',
|
94
|
+
:hu => 'Hungarian',
|
95
|
+
:is => 'Icelandic',
|
96
|
+
:id => 'Indonesian',
|
97
|
+
:ga => 'Irish',
|
98
|
+
:it => 'Italian',
|
99
|
+
:ja => 'Japanese',
|
100
|
+
:kn => 'Kannada',
|
101
|
+
:ko => 'Korean',
|
102
|
+
:la => 'Latin',
|
103
|
+
:lv => 'Latvian',
|
104
|
+
:lt => 'Lithuanian',
|
105
|
+
:mk => 'Macedonian',
|
106
|
+
:ms => 'Malay',
|
107
|
+
:mt => 'Maltese',
|
108
|
+
:no => 'Norwegian',
|
109
|
+
:fa => 'Persian',
|
110
|
+
:pl => 'Polish',
|
111
|
+
:pt => 'Portuguese',
|
112
|
+
:ro => 'Romanian',
|
113
|
+
:ru => 'Russian',
|
114
|
+
:sr => 'Serbian',
|
115
|
+
:sk => 'Slovak',
|
116
|
+
:sl => 'Slovenian',
|
117
|
+
:es => 'Spanish',
|
118
|
+
:sw => 'Swahili',
|
119
|
+
:sv => 'Swedish',
|
120
|
+
:ta => 'Tamil',
|
121
|
+
:te => 'Telugu',
|
122
|
+
:th => 'Thai',
|
123
|
+
:tr => 'Turkish',
|
124
|
+
:uk => 'Ukrainian',
|
125
|
+
:ur => 'Urdu',
|
126
|
+
:vi => 'Vietnamese',
|
127
|
+
:cy => 'Welsh',
|
128
|
+
:yi => 'Yiddish'
|
129
|
+
}
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
metadata
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_translater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Terry S
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-26 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-byebug
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec-nc
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Provides a Ruby object to translate phrases using Google
|
126
|
+
email:
|
127
|
+
- itsterry@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- Guardfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- google_translater.gemspec
|
143
|
+
- lib/google_translater.rb
|
144
|
+
- lib/google_translater/translater.rb
|
145
|
+
- lib/google_translater/version.rb
|
146
|
+
homepage: https://github.com/morsedigital/google_translater
|
147
|
+
licenses:
|
148
|
+
- MIT
|
149
|
+
metadata: {}
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubyforge_project:
|
166
|
+
rubygems_version: 2.6.6
|
167
|
+
signing_key:
|
168
|
+
specification_version: 4
|
169
|
+
summary: Provides a Ruby object to translate phrases using Google
|
170
|
+
test_files: []
|