2   class ChangesController < ApiController
 
   3     authorize_resource :class => false
 
   5     before_action :check_api_readable
 
   6     around_action :api_call_handle_error, :api_call_timeout
 
   8     # Get a list of the tiles that have changed within a specified time
 
  11       zoom = (params[:zoom] || "12").to_i
 
  13       if params.include?(:start) && params.include?(:end)
 
  14         starttime = Time.parse(params[:start])
 
  15         endtime = Time.parse(params[:end])
 
  17         hours = (params[:hours] || "1").to_i.hours
 
  18         endtime = Time.now.getutc
 
  19         starttime = endtime - hours
 
  22       if zoom >= 1 && zoom <= 16 &&
 
  23          endtime > starttime && endtime - starttime <= 24.hours
 
  24         mask = (1 << zoom) - 1
 
  26         tiles = Node.where(:timestamp => starttime..endtime).group("maptile_for_point(latitude, longitude, #{zoom})").count
 
  28         doc = OSM::API.new.get_xml_doc
 
  29         changes = XML::Node.new "changes"
 
  30         changes["starttime"] = starttime.xmlschema
 
  31         changes["endtime"] = endtime.xmlschema
 
  33         tiles.each do |tile, count|
 
  34           x = (tile.to_i >> zoom) & mask
 
  37           t = XML::Node.new "tile"
 
  41           t["changes"] = count.to_s
 
  48         render :xml => doc.to_s
 
  50         render :plain => "Requested zoom is invalid, or the supplied start is after the end time, or the start duration is more than 24 hours", :status => :bad_request