class Emarsys::Response

Attributes

code[RW]
data[RW]
status[RW]
text[RW]

Public Class Methods

new(response) click to toggle source
# File lib/emarsys/response.rb, line 6
def initialize(response)
  if response.headers[:content_type]&.start_with?('text/csv')
    self.code = 0
    self.data = response.body
  else
    json = JSON.parse(response)
    self.code = json['replyCode']
    self.text = json['replyText']
    self.data = json['data']
  end

  self.status = response.code if response.respond_to?(:code)

  if code != 0
    if status == 401
      raise Emarsys::Unauthorized.new(code, text, status)
    elsif status == 429
      raise Emarsys::TooManyRequests.new(code, text, status)
    else
      raise Emarsys::BadRequest.new(code, text, status)
    end
  end
end