Class: Hwacha

Inherits:
Object
  • Object
show all
Defined in:
lib/hwacha.rb,
lib/hwacha/config.rb,
lib/hwacha/version.rb

Defined Under Namespace

Classes: Config

Constant Summary collapse

VERSION =
"1.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_concurrent_requests = 20) {|config| ... } ⇒ Hwacha

Returns a new instance of Hwacha.

Yields:



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/hwacha.rb', line 8

def initialize(max_concurrent_requests=20)
  config = Hwacha::Config.new

  # support the simple legacy API
  config.max_concurrent_requests = max_concurrent_requests

  # the nice configuration object API
  yield config if block_given?

  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/hwacha.rb', line 6

def config
  @config
end

Instance Method Details

#build_hydraObject



44
45
46
# File 'lib/hwacha.rb', line 44

def build_hydra
  Typhoeus::Hydra.new(config.hydra_options)
end

#check(urls) ⇒ Object Also known as: fire



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hwacha.rb', line 20

def check(urls)
  hydra = build_hydra

  Array(urls).each do |url|
    request = Typhoeus::Request.new(url, config.request_options)
    request.on_complete do |response|
      yield response.effective_url, response
    end
    hydra.queue request
  end

  hydra.run
end

#find_existing(urls) ⇒ Object Also known as: strike_true



34
35
36
37
38
# File 'lib/hwacha.rb', line 34

def find_existing(urls)
  check(urls) do |url, response|
    yield url if response.success?
  end
end