rufregle 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +37 -0
- data/.travis.yml +10 -0
- data/README.md +74 -0
- data/Rakefile +19 -0
- data/lib/translators/free_google/extractor.rb +26 -0
- data/lib/translators/free_google/factory.rb +17 -0
- data/lib/translators/free_google/requester.rb +17 -0
- data/lib/translators/free_google/translator.rb +25 -0
- data/lib/translators/translator.rb +16 -0
- data/rufregle.gemspec +12 -0
- data/test/integration/rufregle_test.rb +62 -0
- data/test/unit/rufregle_test.rb +36 -0
- data/test/unit/translators/freegoogle/extractor_test.rb +53 -0
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6eb919aa0fe36fc1c440ae8a5d76b1fb9532c737
|
4
|
+
data.tar.gz: 2dd8d823e2b7240538ab7fde6724ed01a315ae27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4f3729a12d1932f0c2328b2683c16929fb1d3fc4b3d73be7546157ba2ae92763fc7803a36843c853d283ba6519b5694e3a9994939aa6fa0ded16b2a913227e9
|
7
|
+
data.tar.gz: 71842e72de45c627d523cc573b04a17e6cce37344d980de77c83eb15166f299d79ae4e9e436a1cc917cf40e79523249583c067996b23fe7d1f66728e0200f455
|
data/.gitignore
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
## Specific to RubyMotion:
|
14
|
+
.dat*
|
15
|
+
.repl_history
|
16
|
+
build/
|
17
|
+
|
18
|
+
## Documentation cache and generated files:
|
19
|
+
/.yardoc/
|
20
|
+
/_yardoc/
|
21
|
+
/doc/
|
22
|
+
/rdoc/
|
23
|
+
|
24
|
+
## Environment normalisation:
|
25
|
+
/.bundle/
|
26
|
+
/vendor/bundle
|
27
|
+
/lib/bundler/man/
|
28
|
+
|
29
|
+
# for a library or gem, you might want to ignore these files since the code is
|
30
|
+
# intended to run in multiple environments; otherwise, check them in:
|
31
|
+
# Gemfile.lock
|
32
|
+
# .ruby-version
|
33
|
+
# .ruby-gemset
|
34
|
+
|
35
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
36
|
+
.rvmrc
|
37
|
+
*~
|
data/.travis.yml
ADDED
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
Rufregle
|
2
|
+
-----
|
3
|
+
[![Version](http://img.shields.io/gem/v/rufregle.svg)](https://rubygems.org/gems/rufregle)[![Build Status](https://travis-ci.org/CristianOliveiraDaRosa/rufregle.svg)](https://travis-ci.org/CristianOliveiraDaRosa/rufregle)
|
4
|
+
|
5
|
+
Ruby Free Google Translate gem or Rufregle
|
6
|
+
|
7
|
+
Simple unofficial free google translate library.
|
8
|
+
|
9
|
+
#How it works?
|
10
|
+
|
11
|
+
It uses an alternative google url to request the translate that's why this lib may have performance issues.
|
12
|
+
Recommended to use only in prototypes applications.
|
13
|
+
|
14
|
+
###Is it free?
|
15
|
+
|
16
|
+
Yes.
|
17
|
+
|
18
|
+
###Should I use it in my Production Application?
|
19
|
+
|
20
|
+
Maybe. You need performance?
|
21
|
+
|
22
|
+
|
23
|
+
#Using
|
24
|
+
More simple than that?
|
25
|
+
|
26
|
+
Installing gem
|
27
|
+
```
|
28
|
+
$ gem install rufregle
|
29
|
+
|
30
|
+
```
|
31
|
+
|
32
|
+
Using on code:
|
33
|
+
```
|
34
|
+
result = Rufregle.new.translate("Hello World", "en", "pt")
|
35
|
+
p "#{result[:translated]}"
|
36
|
+
```
|
37
|
+
|
38
|
+
# Tests
|
39
|
+
Runing all tests
|
40
|
+
|
41
|
+
```
|
42
|
+
rake test
|
43
|
+
```
|
44
|
+
|
45
|
+
### Integration
|
46
|
+
```
|
47
|
+
rake integration_test
|
48
|
+
```
|
49
|
+
|
50
|
+
### Unit Tests
|
51
|
+
```
|
52
|
+
rake integration_test
|
53
|
+
```
|
54
|
+
|
55
|
+
Contributing
|
56
|
+
===
|
57
|
+
1. Fork it!
|
58
|
+
2. Create your feature branch: `git checkout -b my-new-feature`
|
59
|
+
3. Commit your changes: `git commit -am 'Add some feature'`
|
60
|
+
4. Push to the branch: `git push origin my-new-feature`
|
61
|
+
5. Submit a pull request
|
62
|
+
|
63
|
+
After that include your name in "Authors" section!
|
64
|
+
|
65
|
+
** Pull Request should have unit tests **
|
66
|
+
|
67
|
+
License
|
68
|
+
====
|
69
|
+
This project is under MIT licence
|
70
|
+
|
71
|
+
Authors
|
72
|
+
=====
|
73
|
+
|
74
|
+
Cristian Oliveira - www.cristianoliveira.com.br
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
|
3
|
+
UNIT_TEST_PATTERN = 'test/unit/**/*_test.rb'
|
4
|
+
INTEGRATION_TEST_PATTERN = 'test/integration/**/*_test.rb'
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.test_files = FileList[UNIT_TEST_PATTERN]
|
8
|
+
end
|
9
|
+
|
10
|
+
Rake::TestTask.new(:unit_test) do |t|
|
11
|
+
t.test_files = FileList[UNIT_TEST_PATTERN]
|
12
|
+
end
|
13
|
+
|
14
|
+
Rake::TestTask.new(:integration_test) do |t|
|
15
|
+
t.test_files = FileList[INTEGRATION_TEST_PATTERN]
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Run tests"
|
19
|
+
task :default => :test
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module FreeGoogle
|
2
|
+
##
|
3
|
+
# Responsible for extract translated result from a received raw text
|
4
|
+
|
5
|
+
class Extractor
|
6
|
+
ENCODE = 'utf-8'
|
7
|
+
|
8
|
+
##
|
9
|
+
# Extract translation
|
10
|
+
# @param rawdata [String] Raw text without formating
|
11
|
+
# @return [String] Translated text
|
12
|
+
#
|
13
|
+
# Example of how raw data look like:
|
14
|
+
# [[["Olá","Hello",,,10]],,"en"]
|
15
|
+
#
|
16
|
+
|
17
|
+
def extract(rawdata)
|
18
|
+
return "" if rawdata.nil? || rawdata.empty?
|
19
|
+
|
20
|
+
encoded = rawdata.force_encoding(ENCODE)
|
21
|
+
translations = encoded[/\["(.*?)","/]
|
22
|
+
|
23
|
+
translations[2..-4]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative 'extractor'
|
2
|
+
require_relative 'requester'
|
3
|
+
require_relative 'translator'
|
4
|
+
|
5
|
+
##
|
6
|
+
# Responsible for create instance of {FreeGoogle::Translator}
|
7
|
+
# It holds all dependencies for this creation
|
8
|
+
|
9
|
+
module FreeGoogle
|
10
|
+
class Factory
|
11
|
+
def self.create()
|
12
|
+
extractor = FreeGoogle::Extractor.new
|
13
|
+
http_requester = FreeGoogle::Requester.new
|
14
|
+
FreeGoogle::Translator.new(extractor, http_requester)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Responsible for HTTP requests
|
6
|
+
module FreeGoogle
|
7
|
+
class Requester
|
8
|
+
|
9
|
+
##
|
10
|
+
# Http request using GET method.
|
11
|
+
|
12
|
+
def request(url)
|
13
|
+
url = URI::encode(url)
|
14
|
+
Net::HTTP.get(URI.parse(url))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
##
|
2
|
+
# Implementation of Translator using alternative URL API to request translations
|
3
|
+
|
4
|
+
module FreeGoogle
|
5
|
+
class Translator
|
6
|
+
|
7
|
+
GOOGLE_URL_API = "https://translate.google.com/translate_a/"
|
8
|
+
GOOGLE_PARAMS = "single?client=z&sl=%s&tl=%s-CN&ie=UTF-8&oe=UTF-8&dt=t&dt=rm&q=%s"
|
9
|
+
|
10
|
+
def initialize(extractor, http_requester)
|
11
|
+
@translation_extractor = extractor
|
12
|
+
@http_requester = http_requester
|
13
|
+
end
|
14
|
+
|
15
|
+
def translate(text_to_translate, language_from, language_to)
|
16
|
+
parameters = GOOGLE_PARAMS % [ language_from, language_to , text_to_translate]
|
17
|
+
url = GOOGLE_URL_API + parameters
|
18
|
+
|
19
|
+
raw_data = @http_requester.request(url)
|
20
|
+
translated = @translation_extractor.extract(raw_data)
|
21
|
+
|
22
|
+
{translated: translated, original: text_to_translate}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
##
|
2
|
+
# A Blueprint of how a Translator class must be.
|
3
|
+
|
4
|
+
class Translator
|
5
|
+
##
|
6
|
+
# Return a translated text of a given +text+ +from+ language +to+ another language
|
7
|
+
#
|
8
|
+
# @param text [String] text to be translated.
|
9
|
+
# @param from [String] language from
|
10
|
+
# @param to [String] language to
|
11
|
+
# @return [String] translated text
|
12
|
+
|
13
|
+
def translate(text, from, to)
|
14
|
+
raise "Method do_translate must be implemented"
|
15
|
+
end
|
16
|
+
end
|
data/rufregle.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'rufregle'
|
3
|
+
s.version = '2.0.1'
|
4
|
+
s.date = '2015-09-19'
|
5
|
+
s.summary = "Free google translate!"
|
6
|
+
s.description = "Simple Free google translate."
|
7
|
+
s.authors = ["Cristian Oliveira"]
|
8
|
+
s.email = 'contato@cristianoliveira.com.br'
|
9
|
+
s.files = `git ls-files`.split($/)
|
10
|
+
s.homepage = 'https://github.com/CristianOliveiraDaRosa/rufregle'
|
11
|
+
s.license = 'MIT'
|
12
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/bin/env ruby
|
2
|
+
# encoding: utf-8
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require_relative '../../lib/rufregle.rb'
|
6
|
+
require_relative '../../lib/translators/free_google/factory.rb'
|
7
|
+
|
8
|
+
class RufregleTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_english_to_portuguese_hello
|
11
|
+
#given
|
12
|
+
word = "Hello"
|
13
|
+
expected = "Olá"
|
14
|
+
|
15
|
+
#when
|
16
|
+
result = Rufregle.new.translate(word, :en , :pt)
|
17
|
+
|
18
|
+
#then
|
19
|
+
assert_equal(expected, result[:translated])
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_english_to_portuguese_hello_get_original
|
23
|
+
#given
|
24
|
+
word = "Hello"
|
25
|
+
expected = "Hello"
|
26
|
+
|
27
|
+
#when
|
28
|
+
result = Rufregle.new.translate(expected, :en , :pt)
|
29
|
+
|
30
|
+
#then
|
31
|
+
assert_equal(expected, result[:original])
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_prhase_translate
|
35
|
+
#given
|
36
|
+
phrase = "Isso é uma frase traduzida"
|
37
|
+
expected = "This is a translated phrase"
|
38
|
+
|
39
|
+
#when
|
40
|
+
result = Rufregle.new.translate(phrase, :pt, :en)
|
41
|
+
|
42
|
+
#then
|
43
|
+
assert_equal(expected, result[:translated])
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_given_new_translator
|
47
|
+
#given
|
48
|
+
phrase = "Isso é uma frase traduzida"
|
49
|
+
expected = "This is a translated phrase"
|
50
|
+
translator = FreeGoogle::Factory.create()
|
51
|
+
rufregle = Rufregle.new
|
52
|
+
|
53
|
+
#when
|
54
|
+
rufregle.translator = translator
|
55
|
+
result = rufregle.translate(phrase, :pt, :en)
|
56
|
+
|
57
|
+
#then
|
58
|
+
assert_equal(expected, result[:translated])
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../../lib/rufregle'
|
3
|
+
require_relative '../../lib/rufregle'
|
4
|
+
|
5
|
+
class RufregleTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@translator = StubedTranslator.new
|
9
|
+
@rufregle = Rufregle.new
|
10
|
+
@rufregle.translator = @translator
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_it_should_raise_error
|
14
|
+
assert_raise RuntimeError do @rufregle.translate(nil, nil, nil) end
|
15
|
+
assert_raise RuntimeError do @rufregle.translate("", "", "") end
|
16
|
+
assert_raise RuntimeError do @rufregle.translate("aaa", "", "") end
|
17
|
+
assert_raise RuntimeError do @rufregle.translate("aaa", "aaa", "") end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_it_should_not_raise_error
|
21
|
+
# given
|
22
|
+
translation = "Text translated"
|
23
|
+
@translator.result = translation
|
24
|
+
|
25
|
+
# then
|
26
|
+
assert_equal translation, @rufregle.translate("aaa", "aaa", "aaa")
|
27
|
+
end
|
28
|
+
|
29
|
+
class StubedTranslator
|
30
|
+
attr_accessor :result
|
31
|
+
|
32
|
+
def translate(text, from, to)
|
33
|
+
return @result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../../../../lib/translators/free_google/extractor'
|
3
|
+
|
4
|
+
##
|
5
|
+
# Tests for {FreeGoogle::Extractor}
|
6
|
+
|
7
|
+
class ExtractorTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@translation_extractor = FreeGoogle::Extractor.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_it_should_return_empty_string
|
14
|
+
assert_equal "", @translation_extractor.extract("")
|
15
|
+
assert_equal "", @translation_extractor.extract(nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_it_should_return_only_the_translation_text
|
19
|
+
# given
|
20
|
+
rawdata = '[["translation","original",,,10]'
|
21
|
+
expected = "translation"
|
22
|
+
|
23
|
+
# when
|
24
|
+
result = @translation_extractor.extract(rawdata)
|
25
|
+
|
26
|
+
# then
|
27
|
+
assert_equal expected, result
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_it_should_ignore_inside_quotes
|
31
|
+
# given
|
32
|
+
rawdata = '[["translation\'s","original",,,10]'
|
33
|
+
expected = "translation\'s"
|
34
|
+
|
35
|
+
# when
|
36
|
+
result = @translation_extractor.extract(rawdata)
|
37
|
+
|
38
|
+
# then
|
39
|
+
assert_equal expected, result
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_it_should_ignore_inside_double_quotes
|
43
|
+
# given
|
44
|
+
rawdata = '[["this has it "", bla bla","original",,,10]'
|
45
|
+
expected = 'this has it "", bla bla'
|
46
|
+
|
47
|
+
# when
|
48
|
+
result = @translation_extractor.extract(rawdata)
|
49
|
+
|
50
|
+
# then
|
51
|
+
assert_equal expected, result
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rufregle
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cristian Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Simple Free google translate.
|
14
14
|
email: contato@cristianoliveira.com.br
|
@@ -16,7 +16,22 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- ".travis.yml"
|
21
|
+
- README.md
|
22
|
+
- Rakefile
|
19
23
|
- lib/rufregle.rb
|
24
|
+
- lib/translators/free_google/extractor.rb
|
25
|
+
- lib/translators/free_google/factory.rb
|
26
|
+
- lib/translators/free_google/requester.rb
|
27
|
+
- lib/translators/free_google/translator.rb
|
28
|
+
- lib/translators/translator.rb
|
29
|
+
- rufregle-0.0.2.gem
|
30
|
+
- rufregle-1.0.2.gem
|
31
|
+
- rufregle.gemspec
|
32
|
+
- test/integration/rufregle_test.rb
|
33
|
+
- test/unit/rufregle_test.rb
|
34
|
+
- test/unit/translators/freegoogle/extractor_test.rb
|
20
35
|
homepage: https://github.com/CristianOliveiraDaRosa/rufregle
|
21
36
|
licenses:
|
22
37
|
- MIT
|
@@ -37,8 +52,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
37
52
|
version: '0'
|
38
53
|
requirements: []
|
39
54
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
55
|
+
rubygems_version: 2.2.2
|
41
56
|
signing_key:
|
42
57
|
specification_version: 4
|
43
58
|
summary: Free google translate!
|
44
59
|
test_files: []
|
60
|
+
has_rdoc:
|