Class: AgentX::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/agentx/session.rb

Constant Summary collapse

DEFAULT_PORT =
{ 'http' => 80, 'https' => 443 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url = nil, opts = {}) ⇒ Session

Returns a new instance of Session.



7
8
9
10
11
12
13
# File 'lib/agentx/session.rb', line 7

def initialize(base_url=nil, opts={})
  @history = History.new
  path = File.join(AgentX.root, opts[:cookie_store] || 'cookies.sqlite')
  @jar = HTTP::CookieJar.new(store: :mozilla, filename: path)
  @base_url = base_url
  @headers = {}
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.



5
6
7
# File 'lib/agentx/session.rb', line 5

def history
  @history
end

#jarObject (readonly)

Returns the value of attribute jar.



5
6
7
# File 'lib/agentx/session.rb', line 5

def jar
  @jar
end

Instance Method Details

#[](url, params = {}) ⇒ Object



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

def [](url, params={})
  Request.new(self, url, params)   
end

#base_urlObject



17
18
19
20
21
22
23
24
25
# File 'lib/agentx/session.rb', line 17

def base_url
  if url = URI(@base_url || (history.last && history.last.request.url))
    if url.port != DEFAULT_PORT[url.scheme]
      "#{url.scheme}://#{url.host}:#{url.port}"
    else
      "#{url.scheme}://#{url.host}"
    end
  end
end

#headers(headers = {}) ⇒ Object



39
40
41
42
# File 'lib/agentx/session.rb', line 39

def headers(headers={})
  headers.each { |k,v| set_header(k, v) }
  @headers
end

#inspectObject



48
49
50
# File 'lib/agentx/session.rb', line 48

def inspect
  "(Session)"
end

#relative_base_urlObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/agentx/session.rb', line 27

def relative_base_url
  if history.last
    uri = URI(history.last.request.full_url)
    path = uri.path.split('/')
    path.pop

    URI.join(uri, path.empty? ? '/' : path.join)
  else
    base_url
  end
end