to_lang 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -10,7 +10,7 @@ h2. Usage
10
10
 
11
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.
12
12
 
13
- 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.
13
+ Google Translate attempts to detect the source language, but you can specify it explicitly by calling methods in the form @to_target_language_from_source_language@, where "target language" is the language you are translating to and "source_language" is the language you are starting with. An inverted form with equivalent functionality, @from_source_language_to_target_language@ is also available. 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.
14
14
 
15
15
  The dynamic methods are simply syntactic sugar for @String#translate@, which you can use directly as well.
16
16
 
@@ -18,36 +18,62 @@ h2. Examples
18
18
 
19
19
  Load and initialize *to_lang*:
20
20
 
21
- bq. require 'to_lang'
21
+ <pre><code>require 'to_lang'
22
22
  ToLang.start('YOUR_GOOGLE_TRANSLATE_API_KEY')
23
+ </code></pre>
23
24
 
24
25
  Translate some text to Spanish:
25
26
 
26
- bq. "Very cool gem!".to_spanish
27
+ <pre><code>"Very cool gem!".to_spanish
27
28
  => "Muy fresco joya!"
29
+ </code></pre>
28
30
 
29
31
  A case where the source language is ambiguous:
30
32
 
31
- bq. "a pie".to_spanish
33
+ <pre><code>"a pie".to_spanish
32
34
  => "a pie"
33
35
  "a pie".to_spanish_from_english
34
36
  => "un pastel"
37
+ </code></pre>
38
+
39
+ Or equivalently:
40
+
41
+ <pre><code>"a pie".from_english_to_spanish
42
+ => "un pastel"
43
+ </code></pre>
35
44
 
36
45
  Using @String#translate@ directly:
37
46
 
38
- bq. "hello world".translate('es')
47
+ <pre><code>"hello world".translate('es')
39
48
  => "hola mundo"
40
49
  "a pie".translate('es', :from => 'en')
41
50
  => "un pastel"
51
+ </code></pre>
52
+
53
+ h2. Debugging
54
+
55
+ @String#translate@ also has the advantage of allowing you to get debug output for a translation. @String#translate@ accepts a @:debug@ option with three possible values: @:request@, @:response@, and @:all@. @:request@ will cause the method to return the generated URL for the Google Translate API call. @:response@ will cause the method to return the full response from the API call as a hash. @:all@ will cause the method to return a hash which contains both the generated request URL and the full response.
56
+
57
+ <pre><code>"hello world".translate('es', :debug => :response)
58
+ => https://www.googleapis.com/language/translate/v2?key=my_key&q=hello+world&target=es
59
+ </code></pre>
60
+
61
+ <pre><code>"hello world".translate('es', :debug => :response)
62
+ => {"data"=>{"translations"=>[{"translatedText"=>"hola mundo"}]}}
63
+ </code></pre>
64
+
65
+ <pre><code>"hello world".translate('es', :debug => :all)
66
+ => {:request=>"https://www.googleapis.com/language/translate/v2?key=my_key&q=hello+world&target=es",
67
+ :response=>{"data"=>{"translations"=>[{"translatedText"=>"hola mundo"}]}}}
68
+ </code></pre>
42
69
 
43
70
  h2. Supported Languages
44
71
 
45
- *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.
72
+ *to_lang* adds the following methods to strings. Each of these methods can be called with an explicit source language by appending @_from_source_language@ or prepending @from_source_language_@ to the method name.
46
73
 
47
74
  * to_afrikaans
48
75
  * to_albanian
49
76
  * to_arabic
50
- * to_basque
51
77
  * to_belarusian
52
78
  * to_bulgarian
53
79
  * to_catalan
@@ -104,8 +130,8 @@ API documentation can be found at "rubydoc.info":http://rubydoc.info/github/jimm
104
130
 
105
131
  h2. Roadmap
106
132
 
107
- * Add Unicode support for Ruby 1.8. *to_lang* currently works safely only under 1.9.
133
+ * Investigate Unicode support for Ruby 1.8. *to_lang* has only been tested with 1.9.
108
134
 
109
135
  h2. Feedback and Contributions
110
136
 
111
- Feedback is greatly appreciated, as are patch contributions. Feel free to fork the project and send me a pull request.
137
+ Feedback is greatly appreciated. If you have any problems with *to_lang*, please open a new issue. Make sure you are using the latest version of the gem, or HEAD if you've cloned the Git repository directly. Please include debugging output from using @String#translate@'s @:debug@ option, if relevant to your issue. If you'd like to fix bugs, add features, or improve the library in general, feel free to fork the project and send me a pull request with your changes.
@@ -5,7 +5,6 @@ module ToLang
5
5
  'afrikaans' => 'af',
6
6
  'albanian' => 'sq',
7
7
  'arabic' => 'ar',
8
- 'basque' => 'eu',
9
8
  'belarusian' => 'be',
