Sha256: bd891592b1945c6e8ad4369de3353b4a4ef0ecfc854dbaaff6908f158e91c201

Contents?: true

Size: 790 Bytes

Versions: 21

Compression:

Stored size: 790 Bytes

Contents

module Garage
  module Utils
    private

    # Private: extract date time range query from query parameters
    # Treat `from` and `to` as aliases for `gte` and `lte` respectively
    def extract_datetime_query(prefix)
      query = {}
      {:from => :gte, :to => :lte, :gt => nil, :lt => nil, :gte => nil, :lte => nil}.each do |key, as|
        k = "#{prefix}.#{key}"
        if params.has_key?(k)
          query[as || key] = fuzzy_parse(params[k]) or raise Garage::BadRequest, "Can't parse datetime #{params[k]}"
        end
      end
      query if query.size > 0
    end

    def fuzzy_parse(date)
      if date.is_a?(Numeric) || /^\d+$/ === date
        Time.zone.at(date.to_i)
      else
        Time.zone.parse(date)
      end
    rescue ArgumentError
      nil
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
the_garage-2.8.1 lib/garage/utils.rb
the_garage-2.8.0 lib/garage/utils.rb
the_garage-2.7.0 lib/garage/utils.rb
the_garage-2.6.1 lib/garage/utils.rb
the_garage-2.6.0 lib/garage/utils.rb
the_garage-2.5.0 lib/garage/utils.rb
the_garage-2.4.4 lib/garage/utils.rb
the_garage-2.4.3 lib/garage/utils.rb
the_garage-2.4.2 lib/garage/utils.rb
the_garage-2.4.1 lib/garage/utils.rb
the_garage-2.4.0 lib/garage/utils.rb
the_garage-2.3.3 lib/garage/utils.rb
the_garage-2.3.2 lib/garage/utils.rb
the_garage-2.3.1 lib/garage/utils.rb
the_garage-2.3.0 lib/garage/utils.rb
the_garage-2.2.0 lib/garage/utils.rb
the_garage-2.1.0 lib/garage/utils.rb
the_garage-2.0.3 lib/garage/utils.rb
the_garage-2.0.2 lib/garage/utils.rb
the_garage-2.0.1 lib/garage/utils.rb