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
- #each(&block) ⇒ Object
-
#initialize(cookie_hash = {}) ⇒ Cookies
constructor
A new instance of Cookies.
- #inspect ⇒ Object
- #item(name) ⇒ Object
- #key?(name) ⇒ Boolean (also: #has_key?, #include?)
- #keys ⇒ Object
- #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
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/http_mimic/cookies.rb', line 98 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/http_mimic/cookies.rb', line 68 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 |
#each(&block) ⇒ Object
38 39 40 |
# File 'lib/http_mimic/cookies.rb', line 38 def each(&block) @store.each(&block) end |
#inspect ⇒ Object
59 60 61 |
# File 'lib/http_mimic/cookies.rb', line 59 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 |
#to_cookie_string ⇒ Object
55 56 57 |
# File 'lib/http_mimic/cookies.rb', line 55 def @store.map { |k, v| "#{k}=#{v}" }.join('; ') end |
#to_h ⇒ Object Also known as: to_hash
50 51 52 |
# File 'lib/http_mimic/cookies.rb', line 50 def to_h @store.dup end |
#to_s ⇒ Object
63 64 65 |
# File 'lib/http_mimic/cookies.rb', line 63 def to_s end |
#values ⇒ Object
46 47 48 |
# File 'lib/http_mimic/cookies.rb', line 46 def values @store.values end |