Class: ActiveProxy::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/active_proxy/fetcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy_key, cache_store, options = {}) ⇒ Fetcher

Returns a new instance of Fetcher.



7
8
9
10
11
# File 'lib/active_proxy/fetcher.rb', line 7

def initialize(proxy_key, cache_store, options = {})
  @proxy_key = proxy_key
  @cache_store = cache_store
  @options = options
end

Instance Attribute Details

#cache_storeObject (readonly)

Returns the value of attribute cache_store.



5
6
7
# File 'lib/active_proxy/fetcher.rb', line 5

def cache_store
  @cache_store
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/active_proxy/fetcher.rb', line 5

def options
  @options
end

#proxy_keyObject (readonly)

Returns the value of attribute proxy_key.



5
6
7
# File 'lib/active_proxy/fetcher.rb', line 5

def proxy_key
  @proxy_key
end

Instance Method Details

#current_proxyObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/active_proxy/fetcher.rb', line 33

def current_proxy
  proxy = cache_store.fetch(cache_key("current")) do
    _proxy = proxy_list.sample
    log_message "Got new proxy #{_proxy}"
    _proxy
  end
  return proxy unless proxy.nil?
  cache_store.delete(cache_key("list"))
  current_proxy
end

#format_proxy_fullObject



18
19
20
21
22
# File 'lib/active_proxy/fetcher.rb', line 18

def format_proxy_full
  proxy = current_proxy

  "#{proxy[:type]}://#{proxy[:address]}:#{proxy[:port]}"
end

#format_proxy_httpObject



28
29
30
31
# File 'lib/active_proxy/fetcher.rb', line 28

def format_proxy_http
  proxy = current_proxy
  [proxy[:address], proxy[:port]]
end

#format_proxy_httpartyObject



13
14
15
16
# File 'lib/active_proxy/fetcher.rb', line 13

def format_proxy_httparty
  proxy = current_proxy
  { http_proxyaddr: proxy[:address], http_proxyport: proxy[:port], timeout: 4 }
end

#format_proxy_typhoeusObject



24
25
26
# File 'lib/active_proxy/fetcher.rb', line 24

def format_proxy_typhoeus
  { proxy: format_proxy_full }
end

#user_agentObject



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

def user_agent
  UserAgentRandomizer::UserAgent.fetch.string
end