Class: HotelBeds::Parser::Errors

Inherits:
Object
  • Object
show all
Defined in:
lib/hotel_beds/parser/errors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Errors

Returns a new instance of Errors.



9
10
11
12
# File 'lib/hotel_beds/parser/errors.rb', line 9

def initialize(response)
  self.response = response
  freeze
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/hotel_beds/parser/errors.rb', line 6

def response
  @response
end

Instance Method Details

#to_model(object) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hotel_beds/parser/errors.rb', line 14

def to_model(object)
  ActiveModel::Errors.new(object).tap do |errors|
    if response.http_error?
      errors.add(:base, "HTTP error")
    elsif response.soap_fault?
      errors.add(:base, "SOAP error")
    elsif !response.success?
      errors.add(:base, "Request failed")
    end

    body.css("ErrorList Error").each do |error|
      errors.add(:base, [
        (sm = error.at_css("Message")) && sm.content,
        (dm = error.at_css("DetailedMessage")) && dm.content
      ].compact.join("\n"))
    end
  end
end