Class: Mymemory::Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/mymemory/translation.rb

Constant Summary collapse

BASE_URL =
'http://mymemory.translated.net/api/get'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, opts) ⇒ Translation

Returns a new instance of Translation.



10
11
12
13
14
# File 'lib/mymemory/translation.rb', line 10

def initialize(text, opts)
  @text = text
  @from = opts.fetch(:from, :en)
  @to   = opts[:to] or raise LanguageMissingError.new
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



8
9
10
# File 'lib/mymemory/translation.rb', line 8

def from
  @from
end

#response_jsonObject

Returns the value of attribute response_json.



8
9
10
# File 'lib/mymemory/translation.rb', line 8

def response_json
  @response_json
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/mymemory/translation.rb', line 8

def text
  @text
end

#toObject

Returns the value of attribute to.



8
9
10
# File 'lib/mymemory/translation.rb', line 8

def to
  @to
end

Instance Method Details

#email_keyObject



24
25
26
# File 'lib/mymemory/translation.rb', line 24

def email_key
  config_email && "&de=#{config_email}"
end

#lang_pairObject



20
21
22
# File 'lib/mymemory/translation.rb', line 20

def lang_pair
  [from, to].join('|')
end

#parsed_responseObject



32
33
34
# File 'lib/mymemory/translation.rb', line 32

def parsed_response
  response.code == 200 ? JSON.parse(response.body) : {}
end

#translated_textObject



28
29
30
# File 'lib/mymemory/translation.rb', line 28

def translated_text
  parsed_response['responseData']['translatedText']
end

#urlObject



16
17
18
# File 'lib/mymemory/translation.rb', line 16

def url
  URI.escape("#{BASE_URL}?q=#{text}&langpair=#{lang_pair}#{email_key}")
end