Class: C3po::Translator::Bing

Inherits:
Object
  • Object
show all
Defined in:
lib/c3po/translator/bing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_urlObject (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_queryHash

Build a query for detect method of Bing Translate api.

Examples:

build_detect_query

Returns:

  • (Hash)

    Hash of param.

Since:

  • 0.0.1



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_queryObject



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.

Examples:

build_query :fr, :en

Parameters:

  • Language (Symbol)

    to be translated from.

    Symbol

    Language to be translated to.

Returns:

  • (Hash)

    Hash of param.

Since:

  • 0.0.1



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.

Examples:

parse my_xml

Parameters:

  • response (String)

    Json representation

Returns:

  • (String)

    Translated string.

Since:

  • 0.0.1



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