10
9
  'bulgarian' => 'bg',
11
10
  'catalan' => 'ca',
@@ -28,12 +28,19 @@ module ToLang
28
28
  # @param [String] target The language code for the language to translate to.
29
29
  # @param [Hash] options A hash of options.
30
30
  # @option options [String] :from The language code for the language of @q@.
31
+ # @option options [Symbol] :debug Debug output to return instead of the translated string. Must be one of @:request@, @:response@, or @:all@.
31
32
  #
32
- # @raise [RuntimeError] Raises an error for any errors returned by Google Translate.
33
- # @return [String] The translated string.
33
+ # @raise [RuntimeError] Raises an exception for any errors returned by Google Translate.
34
+ # @return [String, Hash] The translated string or debugging output, as requested.
34
35
  #
35
36
  def request(q, target, options = {})
36
- response = HTTParty.get request_url(q, target, options)
37
+ request = request_url(q, target, options)
38
+ return request if options[:debug] == :request
39
+
40
+ response = HTTParty.get(request)
41
+ return { :request => request, :response => response.parsed_response } if options[:debug] == :all
42
+ return response.parsed_response if options[:debug] == :response
43
+
37
44
  raise response.parsed_response["error"]["message"] if response.parsed_response["error"] && response.parsed_response["error"]["message"]
38
45
  CGI.unescapeHTML(response.parsed_response["data"]["translations"][0]["translatedText"])
39
46
  end
@@ -34,6 +34,18 @@ module ToLang
34
34
  translate(CODEMAP[$1], :from => CODEMAP[$2])
35
35
  end
36
36
 
37
+ return send(new_method_name)
38
+ else
39
+ original_method_missing(method, *args, &block)
40
+ end
41
+ when /^from_(.*)_to_(.*)$/
42
+ if CODEMAP[$1] && CODEMAP[$2]
43
+ new_method_name = "from_#{$1}_to_#{$2}".to_sym
44
+
45
+ self.class.send(:define_method, new_method_name) do
46
+ translate(CODEMAP[$2], :from => CODEMAP[$1])
47
+ end
48
+
37
49
  return send(new_method_name)
38
50
  else
39
51
  original_method_missing(method, *args, &block)
@@ -66,6 +78,8 @@ module ToLang
66
78
  case method.to_s
67
79
  when /^to_(.*)_from_(.*)$/
68
80
  return true if CODEMAP[$1] && CODEMAP[$2]
81
+ when /^from_(.*)_to_(.*)$/
82
+ return true if CODEMAP[$1] && CODEMAP[$2]
69
83
  when /^to_(.*)$/
70
84
  return true if CODEMAP[$1]
71
85
  end
@@ -1,5 +1,5 @@
1
1
  module ToLang
2
2
  # The current version of the Ruby gem.
3
3
  #
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -9,7 +9,7 @@ describe ToLang::Connector do
9
9
  @connector.key.should_not be_nil
10
10
  end
11
11
 
12
- context "when sent :request" do
12
+ describe "#request" do
13
13
  def stub_response(parsed_response)
14
14
  mock_response = mock('HTTParty::Response', :parsed_response => parsed_response)
15
15
  HTTParty.stub(:get).and_return(mock_response)
@@ -18,11 +18,13 @@ describe ToLang::Connector do
18
18
  def stub_good_response(translated_text)
19
19
  parsed_response = { "data" => { "translations" => [ { "translatedText" => translated_text } ] } }
20
20
  stub_response(parsed_response)
21
+ parsed_response
21
22
  end
22
23
 
23
24
  def stub_bad_response(error_message)
24
25
  parsed_response = { "error" => { "message" => error_message } }
25
26
  stub_response(parsed_response)
27
+ parsed_response
26
28
  end
27
29
 
28
30
  context "with only a target language" do
@@ -54,5 +56,27 @@ describe ToLang::Connector do
54
56
  expect { @connector.request("a pie", "en", :from => "en") }.to raise_error(RuntimeError, "Bad language pair: en|en")
55
57
  end
56
58
  end
59
+
60
+ context "when debugging the request" do
61
+ it "returns the request URL" do
62
+ HTTParty.stub(:get)
63
+ @connector.request("hello world", "es", :from => "en", :debug => :request).should == "https://www.googleapis.com/language/translate/v2?key=apikey&q=hello+world&target=es&source=en"
64
+ end
65
+ end
66
+
67
+ context "when debugging the response" do
68
+ it "returns the full parsed response" do
69
+ expected_response = stub_good_response("hola mundo")
70
+ @connector.request("hello world", "es", :from => "en", :debug => :response).should == expected_response
71
+ end
72
+ end
73
+
74
+ context "when debugging the request and the response" do
75
+ it "returns a hash with the request URL and the full parsed response" do
76
+ expected_response = stub_good_response("hola mundo")
77
+ output = @connector.request("hello world", "es", :from => "en", :debug => :all)
78
+ output.should == { :request => "https://www.googleapis.com/language/translate/v2?key=apikey&q=hello+world&target=es&source=en", :response => expected_response }
79
+ end
80
+ end
57
81
  end
