to_lang 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,8 @@ Google Translate attempts to detect the source language, but you can specify it
14
14
 
15
15
  The dynamic methods are simply syntactic sugar for @String#translate@, which you can use directly as well.
16
16
 
17
+ Google Translate currently has a limit of 5,000 characters on input, so any strings longer than that will raise an exception.
18
+
17
19
  h2. Examples
18
20
 
19
21
  Load and initialize *to_lang*:
@@ -5,10 +5,16 @@ module ToLang
5
5
  # Responsible for making the actual HTTP request to the Google Translate API.
6
6
  #
7
7
  class Connector
8
+ include HTTParty
9
+
8
10
  # The base URL for all requests to the Google Translate API.
9
11
  #
10
12
  API_URL = "https://www.googleapis.com/language/translate/v2"
11
13
 
14
+ # The maximum number of characters the string to translate can be.
15
+ #
16
+ MAX_STRING_LENGTH = 5000
17
+
12
18
  # The Google Translate API key to use when making API calls.
13
19
  #
14
20
  # @return [String] The Google Translate API key.
@@ -20,6 +26,7 @@ module ToLang
20
26
  # @return [ToLang::Connector] The new {ToLang::Connector} instance.
21
27
  def initialize(key)
22
28
  @key = key
29
+ self.class.headers "X-HTTP-Method-Override" => "GET"
23
30
  end
24
31
 
25
32
  # Makes a request to the Google Translate API.
@@ -30,37 +37,22 @@ module ToLang
30
37
  # @option options [String] :from The language code for the language of @q@.
31
38
  # @option options [Symbol] :debug Debug output to return instead of the translated string. Must be one of @:request@, @:response@, or @:all@.
32
39
  #
33
- # @raise [RuntimeError] Raises an exception for any errors returned by Google Translate.
40
+ # @raise [RuntimeError] If the string is too long or if Google Translate returns any errors.
34
41
  # @return [String, Hash] The translated string or debugging output, as requested.
35
42
  #
36
43
  def request(q, target, options = {})
37
- request = request_url(q, target, options)
38
- return request if options[:debug] == :request
44
+ raise "The string to translate cannot be greater than #{MAX_STRING_LENGTH} characters" if q.size > MAX_STRING_LENGTH
45
+
46
+ request_hash = { :key => @key, :q => q, :target => target }
47
+ request_hash[:source] = options[:from] if options[:from]
48
+ return request_hash if options[:debug] == :request
39
49
 
40
- response = HTTParty.get(request)
41
- return { :request => request, :response => response.parsed_response } if options[:debug] == :all
50
+ response = self.class.post(API_URL, { :body => request_hash })
42
51
  return response.parsed_response if options[:debug] == :response
52
+ return { :request => request_hash, :response => response.parsed_response } if options[:debug] == :all
43
53
 
44
54
  raise response.parsed_response["error"]["message"] if response.parsed_response["error"] && response.parsed_response["error"]["message"]
45
55
  CGI.unescapeHTML(response.parsed_response["data"]["translations"][0]["translatedText"])
46
56
  end
47
-
48
- private
49
-
50
- # Constructs the URL that will be used by {#request}.
51
- #
52
- # @param [String] q The string to translate.
53
- # @param [String] target The language code for the language to translate to.
54
- # @param [Hash] options A hash of options.
55
- # @option options [String] :from The language code for the language of @q@.
56
- #
57
- # @return [String] The URL to request for the API call.
58
- #
59
- def request_url(q, target, options)
60
- source = options[:from]
61
- url = "#{API_URL}?key=#{@key}&q=#{CGI.escape(q)}&target=#{target}"
62
- url += "&source=#{source}" if source
63
- url
64
- end
65
57
  end
66
58
  end
@@ -1,5 +1,5 @@
1
1
  module ToLang
2
2
  # The current version of the Ruby gem.
3
3
  #
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -12,7 +12,7 @@ describe ToLang::Connector do
12
12
  describe "#request" do
13
13
  def stub_response(parsed_response)
14
14
  mock_response = mock('HTTParty::Response', :parsed_response => parsed_response)
15
- HTTParty.stub(:get).and_return(mock_response)
15
+ ToLang::Connector.stub(:post).and_return(mock_response)
16
16
  end
17
17
 
18
18
  def stub_good_response(translated_text)
@@ -59,8 +59,7 @@ describe ToLang::Connector do
59
59
 
60
60
  context "when debugging the request" do
61
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"
62
+ @connector.request("hello world", "es", :from => "en", :debug => :request).should == { :key=> "apikey", :q => "hello world", :target => "es", :source => "en" }
64
63
  end
65
64
  end
66
65
 
@@ -75,8 +74,13 @@ describe ToLang::Connector do
75
74
  it "returns a hash with the request URL and the full parsed response" do
76
75
  expected_response = stub_good_response("hola mundo")
77
76
  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 }
77
+ output.should == { :request => { :key=> "apikey", :q => "hello world", :target => "es", :source => "en" }, :response => expected_response }
79
78
  end
80
79
  end
80
+
81
+ it "raises an exception if the encoding string is longer than the maximum allowed characters" do
82
+ str = 'x' * (ToLang::Connector::MAX_STRING_LENGTH + 1)
83
+ expect { @connector.request(str, "en", :from => "ja")}.to raise_error(RuntimeError, "The string to translate cannot be greater than #{ToLang::Connector::MAX_STRING_LENGTH} characters")
84
+ end
81
85
  end
82
86
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jimmy Cuadra