easy_translate 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,8 +9,8 @@ module EasyTranslate
9
9
  # @param [String, Array] texts - A single string or set of strings to detect for
10
10
  # @param [Hash] options - Extra options to pass along with the request
11
11
  # @return [String, Array] The resultant language or languages
12
- def detect(texts, options = nil)
13
- request = DetectionRequest.new(texts, options)
12
+ def detect(texts, options = nil, http_options={})
13
+ request = DetectionRequest.new(texts, options, http_options)
14
14
  # Turn the response into an array of detections
15
15
  raw = request.perform_raw
16
16
  detections = JSON.parse(raw)['data']['detections'].map do |res|
@@ -26,14 +26,12 @@ module EasyTranslate
26
26
  # Set the texts and options
27
27
  # @param [String, Array] texts - The text (or texts) to translate
28
28
  # @param [Hash] options - Options to override or pass along with the request
29
- def initialize(texts, options = nil)
30
- self.texts = texts
31
- if options
32
- @options = options
33
- if replacement_api_key = @options.delete(:api_key)
34
- @options[:key] = replacement_api_key
35
- end
29
+ def initialize(texts, options = {}, http_options = {})
30
+ super(options, http_options)
31
+ if replacement_api_key = @options.delete(:api_key)
32
+ @options[:key] = replacement_api_key
36
33
  end
34
+ self.texts = texts
37
35
  end
38
36
 
39
37
  # The params for this request
@@ -5,6 +5,12 @@ require 'uri'
5
5
  module EasyTranslate
6
6
 
7
7
  class Request
8
+ attr_accessor :http_options
9
+
10
+ def initialize(options={}, http_options={})
11
+ @options = options
12
+ @http_options = http_options
13
+ end
8
14
 
9
15
  # Body, blank by default
10
16
  # @return [String] The body for this request
@@ -30,11 +36,6 @@ module EasyTranslate
30
36
  # Perform the given request
31
37
  # @return [String] The response String
32
38
  def perform_raw
33
- # Get the URI
34
- uri = URI.parse("https://www.googleapis.com#{path}?#{param_s}")
35
- # Open the HTTP object
36
- http = Net::HTTP.new(uri.host, uri.port)
37
- http.use_ssl = true
38
39
  # Construct the request
39
40
  request = Net::HTTP::Post.new(uri.request_uri)
40
41
  request.add_field('X-HTTP-Method-Override', 'GET')
@@ -50,6 +51,47 @@ module EasyTranslate
50
51
 
51
52
  private
52
53
 
54
+ def uri
55
+ @uri ||= URI.parse("https://www.googleapis.com#{path}?#{param_s}")
56
+ end
57
+
58
+ def http
59
+ @http ||= Net::HTTP.new(uri.host, uri.port).tap do |http|
60
+ configure_timeouts(http)
61
+ configure_ssl(http)
62
+ end
63
+ end
64
+
65
+ def configure_timeouts(http)
66
+ http.read_timeout = http.open_timeout = http_options[:timeout] if http_options[:timeout]
67
+ http.open_timeout = http_options[:open_timeout] if http_options[:open_timeout]
68
+ end
69
+
70
+ def configure_ssl(http)
71
+ http.use_ssl = true
72
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
73
+ http.cert_store = ssl_cert_store
74
+
75
+ http.cert = ssl_options[:client_cert] if ssl_options[:client_cert]
76
+ http.key = ssl_options[:client_key] if ssl_options[:client_key]
77
+ http.ca_file = ssl_options[:ca_file] if ssl_options[:ca_file]
78
+ http.ca_path = ssl_options[:ca_path] if ssl_options[:ca_path]
79
+ http.verify_depth = ssl_options[:verify_depth] if ssl_options[:verify_depth]
80
+ http.ssl_version = ssl_options[:version] if ssl_options[:version]
81
+ end
82
+
83
+ def ssl_cert_store
84
+ return ssl_options[:cert_store] if ssl_options[:cert_store]
85
+ # Use the default cert store by default, i.e. system ca certs
86
+ cert_store = OpenSSL::X509::Store.new
87
+ cert_store.set_default_paths
88
+ cert_store
89
+ end
90
+
91
+ def ssl_options
92
+ http_options[:ssl] || {}
93
+ end
94
+
53
95
  # Stringify the params
54
96
  # @return [String] The params as a string
55
97
  def param_s
@@ -11,8 +11,8 @@ module EasyTranslate
11
11
  # @option options [String, Symbol] :target - The target language (required)
12
12
  # @option options [Boolean] :html - Whether or not the supplied string is HTML (optional)
13
13
  # @return [String, Array] Translated text or texts
14
- def translate(texts, options = {})
15
- request = TranslationRequest.new(texts, options)
14
+ def translate(texts, options = {}, http_options={})
15
+ request = TranslationRequest.new(texts, options, http_options)
16
16
  # Turn the response into an array of translations
17
17
  raw = request.perform_raw
18
18
  translations = JSON.parse(raw)['data']['translations'].map do |res|
@@ -28,13 +28,14 @@ module EasyTranslate
28
28
  # Set the texts and options
29
29
  # @param [String, Array] texts - the text (or texts) to translate
30
30
  # @param [Hash] options - Options to override or pass along with the request
31
- def initialize(texts, options)
31
+ def initialize(texts, options, http_options={})
32
32
  self.texts = texts
33
33
  self.html = options.delete(:html)
34
34
  @source = options.delete(:from)
35
35
  @target = options.delete(:to)
36
36
  raise ArgumentError.new('No target language provded') unless @target
37
37
  raise ArgumentError.new('Support for multiple targets dropped in V2') if @target.is_a?(Array)
38
+ @http_options = http_options
38
39
  if options
39
40
  @options = options
40
41
  if replacement_api_key = @options.delete(:api_key)
@@ -1,5 +1,5 @@
1
1
  module EasyTranslate
2
2
 
3
- VERSION = '0.3.1'
3
+ VERSION = '0.3.2'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-16 00:00:00.000000000Z
12
+ date: 2013-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &18311100 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *18311100
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: json
27
- requirement: &18310680 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,7 +37,12 @@ dependencies:
32
37
  version: '0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *18310680
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  description: easy_translate is a wrapper for the google translate API that makes sense
37
47
  programatically, and implements API keys
38
48
  email: john.crepezzi@gmail.com
@@ -40,13 +50,13 @@ executables: []
40
50
  extensions: []
41
51
  extra_rdoc_files: []
42
52
  files:
53
+ - lib/easy_translate/detection.rb
54
+ - lib/easy_translate/easy_translate_exception.rb
43
55
  - lib/easy_translate/languages.rb
44
56
  - lib/easy_translate/request.rb
57
+ - lib/easy_translate/translation.rb
45
58
  - lib/easy_translate/translation_target.rb
46
59
  - lib/easy_translate/version.rb
47
- - lib/easy_translate/easy_translate_exception.rb
48
- - lib/easy_translate/translation.rb
49
- - lib/easy_translate/detection.rb
50
60
  - lib/easy_translate.rb
51
61
  - spec/spec_helper.rb
52
62
  homepage: https://github.com/seejohnrun/easy_translate
@@ -69,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
79
  version: '0'
70
80
  requirements: []
71
81
  rubyforge_project:
72
- rubygems_version: 1.8.15
82
+ rubygems_version: 1.8.24
73
83
  signing_key:
74
84
  specification_version: 3
75
85
  summary: Google Translate API Wrapper for Ruby