Sha256: debfde1411f1e12f8a8ff11540ef41ea405728e9c61298c66ca797dbdd9de9d2

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Sappy
  class Response
    class SessionExpired < Error; end
    class UnhandledError < Error; end

    def self.parse(xml)
      r = new(xml)
      r.parse
      r
    end

    attr_reader :data, :xml

    def initialize(data)
      @data = data.to_s
      @xml  = Nokogiri::XML.parse(data)
    end

    def error_response?
      resp = xml.xpath('//rsp')
      resp && resp.first['stat'] == 'fail'
    end

    def error_response
      resp = xml.xpath('//err')
      resp && resp.first
    end

    def first_xpath(path)
      node = xml.xpath(path)
      node && node.first
    end

    def parse
      if error_response?
        error = error_response
        message = error["msg"]
        case code = error["code"]
        when "AUTH_EXPIRED"
          raise SessionExpired, "The auth session expired, reconnect to continue using the API"
        else
          failure(code, message)
          raise UnhandledError, "Unhandled error: #{code}, #{message}"
        end
      else
        success
      end
    end

    def success
      raise NotImplementedError, "Overwrite #success in a Response subclass"
    end

    def failure(code, message)
      case code
      when "WRONG_DATA"
        raise ArgumentError, "You didn't provide a correct monitor id: #{message}"
      else
        raise NotImplementedError, "Overwrite #failure in a Response subclass"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sappy-0.1.7 lib/sappy/response.rb
sappy-0.1.6 lib/sappy/response.rb
sappy-0.1.5 lib/sappy/response.rb