Class: CTA::CustomerAlerts::Parser

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/cta_redux/faraday_middleware/customer_alerts_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, debug) ⇒ Parser

Returns a new instance of Parser.



4
5
6
7
# File 'lib/cta_redux/faraday_middleware/customer_alerts_parser.rb', line 4

def initialize(app, debug)
  @debug = debug
  super(app)
end

Instance Method Details

#call(request_env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cta_redux/faraday_middleware/customer_alerts_parser.rb', line 9

def call(request_env)
  api_response = nil

  @app.call(request_env).on_complete do |response_env|
    parsed_body = ::MultiXml.parse(response_env.body)

    if has_errors?(parsed_body)
      api_response = CTA::API::Response.new(parsed_body, response_env.body, @debug)
    else
      case response_env.url.to_s
      when /routes\.aspx/
        api_response = RouteStatusResponse.new(parsed_body, response_env.body, @debug)
      when /alerts\.aspx/
        api_response = AlertsResponse.new(parsed_body, response_env.body, @debug)
      end
    end
  end

  api_response
end

#has_errors?(parsed_body) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
# File 'lib/cta_redux/faraday_middleware/customer_alerts_parser.rb', line 30

def has_errors?(parsed_body)
  if parsed_body["CTARoutes"]
    Array.wrap(parsed_body["CTARoutes"]["ErrorCode"]).flatten.compact.uniq.first.to_i != 0
  else
    Array.wrap(parsed_body["CTAAlerts"]["ErrorCode"]).flatten.compact.uniq.first.to_i != 0
  end
end