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