Sha256: a0aa7b5d0f6ca96587c9583a401a9cad0741792d9fa63156fef03a4e9783fbb5

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

#
# Basically keeps track of cookies between the requests
#
class Session
  def initialize(default_options={})
    @options = default_options
    @options[:cookies] ||= {}
  end

  def cookies
    @options[:cookies]
  end

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

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

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

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

  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

protected

  def save_cookies(cookies)
    cookies.scan(/([a-z_]+?)=([^; ]+)/).each do |key, value|
      unless ['path', 'domain', 'expires'].include?(key)
        @options[:cookies][key] = value
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
under-os-http-1.4.0 lib/under_os/http/session.rb
under-os-1.3.0 lib/under_os/http/session.rb
under-os-1.2.1 lib/under_os/http/session.rb
under-os-1.2.0 lib/under_os/http/session.rb
under-os-1.1.0 lib/under_os/http/session.rb
under-os-1.0.0 lib/under_os/http/session.rb