X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/d92fc6e526c90ffb2e2f4536f1e8f931bb450ee4..500c1bddf2640c0b59e8d761534f4bb99a83fbec:/app/controllers/concerns/query_methods.rb diff --git a/app/controllers/concerns/query_methods.rb b/app/controllers/concerns/query_methods.rb index 2d0bfee53..672a8c602 100644 --- a/app/controllers/concerns/query_methods.rb +++ b/app/controllers/concerns/query_methods.rb @@ -3,6 +3,43 @@ module QueryMethods private + ## + # Restrict the resulting items to those created during a particular time period + # Using 'to' requires specifying 'from' as well for historical reasons + def query_conditions_time(items, filter_property = :created_at) + interval = query_conditions_time_value + + if interval + items.where(filter_property => interval) + else + items + end + end + + ## + # Get query time interval from request parameters or nil + def query_conditions_time_value + if params[:from] + begin + from = Time.parse(params[:from]).utc + rescue ArgumentError + raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format" + end + + begin + to = if params[:to] + Time.parse(params[:to]).utc + else + Time.now.utc + end + rescue ArgumentError + raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format" + end + + from..to + end + end + ## # Limit the result according to request parameters and settings def query_limit(items) @@ -12,8 +49,9 @@ module QueryMethods ## # Get query limit value from request parameters and settings def query_limit_value - max_limit = Settings["max_#{controller_name.singularize}_query_limit"] - default_limit = Settings["default_#{controller_name.singularize}_query_limit"] + name = controller_path.sub(%r{^api/}, "").tr("/", "_").singularize + max_limit = Settings["max_#{name}_query_limit"] + default_limit = Settings["default_#{name}_query_limit"] if params[:limit] if params[:limit].to_i.positive? && params[:limit].to_i <= max_limit params[:limit].to_i