Class: Recluse::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/recluse/queue.rb

Overview

Link checker

Instance Method Summary collapse

Constructor Details

#initialize(email, redirect: false) ⇒ Queue

Create an empty queue



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/recluse/queue.rb', line 10

def initialize(email, redirect: false)
  @links = []
  @run_if = proc { true }
  @on_complete = proc { |link, response| }
  @redirect = redirect
  @email = email
  @agent = Mechanize.new do |a|
    a.ssl_version = 'TLSv1'
    a.verify_mode = OpenSSL::SSL::VERIFY_NONE
    a.max_history = nil
    a.follow_meta_refresh = true
    a.keep_alive = false
    a.redirect_ok = @redirect
    a.user_agent = "Mozilla/5.0 (compatible; recluse/#{Recluse::VERSION}; +#{Recluse::URL}) #{@email}"
  end
end

Instance Method Details

#add(link) ⇒ Object

Add to queue.



29
30
31
# File 'lib/recluse/queue.rb', line 29

def add(link)
  @links += [*link]
end

#on_complete(&block) ⇒ Object

Run when a link has been checked. Procedure takes the link and response as inputs.



41
42
43
# File 'lib/recluse/queue.rb', line 41

def on_complete(&block)
  @on_complete = block
end

#runObject

Run queue



67
68
69
70
71
72
# File 'lib/recluse/queue.rb', line 67

def run
  until @links.empty?
    link = @links.shift
    run_link link
  end
end

#run_if(&block) ⇒ Object

If the test is true, run the link. Procedure takes the link as input.



35
36
37
# File 'lib/recluse/queue.rb', line 35

def run_if(&block)
  @run_if = block
end

Run a link



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/recluse/queue.rb', line 47

def run_link(link)
  response = Response.new
  return nil unless @run_if.call(link)
  begin
    response.page = @agent.get link.absolute
    response.code = response.page.code
    response.success = true
  rescue Mechanize::ResponseCodeError => code
    response.code = code.response_code
    response.success = false
  rescue => error
    response.errors = error
    response.success = false
  end
  @on_complete.call link, response
  response
end