Class: Requests::Jar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/requests/cookies.rb

Defined Under Namespace

Classes: Ck

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJar

Returns a new instance of Jar.



11
12
13
# File 'lib/requests/cookies.rb', line 11

def initialize
  @s = {}
end

Class Method Details

.load(path) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/requests/cookies.rb', line 107

def self.load(path)
  j = new
  arr = JSON.parse(File.read(path))
  arr.each do |c|
    j.set(c['name'], c['value'], domain: c['domain'].to_s, path: c['path'] || '/',
          secure: !!c['secure'], http_only: !!c['http_only'],
          expires: (c['expires'] ? Time.parse(c['expires']) : nil))
  end
  j
end

Instance Method Details

#[](n) ⇒ Object



14
15
16
17
# File 'lib/requests/cookies.rb', line 14

def [](n)
  e = @s.values.reverse.find { |c| c.name == n }
  e && e.value
end

#[]=(n, v) ⇒ Object



18
19
20
# File 'lib/requests/cookies.rb', line 18

def []=(n, v)
  set(n, v)
end

#allObject



48
49
50
# File 'lib/requests/cookies.rb', line 48

def all
  @s.values
end

#clearObject



33
34
35
# File 'lib/requests/cookies.rb', line 33

def clear
  @s.clear
end

#delete(name) ⇒ Object



30
31
32
# File 'lib/requests/cookies.rb', line 30

def delete(name)
  @s.delete_if { |_, c| c.name == name }
end

#each(&b) ⇒ Object



44
45
46
47
# File 'lib/requests/cookies.rb', line 44

def each(&b)
  return enum_for(:each) unless block_given?
  @s.each_value { |c| yield c.name, c.value }
end

#empty?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/requests/cookies.rb', line 51

def empty?
  @s.empty?
end

#for_domain(host) ⇒ Object



67
68
69
# File 'lib/requests/cookies.rb', line 67

def for_domain(host)
  @s.values.select { |c| c.domain.to_s.empty? || host.to_s == c.domain || host.to_s.end_with?(".#{c.domain}") }
end

#for_url(url) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/requests/cookies.rb', line 54

def for_url(url)
  u = safe_uri(url)
  now = Time.now
  @s.values.select do |c|
    next false if c.expires && c.expires < now
    next false if c.secure && u.scheme != 'https'
    h = u.host.to_s
    dm = c.domain.to_s.empty? || h == c.domain || h.end_with?(".#{c.domain}")
    pp = u.path.to_s
    pm = pp.empty? ? c.path == '/' : pp.start_with?(c.path)
    dm && pm
  end
end

#get(name, default = nil) ⇒ Object



21
22
23
24
# File 'lib/requests/cookies.rb', line 21

def get(name, default = nil)
  v = self[name]
  v.nil? ? default : v
end

#save(path) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/requests/cookies.rb', line 99

def save(path)
  data = @s.values.map do |c|
    { name: c.name, value: c.value, domain: c.domain, path: c.path, secure: c.secure,
      http_only: c.http_only, expires: c.expires && c.expires.iso8601 }
  end
  File.write(path, JSON.generate(data))
  path
end

#set(name, value, domain: '', path: '/', secure: false, http_only: false, expires: nil) ⇒ Object Also known as: set_cookie



25
26
27
28
# File 'lib/requests/cookies.rb', line 25

def set(name, value, domain: '', path: '/', secure: false, http_only: false, expires: nil)
  @s[ck(domain, path, name)] = Ck.new(name, value, domain, path, secure, http_only, expires)
  self
end

#to_aObject



41
42
43
# File 'lib/requests/cookies.rb', line 41

def to_a
  to_h.to_a
end

#to_hObject



36
37
38
39
40
# File 'lib/requests/cookies.rb', line 36

def to_h
  h = {}
  @s.each_value { |c| h[c.name] = c.value }
  h
end

#to_header(url = nil) ⇒ Object



70
71
72
73
# File 'lib/requests/cookies.rb', line 70

def to_header(url = nil)
  list = url ? for_url(url) : @s.values.select { |c| !(c.expires && c.expires < Time.now) }
  list.map { |c| "#{c.name}=#{c.value}" }.join('; ')
end

#update(net_resp, url = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/requests/cookies.rb', line 74

def update(net_resp, url = nil)
  u = url ? safe_uri(url) : nil
  (net_resp.get_fields('set-cookie') || []).each do |line|
    parts = line.split(';').map(&:strip)
    kv = parts.shift
    next unless kv
    k, v = kv.split('=', 2)
    next unless k
    at = {}
    parts.each do |p|
      pk, pv = p.split('=', 2)
      at[pk.to_s.downcase] = pv
    end
    dom = at['domain'].to_s.sub(/\A\./, '')
    dom = u.host.to_s if dom.empty? && u
    exp = nil
    if at['max-age']
      exp = (Time.now + at['max-age'].to_i rescue nil)
    elsif at['expires']
      exp = (Time.parse(at['expires']) rescue nil)
    end
    set(k, v.to_s, domain: dom, path: at['path'] || '/', secure: at.key?('secure'),
        http_only: at.key?('httponly'), expires: exp)
  end
end