class Archivist::Client::Filters

Filters class manages the available filters that are configured to work with Archive.org The public API is very simple, and is used to create Archive.org query strings:

filters = Filters.new(options)
query = filters.to_query

Constants

DEFAULT_FILTERS
STANDARD_OPTIONS

Attributes

end_year[RW]
filters[RW]
language[RW]
options[RW]
start_year[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/archivist/client/filters.rb, line 28
def initialize(opts)
  @options = {}
  self.update(opts)
end

Public Instance Methods

to_query() click to toggle source
# File lib/archivist/client/filters.rb, line 44
def to_query
  self.finalize_filters
  @filters.join(' AND ')
end
update(opts) click to toggle source
# File lib/archivist/client/filters.rb, line 33
def update(opts)
  self.prune_options(opts)
  self.set_language
  self.set_years
  self.set_identifier
  self.set_filters
end

Protected Instance Methods

add_date_filter() click to toggle source
# File lib/archivist/client/filters.rb, line 99
def add_date_filter
  if @start_year or @end_year
    start_year = @start_year || "1400-01-01"
    end_year = @end_year || "2100-12-31"
    @filters << "date:[#{start_year} TO #{end_year}]"
  end
end
add_identifier() click to toggle source
# File lib/archivist/client/filters.rb, line 107
def add_identifier
  @filters << "identifier:#{@identifier}" if @identifier
end
add_language_filter() click to toggle source
# File lib/archivist/client/filters.rb, line 91
def add_language_filter
  @filters << if @language
                    "language:#{@language}"
                  else
                    '(language:eng OR language:English)'
                  end
end
finalize_filters() click to toggle source
# File lib/archivist/client/filters.rb, line 111
def finalize_filters
  add_language_filter
  add_date_filter
  add_identifier
  self
end
prune_options(opts) click to toggle source
# File lib/archivist/client/filters.rb, line 51
def prune_options(opts)
  # Save the options we need to build the filter
  @options.merge!(opts)
  # Remove the options used from the passed in options, so they don't get converted into parameters
  STANDARD_OPTIONS.each do |opt|
    opts.delete(opt)
  end
end
set_filters() click to toggle source
# File lib/archivist/client/filters.rb, line 86
def set_filters
  @filters = Array(@options[:filters]) # If opts[:filters] is nil => self.array is []
  @filters |= DEFAULT_FILTERS
end
set_identifier() click to toggle source
# File lib/archivist/client/filters.rb, line 73
def set_identifier
  @identifier = @options[:identifier] if @options.has_key?(:identifier)
end
set_language() click to toggle source

Only overwrite if update options has language key

# File lib/archivist/client/filters.rb, line 61
def set_language
  @language = @options[:language] if @options.has_key?(:language)
end
set_years() click to toggle source
# File lib/archivist/client/filters.rb, line 65
def set_years
  start = @options[:start_year]
  finish = @options[:end_year]
  @start_year = "#{start}-01-01" if start
  @end_year = "#{finish}-12-31" if finish
  self.validate_years
end
validate_years() click to toggle source

If one is provided, they must both be provided

# File lib/archivist/client/filters.rb, line 78
def validate_years
  unless @start_year && @end_year
    raise FilterException, ":start_year and :end_year must always be provided together, but only :start_year provided as #{@start_year}." if @start_year
    raise FilterException, ":start_year and :end_year must always be provided together, but only :end_year provided as #{@end_year}." if @end_year
  end
  return true
end