Class: HttpMimic::Cookies
- Inherits:
-
Object
- Object
- HttpMimic::Cookies
- Includes:
- Enumerable
- Defined in:
- lib/http_mimic/cookies.rb
Defined Under Namespace
Classes: CookieItem
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Class Method Summary collapse
-
.format(cookie_input) ⇒ Object
Format Hash, Cookies, or String into a curl -b cookie string.
-
.parse_set_cookie(header_value_or_array) ⇒ Object
Parse a Cookies object from Set-Cookie headers.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #clear ⇒ Object
- #delete(name) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(cookie_hash = {}) ⇒ Cookies
constructor
A new instance of Cookies.
- #inspect ⇒ Object
- #item(name) ⇒ Object
- #key?(name) ⇒ Boolean (also: #has_key?, #include?)
- #keys ⇒ Object
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #size ⇒ Object (also: #length)
- #to_cookie_string ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
- #to_s ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(cookie_hash = {}) ⇒ Cookies
Returns a new instance of Cookies.
11 12 13 14 15 16 17 18 |
# File 'lib/http_mimic/cookies.rb', line 11 def initialize( = {}) @store = {} @cookie_items = {} .each do |k, v| self[k] = v end end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
9 10 11 |
# File 'lib/http_mimic/cookies.rb', line 9 def store @store end |
Class Method Details
.format(cookie_input) ⇒ Object
Format Hash, Cookies, or String into a curl -b cookie string
136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/http_mimic/cookies.rb', line 136 def self.format() case when Hash .map { |k, v| "#{k}=#{v}" }.join('; ') when Cookies . when String else .to_s end end |
.parse_set_cookie(header_value_or_array) ⇒ Object
Parse a Cookies object from Set-Cookie headers
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/http_mimic/cookies.rb', line 106 def self.(header_value_or_array) = new headers = Array(header_value_or_array).flatten.compact headers.each do |header_str| parts = header_str.split(';').map(&:strip) next if parts.empty? name_val = parts.first key, val = name_val.split('=', 2) next unless key key = key.strip val = val ? val.strip : '' attributes = {} parts[1..-1].each do |attr_str| attr_k, attr_v = attr_str.split('=', 2) attr_k = attr_k.strip.downcase attributes[attr_k] = attr_v ? attr_v.strip : true end [key] = val .instance_variable_get(:@cookie_items)[key] = CookieItem.new(key, val, attributes) end end |
Instance Method Details
#[](name) ⇒ Object
20 21 22 |
# File 'lib/http_mimic/cookies.rb', line 20 def [](name) @store[name.to_s] end |
#[]=(name, value) ⇒ Object
24 25 26 |
# File 'lib/http_mimic/cookies.rb', line 24 def []=(name, value) @store[name.to_s] = value.to_s end |
#clear ⇒ Object
64 65 66 67 |
# File 'lib/http_mimic/cookies.rb', line 64 def clear @cookie_items.clear @store.clear end |
#delete(name) ⇒ Object
59 60 61 62 |
# File 'lib/http_mimic/cookies.rb', line 59 def delete(name) @cookie_items.delete(name.to_s) @store.delete(name.to_s) end |
#each(&block) ⇒ Object
38 39 40 |
# File 'lib/http_mimic/cookies.rb', line 38 def each(&block) @store.each(&block) end |
#empty? ⇒ Boolean
50 51 52 |
# File 'lib/http_mimic/cookies.rb', line 50 def empty? @store.empty? end |
#inspect ⇒ Object
97 98 99 |
# File 'lib/http_mimic/cookies.rb', line 97 def inspect "#<#{self.class.name} #{@store.inspect}>" end |
#item(name) ⇒ Object
28 29 30 |
# File 'lib/http_mimic/cookies.rb', line 28 def item(name) @cookie_items[name.to_s] end |
#key?(name) ⇒ Boolean Also known as: has_key?, include?
32 33 34 |
# File 'lib/http_mimic/cookies.rb', line 32 def key?(name) @store.key?(name.to_s) end |
#keys ⇒ Object
42 43 44 |
# File 'lib/http_mimic/cookies.rb', line 42 def keys @store.keys end |
#merge(other) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/http_mimic/cookies.rb', line 69 def merge(other) = self.class.new(@store) if other.is_a?(Cookies) other.each { |k, v| [k] = v } elsif other.is_a?(Hash) other.each { |k, v| [k] = v } end end |
#merge!(other) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/http_mimic/cookies.rb', line 79 def merge!(other) if other.is_a?(Cookies) other.each { |k, v| self[k] = v } elsif other.is_a?(Hash) other.each { |k, v| self[k] = v } end self end |
#size ⇒ Object Also known as: length
54 55 56 |
# File 'lib/http_mimic/cookies.rb', line 54 def size @store.size end |
#to_cookie_string ⇒ Object
93 94 95 |
# File 'lib/http_mimic/cookies.rb', line 93 def @store.map { |k, v| "#{k}=#{v}" }.join('; ') end |
#to_h ⇒ Object Also known as: to_hash
88 89 90 |
# File 'lib/http_mimic/cookies.rb', line 88 def to_h @store.dup end |
#to_s ⇒ Object
101 102 103 |
# File 'lib/http_mimic/cookies.rb', line 101 def to_s end |
#values ⇒ Object
46 47 48 |
# File 'lib/http_mimic/cookies.rb', line 46 def values @store.values end |