Class: Emarsys::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/emarsys/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#codeObject

Returns the value of attribute code.



4
5
6
# File 'lib/emarsys/response.rb', line 4

def code
  @code
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/emarsys/response.rb', line 4

def data
  @data
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/emarsys/response.rb', line 4

def status
  @status
end

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/emarsys/response.rb', line 4

def text
  @text
end