Sha256: 1e985450e34cf1aa71e67cafafc118bef8406d3612c788c495b09146adfadc5a

Contents?: true

Size: 923 Bytes

Versions: 2

Compression:

Stored size: 923 Bytes

Contents

module Udongo::Redirects
  class Response
    def initialize(response)
      @response = response
    end

    def endpoint_matches?(destination)
      sanitize_destination(destination) == headers['Location']
    end

    # Apparently Curb does not provide parsed headers... A bit sad.
    # TODO: Handle multiple location headers so #endpoint_matches? can succeed.
    # For now, the last location header is returned as a value.
    def headers
      list = @response.header_str.split(/[\r\n]+/).map(&:strip)
      Hash[list.flat_map{ |s| s.scan(/^(\S+): (.+)/) }.map { |pair|
        [pair.first.to_s.camelcase, pair.second]
      }]
    end

    def raw
      @response
    end

    def sanitize_destination(destination)
      destination.to_s
                 .gsub('/?', '?')
                 .chomp('/')
    end

    def success?
      ['200 OK', '301 Moved Permanently'].include?(@response.status)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
udongo-7.9.0 lib/udongo/redirects/response.rb
udongo-7.8.1 lib/udongo/redirects/response.rb