to_lang 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ Gemfile.lock
5
+ coverage/*
data/README.textile CHANGED
@@ -1,14 +1,14 @@
1
1
  h1. to_lang
2
2
 
3
- *to_lang* is a Ruby gem that uses the Google Translate API to allow you to perform a translation directly on a string.
3
+ *to_lang* is a Ruby library that adds language translation methods to strings, backed by the Google Translate API.
4
4
 
5
- h2. Usage
5
+ h2. Installation
6
+
7
+ Simply run @gem install to_lang@.
6
8
 
7
- To use *to_lang*, require the library, then call @ToLang.start@ with your Google Translate API key to initialize the connector and include the methods in @String@. Then you have access to @String#translate@.
9
+ h2. Usage
8
10
 
9
- bq. *translate(to, *options)*
10
- _to_ (required): a string indicating the language to translate to, e.g. "es"
11
- _options[:from]_ (optional): a string indicating the language of the original string, used when automatic language detection is not accurate
11
+ To use *to_lang*, require the library, then call @ToLang.start@ with your Google Translate API key. At this point you will have access to all the new translation methods, which take the form @to_language@, where "language" is the language you wish to translate to. Google Translate attempts to detect the source language, but you can specify it explicitly by calling methods in the form @to_language_from_source_language@, where "source_language" is the source language. These methods are generated dynamically and will not appear in a call to @String.new.methods@ until they have been called once. Strings will, however, @respond_to?@ these methods prior to their dynamic definition.
12
12
 
13
13
  h2. Examples
14
14
 
@@ -19,21 +19,79 @@ ToLang.start('YOUR_GOOGLE_TRANSLATE_API_KEY')
19
19
 
20
20
  Translate some text to Spanish:
21
21
 
22
- bq. "Very cool gem!".translate('es')
23
- # "Muy fresco joya!"
22
+ bq. "Very cool gem!".to_spanish
23
+ => "Muy fresco joya!"
24
+
25
+ A case where the source language is ambiguous:
26
+
27
+ bq. "a pie".to_spanish
28
+ => "a pie"
29
+ "a pie".to_spanish_from_english
30
+ => "un pastel"
31
+
32
+ h2. Supported Languages
24
33
 
25
- If the source string is of an ambiguous language, the translation might not work as expected. In this case, you can specify the source language explicitly with the @:from@ option:
34
+ *to_lang* adds the following methods to strings. Each of these methods can be called with an explicit source language by appending @_to_source_language@ to the method name.
26
35
 
27
- bq. "a pie".translate('es')
28
- # "a pie"
29
- "a pie".translate('es', :from => 'en')
30
- # "un pastel"
36
+ * to_afrikaans
37
+ * to_albanian
38
+ * to_arabic
39
+ * to_basque
40
+ * to_belarusian
41
+ * to_bulgarian
42
+ * to_catalan
43
+ * to_simplified_chinese
44
+ * to_traditional_chinese
45
+ * to_croatian
46
+ * to_czech
47
+ * to_danish
48
+ * to_dutch
49
+ * to_english
50
+ * to_estonian
51
+ * to_filipino
52
+ * to_finnish
53
+ * to_french
54
+ * to_galician
55
+ * to_german
56
+ * to_greek
57
+ * to_haitian_creole
58
+ * to_hebrew
59
+ * to_hindi
60
+ * to_hungarian
61
+ * to_icelandic
62
+ * to_indonesian
63
+ * to_irish
64
+ * to_italian
65
+ * to_japanese
66
+ * to_latvian
67
+ * to_lithuanian
68
+ * to_macedonian
69
+ * to_malay
70
+ * to_maltese
71
+ * to_norwegian
72
+ * to_persian
73
+ * to_polish
74
+ * to_portuguese
75
+ * to_romanian
76
+ * to_russian
77
+ * to_serbian
78
+ * to_slovak
79
+ * to_slovenian
80
+ * to_spanish
81
+ * to_swahili
82
+ * to_swedish
83
+ * to_thai
84
+ * to_turkish
85
+ * to_ukrainian
86
+ * to_vietnamese
87
+ * to_welsh
88
+ * to_yiddish
31
89
 
32
90
  h2. Roadmap
33
91
 
34
- - Add tests for current code. *to_lang* is in its infancy so use at your own risk.
35
- - Add automagic sugar methods for translating to specific languages, such as @to_spanish@ and @to_spanish_from_english@
92
+ - Add complete documentation for the library.
93
+ - Add Unicode support for Ruby 1.8. *to_lang* currently works safely only under 1.9.
36
94
 
37
95
  h2. Feedback and Contributions
38
96
 
39
- Feedback is appreciated, as are patch contributions. Feel free to fork the project and send me a pull request.
97
+ Feedback is greatly appreciated, as are patch contributions. Feel free to fork the project and send me a pull request.
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,57 @@
1
+ module ToLang
2
+ CODEMAP = {
3
+ 'afrikaans' => 'af',
4
+ 'albanian' => 'sq',
5
+ 'arabic' => 'ar',
6
+ 'basque' => 'eu',
7
+ 'belarusian' => 'be',
8
+ 'bulgarian' => 'bg',
9
+ 'catalan' => 'ca',
10
+ 'simplified_chinese' => 'zh-CN',
11
+ 'traditional_chinese' => 'zh-TW',
12
+ 'croatian' => 'hr',
13
+ 'czech' => 'cs',
14
+ 'danish' => 'da',
15
+ 'dutch' => 'nl',
16
+ 'english' => 'en',
17
+ 'estonian' => 'et',
18
+ 'filipino' => 'tl',
19
+ 'finnish' => 'fi',
20
+ 'french' => 'fr',
21
+ 'galician' => 'gl',
22
+ 'german' => 'de',
23
+ 'greek' => 'el',
24
+ 'haitian_creole' => 'ht',
25
+ 'hebrew' => 'iw',
26
+ 'hindi' => 'hi',
27
+ 'hungarian' => 'hu',
28
+ 'icelandic' => 'is',
29
+ 'indonesian' => 'id',
30
+ 'irish' => 'ga',
31
+ 'italian' => 'it',
32
+ 'japanese' => 'ja',
33
+ 'latvian' => 'lv',
34
+ 'lithuanian' => 'lt',
35
+ 'macedonian' => 'mk',
36
+ 'malay' => 'ms',
37
+ 'maltese' => 'mt',
38
+ 'norwegian' => 'no',
39
+ 'persian' => 'fa',
40
+ 'polish' => 'pl',
41
+ 'portuguese' => 'pt',
42
+ 'romanian' => 'ro',
43
+ 'russian' => 'ru',
44
+ 'serbian' => 'sr',
45
+ 'slovak' => 'sk',
46
+ 'slovenian' => 'sl',
47
+ 'spanish' => 'es',
48
+ 'swahili' => 'sw',
49
+ 'swedish' => 'sv',
50
+ 'thai' => 'th',
51
+ 'turkish' => 'tr',
52
+ 'ukrainian' => 'uk',
53
+ 'vietnamese' => 'vi',
54
+ 'welsh' => 'cy',
55
+ 'yiddish' => 'yi',
56
+ }
57
+ end
@@ -1,10 +1,9 @@
1
+ require 'rubygems'
1
2
  require 'httparty'
2
3
  require 'uri'
3
4
 
4
5
  module ToLang
5
6
  class Connector
6
- include HTTParty
7
-
8
7
  API_URL = "https://www.googleapis.com/language/translate/v2"
9
8
 
10
9
  attr_reader :key
@@ -13,27 +12,19 @@ module ToLang
13
12
  @key = key
14
13
  end
15
14
 
16
- def request(q, target, *args)
17
- response = self.class.get request_url(q, target, *args)
15
+ def request(q, target, options = {})
16
+ response = HTTParty.get request_url(q, target, options)
17
+ raise response.parsed_response["error"]["message"] if response.parsed_response["error"] && response.parsed_response["error"]["message"]
18
18
  response.parsed_response["data"]["translations"][0]["translatedText"]
19
19
  end
20
20
 
21
21
  private
22
22
 
23
- def request_url(q, target, *args)
24
- options = extract_options(*args)
23
+ def request_url(q, target, options)
25
24
  source = options[:from]
26
25
  url = "#{API_URL}?key=#{@key}&q=#{URI.escape(q)}&target=#{target}"
27
26
  url += "&source=#{source}" if source
28
27
  url
29
28
  end
30
-
31
- def extract_options(*args)
32
- if args.last.is_a? Hash
33
- args.pop
34
- else
35
- {}
36
- end
37
- end
38
29
  end
39
30
  end
@@ -1,3 +1,3 @@
1
1
  module ToLang
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/to_lang.rb CHANGED
@@ -1,17 +1,58 @@
1
+ require File.expand_path("../to_lang/codemap", __FILE__)
1
2
  require File.expand_path("../to_lang/connector", __FILE__)
2
3
 
3
4
  module ToLang
4
- def self.start(key)
5
- @@connector = ToLang::Connector.new(key)
5
+ class << self
6
+ attr_reader :connector
6
7
 
7
- String.send(:include, InstanceMethods)
8
- end
8
+ def start(key)
9
+ return false if defined?(@connector) && !@connector.nil?
10
+ @connector = ToLang::Connector.new(key)
11
+ String.send(:include, StringMethods)
12
+ add_magic_methods
13
+ true
14
+ end
15
+
16
+ private
17
+
18
+ def add_magic_methods
19
+ String.class_eval do
20
+ def method_missing(method, *args, &block)
21
+ if method.to_s =~ /^to_(.*)_from_(.*)$/ && CODEMAP[$1] && CODEMAP[$2]
22
+ new_method_name = "to_#{$1}_from_#{$2}".to_sym
23
+
24
+ self.class.send(:define_method, new_method_name, Proc.new {
25
+ translate(CODEMAP[$1], :from => CODEMAP[$2])
26
+ })
9
27
 
10
- def self.connector
11
- @@connector
28
+ send new_method_name
29
+ elsif method.to_s =~ /^to_(.*)$/ && CODEMAP[$1]
30
+ new_method_name = "to_#{$1}".to_sym
31
+
32
+ self.class.send(:define_method, new_method_name, Proc.new {
33
+ translate(CODEMAP[$1])
34
+ })
35
+
36
+ send new_method_name
37
+ else
38
+ super
39
+ end
40
+ end
41
+
42
+ def respond_to?(method, include_private = false)
43
+ if method.to_s =~ /^to_(.*)_from_(.*)$/ && CODEMAP[$1] && CODEMAP[$2]
44
+ true
45
+ elsif method.to_s =~ /^to_(.*)$/ && CODEMAP[$1]
46
+ true
47
+ else
48
+ super
49
+ end
50
+ end
51
+ end
52
+ end
12
53
  end
13
54
 
14
- module InstanceMethods
55
+ module StringMethods
15
56
  def translate(target, *args)
16
57
  ToLang.connector.request(self, target, *args)
17
58
  end
@@ -0,0 +1,6 @@
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/spec/"
4
+ end
5
+
6
+ require File.expand_path('../../lib/to_lang', __FILE__)
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe ToLang::Connector do
4
+ before :all do
5
+ @connector = ToLang::Connector.new('apikey')
6
+ end
7
+
8
+ it "stores a key when initialized" do
9
+ @connector.key.should_not be_nil
10
+ end
11
+
12
+ context "when sent :request" do
13
+ def stub_response(parsed_response)
14
+ mock_response = mock('HTTParty::Response', :parsed_response => parsed_response)
15
+ HTTParty.stub(:get).and_return(mock_response)
16
+ end
17
+
18
+ def stub_good_response(translated_text)
19
+ parsed_response = { "data" => { "translations" => [ { "translatedText" => translated_text } ] } }
20
+ stub_response(parsed_response)
21
+ end
22
+
23
+ def stub_bad_response(error_message)
24
+ parsed_response = { "error" => { "message" => error_message } }
25
+ stub_response(parsed_response)
26
+ end
27
+
28
+ context "with only a target language" do
29
+ it "returns the translated string" do
30
+ stub_good_response "hola mundo"
31
+ @connector.request("hello world", "es").should == "hola mundo"
32
+ end
33
+ end
34
+
35
+ context "with an ambiguous source language" do
36
+ context "and no source language specified" do
37
+ it "returns the same string" do
38
+ stub_good_response "a pie"
39
+ @connector.request("a pie", "es").should == "a pie"
40
+ end
41
+ end
42
+
43
+ context "and a source language specified" do
44
+ it "returns the translated string" do
45
+ stub_good_response "un pastel"
46
+ @connector.request("a pie", "es", :from => "en").should == "un pastel"
47
+ end
48
+ end
49
+ end
50
+
51
+ context "with a bad language pair" do
52
+ it "raises an exception" do
53
+ stub_bad_response "Bad language pair: en|en"
54
+ expect { @connector.request("a pie", "en", :from => "en") }.to raise_error(RuntimeError, "Bad language pair: en|en")
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ describe ToLang do
4
+ context "when sent :start" do
5
+ before :all do
6
+ ToLang.start('apikey')
7
+ end
8
+
9
+ it "returns false if :start was already called" do
10
+ ToLang.start('apikey').should == false
11
+ end
12
+
13
+ it "stores a ToLang::Connector object" do
14
+ ToLang.connector.should be_an_instance_of ToLang::Connector
15
+ end
16
+
17
+ it "mixes StringMethods into String" do
18
+ String.should include ToLang::StringMethods
19
+ end
20
+ end
21
+ end
22
+
23
+ describe "A string" do
24
+ context "after ToLang has received :start" do
25
+ before :all do
26
+ ToLang.start('apikey')
27
+ end
28
+
29
+ it "responds to :translate" do
30
+ String.new.should respond_to :translate
31
+ end
32
+
33
+ context "when sent :translate" do
34
+ it "calls ToLang::Connector#request" do
35
+ ToLang.connector.stub(:request)
36
+ ToLang.connector.should_receive(:request).with("hello world", "es")
37
+ "hello world".translate("es")
38
+ end
39
+ end
40
+
41
+ ToLang::CODEMAP.each do |language, code|
42
+ it "will respond_to :to_#{language}" do
43
+ "hello_world".should respond_to "to_#{language}"
44
+ end
45
+
46
+ it "will respond to :to_#{language}_from_english" do
47
+ "hello_world".should respond_to "to_#{language}_from_english"
48
+ end
49
+
50
+ it "translates to #{language} when sent :to_#{language}" do
51
+ ToLang.connector.stub(:request)
52
+ ToLang.connector.should_receive(:request).with("hello world", code)
53
+ "hello world".send("to_#{language}")
54
+ end
55
+
56
+ it "translates to #{language} from english when sent :to_#{language}_from_english" do
57
+ ToLang.connector.stub(:request)
58
+ ToLang.connector.should_receive(:request).with("hello world", code, :from => 'en')
59
+ "hello world".send("to_#{language}_from_english")
60
+ end
61
+ end
62
+
63
+ context "when a magic method has been called once" do
64
+ before :each do
65
+ ToLang.connector.stub(:request)
66
+ "hello world".to_spanish
67
+ "hello world".to_spanish_from_english
68
+ end
69
+
70
+ it "defines the method and does not call :method_missing the next time" do
71
+ string = "hello world"
72
+ string.should_not_receive(:method_missing)
73
+ string.to_spanish
74
+ string.to_spanish_from_english
75
+ end
76
+ end
77
+ end
78
+ end
data/to_lang.gemspec CHANGED
@@ -1,6 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "to_lang/version"
2
+ require File.expand_path("../lib/to_lang/version", __FILE__)
4
3
 
5
4
  Gem::Specification.new do |s|
6
5
  s.name = "to_lang"
@@ -8,16 +7,16 @@ Gem::Specification.new do |s|
8
7
  s.platform = Gem::Platform::RUBY
9
8
  s.authors = ["Jimmy Cuadra"]
10
9
  s.email = ["jimmy@jimmycuadra.com"]
11
- s.homepage = "http://github.com/jimmycuadra/to_lang"
12
- s.summary = %q{A Ruby client for the Google Translate API built directly into String}
13
- s.description = %q{A Ruby client for the Google Translate API built directly into String}
14
-
15
- s.rubyforge_project = "to_lang"
16
-
10
+ s.homepage = "https://github.com/jimmycuadra/to_lang"
11
+ s.summary = %q{Adds language translation methods to strings, backed by the Google Translate API}
12
+ s.description = %q{Adds language translation methods to strings, backed by the Google Translate API}
13
+ s.rubyforge_project = s.name
17
14
  s.files = `git ls-files`.split("\n")
18
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
17
  s.require_paths = ["lib"]
21
18
 
22
- s.add_dependency "httparty", "~> 0.6.1"
19
+ s.add_dependency "httparty", "~> 0.6"
20
+ s.add_development_dependency "rspec", "~> 2.3"
21
+ s.add_development_dependency "simplecov", "~> 0.3"
23
22
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_lang
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 0
9
7
  - 1
10
- version: 0.0.1
8
+ - 0
9
+ version: 0.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Jimmy Cuadra
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-12-02 00:00:00 -08:00
17
+ date: 2010-12-26 00:00:00 -08:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,15 +25,41 @@ dependencies:
26
25
  requirements:
27
26
  - - ~>
28
27
  - !ruby/object:Gem::Version
29
- hash: 5
30
28
  segments:
31
29
  - 0
32
30
  - 6
33
- - 1
34
- version: 0.6.1
31
+ version: "0.6"
35
32
  type: :runtime
36
33
  version_requirements: *id001
37
- description: A Ruby client for the Google Translate API built directly into String
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 2
44
+ - 3
45
+ version: "2.3"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: simplecov
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ - 3
59
+ version: "0.3"
60
+ type: :development
61
+ version_requirements: *id003
62
+ description: Adds language translation methods to strings, backed by the Google Translate API
38
63
  email:
39
64
  - jimmy@jimmycuadra.com
40
65
  executables: []
@@ -46,15 +71,18 @@ extra_rdoc_files: []
46
71
  files:
47
72
  - .gitignore
48
73
  - Gemfile
49
- - Gemfile.lock
50
74
  - README.textile
51
75
  - Rakefile
52
76
  - lib/to_lang.rb
77
+ - lib/to_lang/codemap.rb
53
78
  - lib/to_lang/connector.rb
54
79
  - lib/to_lang/version.rb
80
+ - spec/spec_helper.rb
81
+ - spec/to_lang/connector_spec.rb
82
+ - spec/to_lang_spec.rb
55
83
  - to_lang.gemspec
56
84
  has_rdoc: true
57
- homepage: http://github.com/jimmycuadra/to_lang
85
+ homepage: https://github.com/jimmycuadra/to_lang
58
86
  licenses: []
59
87
 
60
88
  post_install_message:
@@ -67,7 +95,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
95
  requirements:
68
96
  - - ">="
69
97
  - !ruby/object:Gem::Version
70
- hash: 3
71
98
  segments:
72
99
  - 0
73
100
  version: "0"
@@ -76,7 +103,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
103
  requirements:
77
104
  - - ">="
78
105
  - !ruby/object:Gem::Version
79
- hash: 3
80
106
  segments:
81
107
  - 0
82
108
  version: "0"
@@ -86,6 +112,8 @@ rubyforge_project: to_lang
86
112
  rubygems_version: 1.3.7
87
113
  signing_key:
88
114
  specification_version: 3
89
- summary: A Ruby client for the Google Translate API built directly into String
90
- test_files: []
91
-
115
+ summary: Adds language translation methods to strings, backed by the Google Translate API
116
+ test_files:
117
+ - spec/spec_helper.rb
118
+ - spec/to_lang/connector_spec.rb
119
+ - spec/to_lang_spec.rb
data/Gemfile.lock DELETED
@@ -1,19 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- to_lang (0.0.1)
5
- httparty (~> 0.6.1)
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- crack (0.1.8)
11
- httparty (0.6.1)
12
- crack (= 0.1.8)
13
-
14
- PLATFORMS
15
- ruby
16
-
17
- DEPENDENCIES
18
- httparty (~> 0.6.1)
19
- to_lang!