Class: Gizoogle::String

Inherits:
Object
  • Object
show all
Defined in:
lib/gizoogle/string.rb

Overview

Translate your strings into gangsta

Class Method Summary collapse

Class Method Details

.translate(str) ⇒ Object

Translate a single string, returns translated string

Example:

>> Gizoogle::String.translate('hello world')
=> wassup ghetto

Arguments:

str: (String)

17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gizoogle/string.rb', line 17

def self.translate(str)
  params = { 'translatetext' => str }
  url = URI('http://www.gizoogle.net/textilizer.php')
  resp = Net::HTTP.post_form(url, params)
  
  # gizoogle returns invalid html, let's fix that before parsing
  body = resp.body.gsub(/name="translatetext"[^>]*>/, 
                        'name="translatetext" >')

  html_doc = Nokogiri::HTML(body)
  textarea = html_doc.css('textarea[name=translatetext]').first
  
  textarea ? textarea.content.strip : str
end