Class: Scruber::Helpers::FetcherAgentAdapters::AbstractAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb

Direct Known Subclasses

Memory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ AbstractAdapter

Returns a new instance of AbstractAdapter.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 8

def initialize(options={})
  @id = options.fetch(:id) { nil }
  @user_agent = options.fetch(:user_agent) { nil }
  @proxy_id = options.fetch(:proxy_id) { nil }
  @headers = options.fetch(:headers) { {} }
  @cookie_jar = options.fetch(:cookie_jar) { {} }
  @disable_proxy = options.fetch(:disable_proxy) { false }
  @updated_at = options.fetch(:updated_at) { Time.now }
  @created_at = options.fetch(:created_at) { Time.now }
  @jar = HTTP::CookieJar.new
  if @cookie_jar.is_a?(String)
    @jar.load(StringIO.new(@cookie_jar))
  end
  @_proxy = false
end

Instance Attribute Details

Returns the value of attribute cookie_jar.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def cookie_jar
  @cookie_jar
end

#created_atObject

Returns the value of attribute created_at.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def created_at
  @created_at
end

#disable_proxyObject

Returns the value of attribute disable_proxy.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def disable_proxy
  @disable_proxy
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def headers
  @headers
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def id
  @id
end

#jarObject (readonly)

Returns the value of attribute jar.



6
7
8
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 6

def jar
  @jar
end

#proxy_idObject

Returns the value of attribute proxy_id.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def proxy_id
  @proxy_id
end

#updated_atObject

Returns the value of attribute updated_at.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def updated_at
  @updated_at
end

#user_agentObject

Returns the value of attribute user_agent.



5
6
7
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 5

def user_agent
  @user_agent
end

Class Method Details

.find(id) ⇒ Object

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 61

def find(id)
  raise NotImplementedError
end

Instance Method Details



45
46
47
48
49
50
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 45

def cookie_for(uri_or_url)
  if uri_or_url.is_a?(String)
    uri_or_url = URI(uri_or_url)
  end
  HTTP::Cookie.cookie_value(@jar.cookies(uri_or_url))
end

#deleteObject

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 56

def delete
  raise NotImplementedError
end

#parse_cookies_from_page!(page) ⇒ Object



32
33
34
35
36
37
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 32

def parse_cookies_from_page!(page)
  cookies = page.response_cookies
  cookies.each do |cookie|
    @jar.parse(cookie, URI(page.url))
  end
end

#proxyObject



24
25
26
27
28
29
30
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 24

def proxy
  if @_proxy == false
    @_proxy = (@proxy_id ? Scruber::Helpers::ProxyRotator.find(@proxy_id) : nil)
  else
    @_proxy
  end
end

#saveObject

Raises:

  • (NotImplementedError)


52
53
54
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 52

def save
  raise NotImplementedError
end

#serialize_cookiesObject



39
40
41
42
43
# File 'lib/scruber/helpers/fetcher_agent_adapters/abstract_adapter.rb', line 39

def serialize_cookies
  io = StringIO.new
  @jar.save(io)
  @cookie_jar = io.string
end