Class: GTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/gtranslate-two.rb

Constant Summary collapse

@@key =
nil

Class Method Summary collapse

Class Method Details

.detect_language(query) ⇒ Object



19
20
21
# File 'lib/gtranslate-two.rb', line 19

def self.detect_language(query)
  translate(query, 'en').source
end

.keyObject



11
12
13
# File 'lib/gtranslate-two.rb', line 11

def self.key
  @@key
end

.key=(key) ⇒ Object



15
16
17
# File 'lib/gtranslate-two.rb', line 15

def self.key=(key)
  @@key = key
end

.translate(query, target, source = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gtranslate-two.rb', line 23

def self.translate(query, target, source=nil)
  
  # Step 1a: make an array of required parameters to send to the Google Translate server
  options = { :q => query, :target => target, :key => @@key } 
  
  # Step 1b: Add the optional source parameter if it has been explicitly specified
  options[:source] = source if source
  
  # Step 1c: Serialize the hash for the POST request
  params = URI.escape(options.collect{|k,v| "#{k}=#{v}"}.join('&'))
  
  # Step 2a: Get the Google Translate host address
  uri = URI.parse('https://www.googleapis.com/language/translate/v2')
  
  # Step 2b: Create a new HTTP client from the provided host address
  http = Net::HTTP.new(uri.host, uri.port)
  
  # Step 2c: Force the HTTP client to use SSL and assign a proper certificate
  http.use_ssl = true
  http.ssl_timeout = 2
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  store = OpenSSL::X509::Store.new
  store.set_default_paths
  http.cert_store = store
  
  # Step 2d: Make an actual POST request and grab the response and it's body
  response, body = http.post(uri.path, params, { 'X-HTTP-Method-Override' => 'GET' })
  
  # Step 3a: If the response indicates an HTTP OK code (200), parse the body as JSON
  if response.is_a?(Net::HTTPOK)
    # Step 3b: The JSON response has 1 or more 'translations' - out of each one...
    result = JSON.parse(body)['data']['translations'].collect do |params|
      #... create a new GTranslatorResult object
      GTranslatorResult.new(query, target, params, source)
    end
    # Step 4a: If all went well, return the resulting GTranslatorResult or an array of those 
    #          if more than one translation has been returned
    return result.size == 1 ? result.first : result
  else
    # Step 4b: Otherwise, return the error response (generally, a Net::HTTPBadRequest)
    response
  end
end