painful_translate 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 44f5d2c3c54932871779c0b79fbdcae8bef88760
4
+ data.tar.gz: edd3d9871e10014f3271c25f7d95a9658a47a702
5
+ SHA512:
6
+ metadata.gz: ca74514e7b953fd5f4497e065db8fabafd9aef2119a2955f43582c722fef734312a774a41e1b31e32ba561bbf5d64f82c244c4135adbad138a1b8641b98f8d1d
7
+ data.tar.gz: 491b1727a7fbfa9eee64dc16c3fd315c22b26bbb5f2357c4b5c87177ac3ef66e388bebfc800805ca618c2be8c798912e7b3eb57f9714563ff9df86510fde90e8
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
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in painful_translate.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Thomas Chen
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,73 @@
1
+ # PainfulTranslate
2
+
3
+ This is the poor man's version of https://github.com/seejohnrun/easy_translate
4
+
5
+ For those of us who want to use Google Translate, but can't afford to sign up for the api (seriously though, it's apparently $20 per million characters translated, and if you can't afford that, I don't know how you're managing to write computer programs and whatnot).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'painful_translate'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install painful_translate
22
+
23
+ ## Usage
24
+
25
+ The API is very much the same as easy_translate... however, because we don't actually use Google's API, you'll need capybara and selenium-webdriver instead of an API key
26
+
27
+ ## Batch translation (Yay!)
28
+ ```ruby
29
+ # multiple strings and specify your languages
30
+ PainfulTranslate.translate('Hello', 'Goodbye', :to => :es, :from => :en) # => ["¡Hola", "Despedida"]
31
+
32
+ # multiple strings auto-detect
33
+ PainfulTranslate.translate('Hello', 'Goodbye', :to => :es) # => ["¡Hola", "Despedida"]
34
+ ```
35
+
36
+ ## You want language detection too?
37
+
38
+ Sorry, not built yet
39
+
40
+ ## Batch detection (Woohoo!)
41
+
42
+ Sorry, not built yet
43
+
44
+ ## Need confidence in your detections?
45
+
46
+ Sorry, not built yet
47
+
48
+ ---
49
+
50
+ ## A note on capybara and selenium and how this thing works
51
+
52
+ This gem will open up selenium via capybara which, for most of you, will mean it actually opens up firefox and visits https://translate.google.com . It then fills out the translation form, and fetches the translated answer from the webpage. In other words, this is exactly what you (as a lowly human) would do if you had to translate something with Google. Consequently, this process is slow and painful.
53
+
54
+ I suggest this thing be used to make seed files and whatnot for nascent rails projects, and not actively in production.
55
+
56
+ Also, for performance reasons, one and only one selenium session is initialized by this code. So the session will just sort of stick around until your ruby program exist.
57
+
58
+ ---
59
+
60
+ ## List of languages
61
+
62
+ ```ruby
63
+ # list from EasyTranslate::LANGUAGES which was originally from <http://translate.google.com/>
64
+ PainfulTranslate::Languages # => { 'en' => 'english', ... }
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/[my-github-username]/painful_translate/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,40 @@
1
+ require 'singleton'
2
+ class PainfulTranslate::Client
3
+ class NoTargetLanguage < StandardError; end
4
+
5
+ class << self
6
+ def instance
7
+ @instance ||= new
8
+ end
9
+ end
10
+
11
+ def translate(*keys, options)
12
+ opts = _normalize options
13
+ PainfulTranslate::GoogleTranslator.new.tap do |t|
14
+ t.strings = keys
15
+ t.to = opts[:to]
16
+ t.from = opts[:from]
17
+ t.session = _session
18
+ end.translations
19
+ end
20
+
21
+ private
22
+ def _session
23
+ _raw_session.tap(&:reset!)
24
+ end
25
+
26
+ def _raw_session
27
+ @session ||= Capybara::Session.new :selenium
28
+ end
29
+
30
+ def _normalize(opts={})
31
+ {
32
+ to: opts[:to] || _uh_oh!,
33
+ from: opts[:from] || :auto
34
+ }
35
+ end
36
+
37
+ def _uh_oh!
38
+ raise NoTargetLanguage, "You didn't specific a target language to translate into"
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ class PainfulTranslate::GoogleTranslator
2
+ attr_accessor :session, :to, :from, :strings
3
+
4
+ def translations
5
+ PainfulTranslate::TranslationPage.new(_page).translation_output_strings
6
+ end
7
+
8
+ private
9
+ def _page
10
+ session.tap { |s| s.visit _page_url }
11
+ end
12
+
13
+ def _page_url
14
+ @page_url ||= ["https://translate.google.com", _transition, _query_string].join "/"
15
+ end
16
+ def _transition
17
+ '#' + [from, to].map(&:to_s).join('/')
18
+ end
19
+ def _query_string
20
+ strings.join("\n").to_query("").gsub /^=+/, ""
21
+ end
22
+ end
23
+
@@ -0,0 +1,9 @@
1
+ class PainfulTranslate::TranslationPage
2
+ attr_accessor :session
3
+ def initialize(session)
4
+ @session = session
5
+ end
6
+ def translation_output_strings
7
+ session.find(:xpath, '//*[@id="result_box"]').all(:css, 'span.hps').map(&:text)
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module PainfulTranslate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,70 @@
1
+ require "painful_translate/version"
2
+ require 'active_support'
3
+ require 'active_support/core_ext'
4
+ require 'capybara'
5
+ module PainfulTranslate
6
+ require "painful_translate/google_translator"
7
+ require "painful_translate/translation_page"
8
+ require "painful_translate/client"
9
+
10
+ class << self
11
+ def translate(*keys, opts)
12
+ PainfulTranslate::Client.instance.translate keys, opts
13
+ end
14
+ end
15
+ Languages = {
16
+ "af"=>"afrikaans",
17
+ "sq"=>"albanian",
18
+ "ar"=>"arabic",
19
+ "be"=>"belarusian",
20
+ "bg"=>"bulgarian",
21
+ "ca"=>"catalan",
22
+ "zh-CN"=>"chinese_simplified",
23
+ "zh-TW"=>"chinese_traditional",
24
+ "hr"=>"croatian",
25
+ "cs"=>"czech",
26
+ "da"=>"danish",
27
+ "nl"=>"dutch",
28
+ "en"=>"english",
29
+ "et"=>"estonian",
30
+ "tl"=>"filipino",
31
+ "fi"=>"finnish",
32
+ "fr"=>"french",
33
+ "gl"=>"galician",
34
+ "de"=>"german",
35
+ "el"=>"greek",
36
+ "iw"=>"hebrew",
37
+ "hi"=>"hindi",
38
+ "hu"=>"hungarian",
39
+ "is"=>"icelandic",
40
+ "id"=>"indonesian",
41
+ "ga"=>"irish",
42
+ "it"=>"italian",
43
+ "ja"=>"japanese",
44
+ "ko"=>"korean",
45
+ "la"=>"latin",
46
+ "lv"=>"latvian",
47
+ "lt"=>"lithuanian",
48
+ "mk"=>"macedonian",
49
+ "ms"=>"malay",
50
+ "mt"=>"maltese",
51
+ "no"=>"norwegian",
52
+ "fa"=>"persian",
53
+ "pl"=>"polish",
54
+ "pt"=>"portuguese",
55
+ "ro"=>"romanian",
56
+ "ru"=>"russian",
57
+ "sr"=>"serbian",
58
+ "sk"=>"slovak",
59
+ "sl"=>"slovenian",
60
+ "es"=>"spanish",
61
+ "sw"=>"swahili",
62
+ "sv"=>"swedish",
63
+ "th"=>"thai",
64
+ "tr"=>"turkish",
65
+ "uk"=>"ukrainian",
66
+ "vi"=>"vietnamese",
67
+ "cy"=>"welsh",
68
+ "yi"=>"yiddish"
69
+ }
70
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'painful_translate/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "painful_translate"
8
+ spec.version = PainfulTranslate::VERSION
9
+ spec.authors = ["Thomas Chen"]
10
+ spec.email = ["foxnewsnetwork@gmail.com"]
11
+ spec.summary = %q{A capybara (selenium automation) based wrapper over google translate... for those of us too damn cheap to sign up for paid google API}
12
+ spec.description = %q{A capybara (selenium automation) based wrapper over google translate... for those of us too damn cheap to sign up for paid google API}
13
+ spec.homepage = "http://github.com/foxnewsnetwork/painful_translate"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "> 3"
24
+
25
+ spec.add_dependency "capybara", "~> 2.4"
26
+ spec.add_dependency 'selenium-webdriver', "~> 2.41.0"
27
+ spec.add_dependency "activesupport", ">= 3"
28
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe PainfulTranslate::Client do
4
+ let(:client) { PainfulTranslate::Client.instance }
5
+ specify { should be_present }
6
+ context 'translate' do
7
+ let(:actual) { client.translate "dog", "cat", from: :en, to: :es }
8
+ let(:expected) { ["perro", "gato"] }
9
+ specify { expect(actual).to eq expected }
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ require 'painful_translate'
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: painful_translate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-16 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: capybara
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.4'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: selenium-webdriver
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.41.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.41.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '3'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '3'
97
+ description: A capybara (selenium automation) based wrapper over google translate...
98
+ for those of us too damn cheap to sign up for paid google API
99
+ email:
100
+ - foxnewsnetwork@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - lib/painful_translate.rb
112
+ - lib/painful_translate/client.rb
113
+ - lib/painful_translate/google_translator.rb
114
+ - lib/painful_translate/translation_page.rb
115
+ - lib/painful_translate/version.rb
116
+ - painful_translate.gemspec
117
+ - spec/painful_translate/client_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: http://github.com/foxnewsnetwork/painful_translate
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.2.2
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: A capybara (selenium automation) based wrapper over google translate... for
143
+ those of us too damn cheap to sign up for paid google API
144
+ test_files:
145
+ - spec/painful_translate/client_spec.rb
146
+ - spec/spec_helper.rb