Class: MicrosoftTranslator::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/microsoft_translator/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
9
10
# File 'lib/microsoft_translator/client.rb', line 4

def initialize(client_id, client_secret)
  @client_id = client_id
  raise "client_id must be a string" unless @client_id.is_a?(String)
  @client_secret = client_secret
  raise "client_secret must be a string" unless @client_secret.is_a?(String)
  @auth = MicrosoftTranslator::AzureAuthentication.new(client_id, client_secret)
end

Instance Method Details

#detect(text) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/microsoft_translator/client.rb', line 24

def detect(text)
  begin
    response = RestClient.get(
      "http://api.microsofttranslator.com/V2/Http.svc/Detect",
      detect_params(text)
    )
    parse_xml(response.body)
  rescue RestClient::BadRequest
    false
  end
end

#translate(text, from_lang, to_lang, content_type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/microsoft_translator/client.rb', line 12

def translate(text, from_lang, to_lang, content_type)
  begin
    response = RestClient.get(
      "http://api.microsofttranslator.com/V2/Http.svc/Translate",
      translate_params(text, from_lang, to_lang, content_type)
    )
    parse_xml(response.body)
  rescue RestClient::BadRequest
    false
  end
end