Class: Klepto::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/klepto/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/klepto/config.rb', line 6

def initialize
  @headers = {}
  @abort_on_failure = true
  @sleep_time = 0.5
  @sleep_tries = 10
  @abort_on_redirect = false
  @after_handlers   = {
    :structure => [], #after each call to
    :get  => [], #after GET, before structure
    :abort=> []  #after abort
  }
  @before_handlers  = {:get => []}
  @status_handlers  = {}
  @handlers = {}
end

Instance Attribute Details

#after_handlersObject (readonly)

Returns the value of attribute after_handlers.



3
4
5
# File 'lib/klepto/config.rb', line 3

def after_handlers
  @after_handlers
end

#before_handlersObject (readonly)

Returns the value of attribute before_handlers.



4
5
6
# File 'lib/klepto/config.rb', line 4

def before_handlers
  @before_handlers
end

Instance Method Details

#abort_on_failure(aof) ⇒ Object

4xx, 5xx



62
63
64
# File 'lib/klepto/config.rb', line 62

def abort_on_failure(aof)
  @abort_on_failure = aof
end

#abort_on_failure?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/klepto/config.rb', line 53

def abort_on_failure?
  !!@abort_on_failure
end

#abort_on_redirect(aor) ⇒ Object



66
67
68
# File 'lib/klepto/config.rb', line 66

def abort_on_redirect(aor)
  @abort_on_redirect = aor
end

#abort_on_redirect?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/klepto/config.rb', line 57

def abort_on_redirect?
  !!@abort_on_redirect
end

#after(which = :structure, &block) ⇒ Object



86
87
88
89
# File 'lib/klepto/config.rb', line 86

def after(which = :structure, &block)
  @after_handlers[which] ||= []
  @after_handlers[which].push block
end

#before(which, &block) ⇒ Object



91
92
93
94
# File 'lib/klepto/config.rb', line 91

def before(which, &block)
  @before_handlers[which] ||= []
  @before_handlers[which].push block
end

#driver(_driver = nil) ⇒ Object



39
40
41
42
# File 'lib/klepto/config.rb', line 39

def driver(_driver=nil)
  @default_driver = _driver if _driver
  @default_driver
end

#has_timeout_handler?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/klepto/config.rb', line 49

def has_timeout_handler?
  @status_handlers[:timeout] && @status_handlers[:timeout].any?
end

#headers(_headers = nil) ⇒ Object



44
45
46
47
# File 'lib/klepto/config.rb', line 44

def headers(_headers=nil)
  @headers = _headers if _headers
  @headers
end

#on_http_status(*statuses, &block) ⇒ Object



79
80
81
82
83
84
# File 'lib/klepto/config.rb', line 79

def on_http_status(*statuses,&block)
  statuses.each do |status|
    @status_handlers[status] ||= []
    @status_handlers[status].push block
  end
end

#on_http_timeout(&block) ⇒ Object



74
75
76
77
# File 'lib/klepto/config.rb', line 74

def on_http_timeout(&block)
  @status_handlers[:timeout] ||= []
  @status_handlers[:timeout].push block
end

#sleep_time(_sleep_time = nil) ⇒ Object

TODO: DRY up handlers… def dispatch(group, handler, *args)

@handlers[group] ||= {}
@handlers[group][handler] ||= []
@handlers[group][handler].each{|handler| handler.call(*args)}

end



29
30
31
32
# File 'lib/klepto/config.rb', line 29

def sleep_time(_sleep_time=nil)
  @sleep_time = _sleep_time if _sleep_time
  @sleep_time
end

#sleep_tries(_sleep_tries = nil) ⇒ Object



34
35
36
37
# File 'lib/klepto/config.rb', line 34

def sleep_tries(_sleep_tries=nil)
  @sleep_tries = _sleep_tries if _sleep_tries
  @sleep_tries
end

#status_handler(status) ⇒ Object



70
71
72
# File 'lib/klepto/config.rb', line 70

def status_handler(status)
  @status_handlers[status] || []
end

#url(url = nil) ⇒ Object



96
97
98
99
# File 'lib/klepto/config.rb', line 96

def url(url=nil)
  @url = url if url
  @url
end