class Apiphobic::Parameters::Filter

Constants

DATE_RANGE
FLOAT
INTEGER
ISO8601
NUMERICAL
NUMERICAL_RANGE

Attributes

raw_parameters[RW]

Public Class Methods

new(raw_parameters) click to toggle source
# File lib/apiphobic/parameters/filter.rb, line 16
def initialize(raw_parameters)
  self.raw_parameters = raw_parameters || {}
end

Public Instance Methods

each_with_object(memoized) { |[name, format_value(value)], memoized| ... } click to toggle source
# File lib/apiphobic/parameters/filter.rb, line 24
def each_with_object(memoized)
  compacted_parameters.each do |name, value|
    memoized = yield [name, format_value(value)], memoized
  end

  memoized
end
present?() click to toggle source
# File lib/apiphobic/parameters/filter.rb, line 20
def present?
  compacted_parameters.any?
end

Private Instance Methods

compacted_parameters() click to toggle source
# File lib/apiphobic/parameters/filter.rb, line 34
def compacted_parameters
  @compacted_parameters ||= raw_parameters.reject do |name, value|
    name == 'query' ||
    value == '' ||
    value.nil?
  end
end
format_value(value) click to toggle source

rubocop:disable Lint/AssignmentInCondition, Metrics/AbcSize, rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/apiphobic/parameters/filter.rb, line 44
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   = string_to_number(range_points[1])
    ending_point     = string_to_number(range_points[2])

    ::Range.new(starting_point, ending_point, exclusive)
  elsif value.match?(FLOAT)
    Float(value.delete('_'))
  elsif value.match?(INTEGER)
    Integer(value.delete('_'))
  elsif value.match?(ISO8601)
    ::Time.iso8601(value)
  else
    value
  end
end
string_to_number(other) click to toggle source

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity rubocop:enable Lint/AssignmentInCondition, Metrics/AbcSize,

# File lib/apiphobic/parameters/filter.rb, line 72
def string_to_number(other)
  other = other.delete('_')

  if other == 'Infinity'
    9_999_999
  elsif other.match?(FLOAT)
    other.to_f
  elsif other.match?(INTEGER)
    other.to_i
  end
end