Class: HttpMimic::CookieStore
- Inherits:
-
Object
- Object
- HttpMimic::CookieStore
- Defined in:
- lib/http_mimic/cookie_store.rb
Constant Summary collapse
- DEFAULT_TTL =
2 hours
7200
Class Method Summary collapse
-
.clear(host = nil) ⇒ Object
Clear stored cookies for a host or all hosts.
- .cookie_file_for(host) ⇒ Object
-
.extract_host(url_or_host) ⇒ Object
Extract host from URL or host string.
-
.load(host, max_age: DEFAULT_TTL) ⇒ Object
Load cookies for a given host Returns a Hash of cookies if valid and not expired, or empty Hash.
-
.save(host, cookies, ttl: DEFAULT_TTL) ⇒ Object
Save cookies for a given host with atomic locking.
- .store_dir ⇒ Object
Class Method Details
.clear(host = nil) ⇒ Object
Clear stored cookies for a host or all hosts
85 86 87 88 89 90 91 92 |
# File 'lib/http_mimic/cookie_store.rb', line 85 def clear(host = nil) if host file = (host) FileUtils.rm_f(file) else FileUtils.rm_rf(store_dir) end end |
.cookie_file_for(host) ⇒ Object
16 17 18 19 |
# File 'lib/http_mimic/cookie_store.rb', line 16 def (host) clean_host = sanitize_host(host) File.join(store_dir, "#{clean_host}.json") end |
.extract_host(url_or_host) ⇒ Object
Extract host from URL or host string
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/http_mimic/cookie_store.rb', line 95 def extract_host(url_or_host) str = url_or_host.to_s.strip if str =~ %r{^https?://}i URI.parse(str).host else str.split(':').first end rescue StandardError str end |
.load(host, max_age: DEFAULT_TTL) ⇒ Object
Load cookies for a given host Returns a Hash of cookies if valid and not expired, or empty Hash
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/http_mimic/cookie_store.rb', line 23 def load(host, max_age: DEFAULT_TTL) file = (host) return {} unless File.file?(file) data = nil File.open(file, File::RDONLY) do |f| f.flock(File::LOCK_SH) data = JSON.parse(f.read) rescue nil end return {} unless data.is_a?(Hash) updated_at = data['updated_at'].to_i if !max_age.nil? return {} if (Time.now.to_i - updated_at) > max_age end data['cookies'] || {} rescue StandardError => e if HttpMimic.configuration.debug puts "[HttpMimic::CookieStore] Error loading cookies for #{host}: #{e.}" end {} end |
.save(host, cookies, ttl: DEFAULT_TTL) ⇒ Object
Save cookies for a given host with atomic locking
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/http_mimic/cookie_store.rb', line 49 def save(host, , ttl: DEFAULT_TTL) return if .nil? || .empty? clean_host = sanitize_host(host) dir = store_dir FileUtils.mkdir_p(dir) file = (clean_host) = load(clean_host, max_age: ttl) || {} = .is_a?(Cookies) ? .to_h : .dup merged = .merge(stringify_keys()) payload = { 'host' => clean_host, 'updated_at' => Time.now.to_i, 'ttl' => ttl, 'cookies' => merged } # Thread & Process safe atomic write tmp_file = "#{file}.tmp.#{Process.pid}_#{Time.now.to_f}" File.open(tmp_file, File::RDWR | File::CREAT, 0644) do |f| f.flock(File::LOCK_EX) f.write(JSON.pretty_generate(payload)) f.flush end File.rename(tmp_file, file) rescue StandardError => e FileUtils.rm_f(tmp_file) if tmp_file if HttpMimic.configuration.debug puts "[HttpMimic::CookieStore] Error saving cookies for #{host}: #{e.}" end end |
.store_dir ⇒ Object
12 13 14 |
# File 'lib/http_mimic/cookie_store.rb', line 12 def store_dir File.(HttpMimic.configuration. || '~/.http_mimic/cookies') end |