Class: Archaeo::CdxFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/archaeo/cdx_filter.rb

Overview

Builds and validates CDX Server API filter expressions.

CDX filter format: [!]field:regex The optional ! prefix inverts the match. The field must be a recognized CDX field name. The regex is a Java-compatible regex pattern matched against the field value.

Constant Summary collapse

VALID_FIELDS =
%w[
  urlkey timestamp original mimetype statuscode
  digest length
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(expression) ⇒ CdxFilter

Returns a new instance of CdxFilter.



16
17
18
19
# File 'lib/archaeo/cdx_filter.rb', line 16

def initialize(expression)
  @expression = expression.to_s
  validate!
end

Class Method Details

.by_digest(digest) ⇒ Object



50
51
52
# File 'lib/archaeo/cdx_filter.rb', line 50

def self.by_digest(digest)
  new("digest:#{digest}")
end

.by_mimetype(type) ⇒ Object



42
43
44
# File 'lib/archaeo/cdx_filter.rb', line 42

def self.by_mimetype(type)
  new("mimetype:#{type}")
end

.by_status(code) ⇒ Object



34
35
36
# File 'lib/archaeo/cdx_filter.rb', line 34

def self.by_status(code)
  new("statuscode:#{code}")
end

.by_url(pattern) ⇒ Object



54
55
56
# File 'lib/archaeo/cdx_filter.rb', line 54

def self.by_url(pattern)
  new("original:#{pattern}")
end

.by_urlkey(pattern) ⇒ Object



58
59
60
# File 'lib/archaeo/cdx_filter.rb', line 58

def self.by_urlkey(pattern)
  new("urlkey:#{pattern}")
end

.combine(*filters) ⇒ Object



66
67
68
# File 'lib/archaeo/cdx_filter.rb', line 66

def self.combine(*filters)
  filters.flatten
end

.excluding_errorsObject



74
75
76
77
# File 'lib/archaeo/cdx_filter.rb', line 74

def self.excluding_errors
  [excluding_status(404), excluding_status(500),
   excluding_status(502), excluding_status(503)]
end

.excluding_mimetype(type) ⇒ Object



46
47
48
# File 'lib/archaeo/cdx_filter.rb', line 46

def self.excluding_mimetype(type)
  new("!mimetype:#{type}")
end

.excluding_status(code) ⇒ Object



38
39
40
# File 'lib/archaeo/cdx_filter.rb', line 38

def self.excluding_status(code)
  new("!statuscode:#{code}")
end

.only_successfulObject



70
71
72
# File 'lib/archaeo/cdx_filter.rb', line 70

def self.only_successful
  [by_status(200)]
end

Instance Method Details

#and(other) ⇒ Object



62
63
64
# File 'lib/archaeo/cdx_filter.rb', line 62

def and(other)
  [self, other]
end

#fieldObject



29
30
31
32
# File 'lib/archaeo/cdx_filter.rb', line 29

def field
  stripped = @expression.delete_prefix("!")
  stripped.split(":", 2).first.to_s
end

#negated?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/archaeo/cdx_filter.rb', line 25

def negated?
  @expression.start_with?("!")
end

#to_sObject



21
22
23
# File 'lib/archaeo/cdx_filter.rb', line 21

def to_s
  @expression
end