Class: Webcache::Headers

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

Overview

nested class for convenience access to (meta) headers

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Headers

Returns a new instance of Headers.



29
30
31
# File 'lib/webget/webcache.rb', line 29

def initialize( data )
  @data = data
end

Class Method Details

.parse(txt) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/webget/webcache.rb', line 8

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
     ## always downcase keys for now
     ##  and strip value from leading and trailing spaces
     ##
     ##  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!!!
     data[ key.strip.downcase ] = value.strip
  end
  new( data )
end

Instance Method Details

#[](key) ⇒ Object



34
# File 'lib/webget/webcache.rb', line 34

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

#dateObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/webget/webcache.rb', line 43

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



36
37
38
39
40
# File 'lib/webget/webcache.rb', line 36

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 (60secs*60min*12h)

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/webget/webcache.rb', line 55

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)


61
# File 'lib/webget/webcache.rb', line 61

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

#expired_in_24h?Boolean Also known as: expired_in_1d?

Returns:

  • (Boolean)


62
# File 'lib/webget/webcache.rb', line 62

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

#to_hObject



33
# File 'lib/webget/webcache.rb', line 33

def to_h() @data; end