class Translatomatic::HTTP::Exception

Exception used for unexpected http responses

Attributes

response[R]

Public Class Methods

new(response) click to toggle source
# File lib/translatomatic/http/exception.rb, line 7
def initialize(response)
  @response = response
end

Public Instance Methods

to_s() click to toggle source

Exception string

# File lib/translatomatic/http/exception.rb, line 12
def to_s
  error_from_response(@response) || default_error
end

Private Instance Methods

default_error() click to toggle source
# File lib/translatomatic/http/exception.rb, line 18
def default_error
  "error #{@response.code}"
end
error_from_html(body) click to toggle source
# File lib/translatomatic/http/exception.rb, line 30
def error_from_html(body)
  doc = Nokogiri::HTML(body)
  texts = doc.search('//text()')
  msg = texts.find { |i| error_from_string(i.content) }
  msg ? error_from_string(msg.content) : nil
end
error_from_response(response) click to toggle source
# File lib/translatomatic/http/exception.rb, line 22
def error_from_response(response)
  if response.content_type == 'text/html'
    error_from_html(response.body)
  elsif response.body.present?
    response.body
  end
end
error_from_string(string) click to toggle source
# File lib/translatomatic/http/exception.rb, line 37
def error_from_string(string)
  match = string.match(/(?:Message|Error):\s*(.*)/i)
  match ? match[1] : nil
end