58
82
  end
@@ -1,15 +1,11 @@
1
1
  require "spec_helper"
2
2
 
3
- describe "A ToLang-enabled string" do
3
+ describe String do
4
4
  before :all do
5
5
  ToLang.start('apikey')
6
6
  end
7
7
 
8
- it "responds to :translate" do
9
- String.new.should respond_to :translate
10
- end
11
-
12
- context "when sent :translate" do
8
+ describe "#translate" do
13
9
  it "calls ToLang::Connector#request" do
14
10
  ToLang.connector.stub(:request)
15
11
  ToLang.connector.should_receive(:request).with("hello world", "es")
@@ -17,41 +13,47 @@ describe "A ToLang-enabled string" do
17
13
  end
18
14
  end
19
15
 
20
- ToLang::CODEMAP.each do |language, code|
21
- it "will respond_to :to_#{language}" do
22
- "hello_world".should respond_to "to_#{language}"
23
- end
16
+ it "will respond to :to_<language>" do
17
+ "hello world".should respond_to :to_spanish
18
+ end
24
19
 
25
- it "will respond to :to_#{language}_from_english" do
26
- "hello_world".should respond_to "to_#{language}_from_english"
27
- end
20
+ it "will respond to :to_<target>_from_<source>" do
21
+ "hello world".should respond_to :to_spanish_from_english
22
+ end
28
23
 
29
- it "translates to #{language} when sent :to_#{language}" do
30
- ToLang.connector.stub(:request)
31
- ToLang.connector.should_receive(:request).with("hello world", code)
32
- "hello world".send("to_#{language}")
33
- end
24
+ it "will respond to :from_<source>_to_<target>" do
25
+ "hello world".should respond_to :from_english_to_spanish
26
+ end
34
27
 
35
- it "translates to #{language} from english when sent :to_#{language}_from_english" do
36
- ToLang.connector.stub(:request)
37
- ToLang.connector.should_receive(:request).with("hello world", code, :from => 'en')
38
- "hello world".send("to_#{language}_from_english")
39
- end
28
+ it "translates to <language> when sent :to_<language>" do
29
+ ToLang.connector.stub(:request)
30
+ ToLang.connector.should_receive(:request).with("hello world", 'es')
31
+ "hello world".send(:to_spanish)
40
32
  end
41
33
 
42
- context "when a magic method has been called once" do
43
- before :each do
44
- ToLang.connector.stub(:request)
45
- "hello world".to_spanish
46
- "hello world".to_spanish_from_english
47
- end
34
+ it "translates to <target> from <source> when sent :to_<target>_from_<source>" do
35
+ ToLang.connector.stub(:request)
36
+ ToLang.connector.should_receive(:request).with("hello world", 'es', :from => 'en')
37
+ "hello world".send(:to_spanish_from_english)
38
+ end
48
39
 
49
- it "defines the method and does not call :method_missing the next time" do
50
- string = "hello world"
51
- string.should_not_receive(:method_missing)
40
+ it "translates to <target> from <source> when sent :from_<source>_to_<target>" do
41
+ ToLang.connector.stub(:request)
42
+ ToLang.connector.should_receive(:request).with("hello world", 'es', :from => 'en')
43
+ "hello world".send(:from_english_to_spanish)
44
+ end
45
+
46
+ it "defines magic methods when first called and doesn't call :method_missing after that" do
47
+ ToLang.connector.stub(:request)
48
+ string = "hello world"
49
+ magic_methods = lambda do
52
50
  string.to_spanish
53
51
  string.to_spanish_from_english
52
+ string.from_english_to_spanish
54
53
  end
54
+ magic_methods.call
55
+ string.should_not_receive(:method_missing)
56
+ magic_methods.call
55
57
  end
56
58
 
57
59
  it "calls the original :method_missing if there is no language match in the first form" do
@@ -62,6 +64,10 @@ describe "A ToLang-enabled string" do
62
64
  expect { "hello world".to_foo_from_bar }.to raise_error(NoMethodError)
63
65
  end
64
66
 
67
+ it "calls the original :method_missing if there is a bad language match in the reversed second form" do
68
+ expect { "hello world".from_bar_to_foo }.to raise_error(NoMethodError)
69
+ end
70
+
65
71
  it "calls the original :method_missing if the method does not match either form" do
66
72
  expect { "hello world".foo }.to raise_error(NoMethodError)
67
73
  end
data/spec/to_lang_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe ToLang do
4
- context "when sent :start" do
4
+ describe ".start" do
5
5
  before :all do
6
6
  ToLang.start('apikey')
7
7
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jimmy Cuadra
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-29 00:00:00 -08:00
17
+ date: 2011-01-04 00:00:00 -08:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency