Class: MicrosoftTranslator

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

Constant Summary collapse

CONFIG =
YAML.load_file("./config/idiom.yml")

Class Method Summary collapse

Class Method Details

.t(value, code) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/idiom/microsoft_translator.rb', line 5

def t(value, code)
  value = URI.encode(value)
  # "http://api.microsofttranslator.com/V2/Http.svc/Translate?to=#{code}&text=#{}&appId=2CEF8B6B9CA38C6C8355B154C760C28A66E4339F"
  
  appId = CONFIG["appId"]
  url = "http://api.microsofttranslator.com/V2/Http.svc/Translate?to=#{code}&text=#{value}&appId=#{appId}"
  result = Net::HTTP.get(URI.parse(url))
  
  if result =~ /<string xmlns=\"http:\/\/schemas.microsoft.com\/2003\/10\/Serialization\/\">(.*)<\/string>/
    output = $1
  end
  if result =~ /Message: (.*)/
    $stdout.puts(result)
  end
  if result =~ /AppId is over the quota/
    raise "AppId is over the quota"
  end
  output.to_s
end