Class: Anemone::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/anemone/http.rb

Constant Summary collapse

REDIRECT_LIMIT =

Maximum number of redirects to follow on each get_response

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ HTTP

Returns a new instance of HTTP.



13
14
15
16
17
# File 'lib/anemone/http.rb', line 13

def initialize(opts = {})
  @connections = {}
  @opts = opts
  @cookie_store = CookieStore.new(@opts[:cookies])
end

Instance Attribute Details

CookieStore for this HTTP client



11
12
13
# File 'lib/anemone/http.rb', line 11

def cookie_store
  @cookie_store
end

Instance Method Details

#accept_cookies?Boolean

Does this HTTP client accept cookies from the server?

Returns:

  • (Boolean)


73
74
75
# File 'lib/anemone/http.rb', line 73

def accept_cookies?
  @opts[:accept_cookies]
end

#fetch_page(url, referer = nil, depth = nil) ⇒ Object

Fetch a single Page from the response of an HTTP request to url. Just gets the final destination page.



23
24
25
# File 'lib/anemone/http.rb', line 23

def fetch_page(url, referer = nil, depth = nil)
  fetch_pages(url, referer, depth).last
end

#fetch_pages(url, referer = nil, depth = nil) ⇒ Object

Create new Pages from the response of an HTTP request to url, including redirects



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/anemone/http.rb', line 31

def fetch_pages(url, referer = nil, depth = nil)
  begin
    url = URI(url) unless url.is_a?(URI)
    pages = []
    get(url, referer) do |response, code, location, redirect_to, response_time|
      pages << Page.new(location, :body => response.body.dup,
                                  :code => code,
                                  :headers => response.to_hash,
                                  :referer => referer,
                                  :depth => depth,
                                  :redirect_to => redirect_to,
                                  :response_time => response_time)
    end

    return pages
  rescue => e
    if verbose?
      puts e.inspect
      puts e.backtrace
    end
    return [Page.new(url, :error => e)]
  end
end

#redirect_limitObject

The maximum number of redirects to follow



58
59
60
# File 'lib/anemone/http.rb', line 58

def redirect_limit
  @opts[:redirect_limit] || REDIRECT_LIMIT
end

#user_agentObject

The user-agent string which will be sent with each request, or nil if no such option is set



66
67
68
# File 'lib/anemone/http.rb', line 66

def user_agent
  @opts[:user_agent]
end