Sha256: 9b870ab55924e59038753d5fb226865d5c8baead12e777ccb97e40fe839c438b

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module  Drillbit
class   Parameters
class   Filter
  NUMERICAL       = /[\d_\.]+?/
  ISO8601         = /\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}(?:[+-]\d{2}\:\d{2}|Z)/
  NUMERICAL_RANGE = /\A(#{NUMERICAL})\.\.\.?(#{NUMERICAL}|Infinity)\z/
  DATE_RANGE      = /\A(#{ISO8601})\.\.\.?(#{ISO8601})\z/

  attr_accessor :raw_parameters

  def initialize(raw_parameters)
    self.raw_parameters = raw_parameters || {}
  end

  def present?
    compacted_parameters.any?
  end

  def each_with_object(memoized)
    compacted_parameters.each do |name, value|
      memoized = yield name, format_value(value), memoized
    end

    memoized
  end

  private

  def compacted_parameters
    @compacted_parameters ||= raw_parameters.reject do |name, value|
      name == 'query' ||
      value == '' ||
      value.nil?
    end
  end

  # rubocop:disable Lint/AssignmentInCondition
  def format_value(value)
    return value unless value.is_a?(String)

    if range_points  = value.match(DATE_RANGE)
      exclusive      = value.include? '...'
      starting_point = Time.iso8601(range_points[1])
      ending_point   = Time.iso8601(range_points[2])

      Range.new(starting_point, ending_point, exclusive)
    elsif range_points = value.match(NUMERICAL_RANGE)
      exclusive        = value.include? '...'
      starting_point   = range_points[1].to_f
      ending_point     = if range_points[2] == 'Infinity'
                           9_999_999
                         else
                           range_points[2].to_f
                         end

      Range.new(starting_point, ending_point, exclusive)
    else
      value
    end
  end
  # rubocop:enable Lint/AssignmentInCondition
end
end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
drillbit-3.0.3 lib/drillbit/parameters/filter.rb
drillbit-3.0.2 lib/drillbit/parameters/filter.rb
drillbit-3.0.1 lib/drillbit/parameters/filter.rb
drillbit-3.0.0 lib/drillbit/parameters/filter.rb