Class: Udongo::Redirects::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



3
4
5
# File 'lib/udongo/redirects/response.rb', line 3

def initialize(response)
  @response = response
end

Instance Method Details

#endpoint_matches?(destination) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/udongo/redirects/response.rb', line 7

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

#headersObject

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.



14
15
16
17
18
19
# File 'lib/udongo/redirects/response.rb', line 14

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

#rawObject



21
22
23
# File 'lib/udongo/redirects/response.rb', line 21

def raw
  @response
end

#sanitize_destination(destination) ⇒ Object



25
26
27
28
29
# File 'lib/udongo/redirects/response.rb', line 25

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

#success?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/udongo/redirects/response.rb', line 31

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