Class: Session

Inherits:
Object show all
Defined in:
lib/under_os/http/session.rb

Overview

Basically keeps track of cookies between the requests

Instance Method Summary collapse

Constructor Details

#initialize(default_options = {}) ⇒ Session

Returns a new instance of Session.



5
6
7
8
# File 'lib/under_os/http/session.rb', line 5

def initialize(default_options={})
  @options = default_options
  @options[:cookies] ||= {}
end

Instance Method Details

#cookiesObject



10
11
12
# File 'lib/under_os/http/session.rb', line 10

def cookies
  @options[:cookies]
end

#delete(url, options = {}, &block) ⇒ Object



26
27
28
# File 'lib/under_os/http/session.rb', line 26

def delete(url, options={}, &block)
  request(url, {method: 'DELETE'}.merge(options), &block).send
end

#get(url, options = {}, &block) ⇒ Object



14
15
16
# File 'lib/under_os/http/session.rb', line 14

def get(url, options={}, &block)
  request(url, {method: 'GET'}.merge(options), &block).send
end

#post(url, options = {}, &block) ⇒ Object



18
19
20
# File 'lib/under_os/http/session.rb', line 18

def post(url, options={}, &block)
  request(url, {method: 'POST'}.merge(options), &block).send
end

#put(url, options = {}, &block) ⇒ Object



22
23
24
# File 'lib/under_os/http/session.rb', line 22

def put(url, options={}, &block)
  request(url, {method: 'PUT'}.merge(options), &block).send
end

#request(url, options = {}, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/under_os/http/session.rb', line 30

def request(url, options={}, &block)
  options = options.merge(@options){|k,a,b| a.merge(b) }

  UnderOs::HTTP::Request.new(url, options, &block).tap do |request|
    request.on :response do |response|
      save_cookies response.headers["Set-Cookie"] || ''
    end
  end
end