Class: C3po::Translator::Bing
- Inherits:
-
Object
- Object
- C3po::Translator::Bing
- Defined in:
- lib/c3po/translator/bing.rb
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
-
#build_detect_query ⇒ Hash
Build a query for detect method of Bing Translate api.
- #build_languages_query ⇒ Object
-
#build_query(from, to) ⇒ Hash
Build a query for the Bing Translate api.
-
#initialize(to_be_translated) ⇒ Bing
constructor
A new instance of Bing.
-
#parse(response) ⇒ String
Parse xml response from Bing webservice.
Constructor Details
#initialize(to_be_translated) ⇒ Bing
Returns a new instance of Bing.
9 10 11 12 13 14 |
# File 'lib/c3po/translator/bing.rb', line 9 def initialize(to_be_translated) @to_be_translated = to_be_translated @default = { :appId => C3po::Translator::Configuration.bing_api_key, } end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
7 8 9 |
# File 'lib/c3po/translator/bing.rb', line 7 def base_url @base_url end |
Instance Method Details
#build_detect_query ⇒ Hash
Build a query for detect method of Bing Translate api.
50 51 52 53 |
# File 'lib/c3po/translator/bing.rb', line 50 def build_detect_query @base_url = 'http://api.microsofttranslator.com/V2/Http.svc/Detect' @default.merge({:text => @to_be_translated}) end |
#build_languages_query ⇒ Object
36 37 38 39 |
# File 'lib/c3po/translator/bing.rb', line 36 def build_languages_query @base_url = 'http://api.microsofttranslator.com/V1/Http.svc/GetLanguages' @default end |
#build_query(from, to) ⇒ Hash
Build a query for the Bing Translate api.
28 29 30 31 32 33 34 |
# File 'lib/c3po/translator/bing.rb', line 28 def build_query(from, to) @base_url = 'http://api.microsofttranslator.com/V2/Http.svc/Translate' @default.merge({:text => @to_be_translated, :from => from.to_s, :to => to.to_s }) end |
#parse(response) ⇒ String
Parse xml response from Bing webservice.
XML is serious business.
68 69 70 71 72 |
# File 'lib/c3po/translator/bing.rb', line 68 def parse(response) xpath = Nokogiri::XML(response).xpath('/') return xpath.text unless xpath.children.empty? response.to_s.split("\r\n") end |