Class: Wayfarer::Networking::Follow

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfarer/networking/follow.rb

Constant Summary collapse

RedirectsExhaustedError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Follow

Returns a new instance of Follow.



14
15
16
# File 'lib/wayfarer/networking/follow.rb', line 14

def initialize(context)
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/wayfarer/networking/follow.rb', line 12

def context
  @context
end

Class Method Details

.httpObject



8
9
10
# File 'lib/wayfarer/networking/follow.rb', line 8

def self.http
  new(Wayfarer::Networking::Context.new(Wayfarer::Networking::HTTP.new))
end

Instance Method Details

#fetch(url, follow:) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/wayfarer/networking/follow.rb', line 18

def fetch(url, follow:)
  raise RedirectsExhaustedError if follow < 0

  case result = context.fetch(url)
  when Result::Success then result.page
  when Result::Redirect then fetch(result.redirect_url, follow: follow - 1)
  end
end