Class: Webcache::Headers

Inherits:
Object
  • Object
show all
Defined in:
lib/webget/webcache_headers.rb

Overview

todo/check - rename to Meta or such - why? why not?

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Headers

Returns a new instance of Headers.



40
41
42
# File 'lib/webget/webcache_headers.rb', line 40

def initialize( data )
  @data = data
end

Class Method Details

.parse(txt) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/webget/webcache_headers.rb', line 15

def self.parse( txt )
  data = {}
  txt.each_line do |line|
     line = line.strip
     next  if line.empty? || line.start_with?( '#' )

     key, value = line.split( ':', 2 )  ## split on first colon

     ##  todo/fix: deal with possible duplicate header keys!!
     ##   if duplicate do NOT replease, add with leading ", " comma-separated!!!
     ##
     ##  check if multi-line headers are possible!!!
     ##   and than may turn value into array of values - why? why not?
     ##    or auto-add


     ## note - always downcase keys for now
     ##  and strip value from leading and trailing spaces
     data[ key.strip.downcase ] = value.strip
  end
  new( data )
end

Instance Method Details

#[](key) ⇒ Object



45
# File 'lib/webget/webcache_headers.rb', line 45

def [](key) @data[key]; end

#dateObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/webget/webcache_headers.rb', line 55

def date
   ## return date header
   ##  parses the time as RFC 1123 date of HTTP-date defined by RFC 2616:
   ##    day-of-week, DD month-name CCYY hh:mm:ss GMT
   ##   !!! Note that the result is always UTC (GMT). !!!
   ##   e.g. Sun, 19 May 2024 15:15:34 GMT
   ##        Mon, 10 Jun 2024 15:58:16 GMT
   @date ||= Time.httpdate( @data['date'] )
   @date
end

#each(&blk) ⇒ Object



47
48
49
50
51
# File 'lib/webget/webcache_headers.rb', line 47

def each( &blk )
  @data.each do |key, value|
    blk.call( key, value )
  end
end

#expired?(expires_in_date = Time.now.utc-60*60*12) ⇒ Boolean

default to 12h (60secs60min12h)

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/webget/webcache_headers.rb', line 67

def expired?( expires_in_date=Time.now.utc-60*60*12 )
  ## pp expires_in_date
  expires_in_date > date
end

#expired_in_12h?Boolean

add convenience helpers - why? why not?

Returns:

  • (Boolean)


73
# File 'lib/webget/webcache_headers.rb', line 73

def expired_in_12h?() expired?( Time.now.utc-60*60*12 ); end

#expired_in_24h?Boolean Also known as: expired_in_1d?

Returns:

  • (Boolean)


74
# File 'lib/webget/webcache_headers.rb', line 74

def expired_in_24h?() expired?( Time.now.utc-60*60*24 ); end

#to_hObject



44
# File 'lib/webget/webcache_headers.rb', line 44

def to_h() @data; end