Class: CartoCSSHelper::OverpassDownloader

Inherits:
Object
  • Object
show all
Defined in:
lib/cartocss_helper/overpass_downloader.rb

Defined Under Namespace

Classes: OverpassRefusedResponse

Class Method Summary collapse

Class Method Details

.cache_filename(query) ⇒ Object



12
13
14
15
16
# File 'lib/cartocss_helper/overpass_downloader.rb', line 12

def self.cache_filename(query)
  hash = Digest::SHA1.hexdigest query
  query_cache_filename = CartoCSSHelper::Configuration.get_path_to_folder_for_overpass_cache + hash + '_query.cache'
  return query_cache_filename
end

.cache_timestamp(query) ⇒ Object



18
19
20
21
# File 'lib/cartocss_helper/overpass_downloader.rb', line 18

def self.cache_timestamp(query)
  downloader = GenericCachedDownloader.new
  return downloader.get_cache_timestamp(cache_filename(query))
end

.format_query_into_url(query) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cartocss_helper/overpass_downloader.rb', line 56

def self.format_query_into_url(query)
  query = query.gsub(/\/\/.*\n/, '') # add proper parsing - it will mutilate // inside quotes etc
  query = query.gsub('\\', '\\\\')
  query = query.delete("\n")
  query = query.delete("\t")
  query = URI.escape(query)
  query = query.gsub("&", "%26")
  if query.length > 8174 #8175 is too much and allows crashes
    raise 'see https://github.com/matkoniecz/CartoCSSHelper/issues/35'
  end
  base_overpass_url = OverpassDownloader.get_overpass_instance_url
  return base_overpass_url + '/interpreter?data=' + query
end

.get_allowed_timeout_in_secondsObject



52
53
54
# File 'lib/cartocss_helper/overpass_downloader.rb', line 52

def self.get_allowed_timeout_in_seconds
  return 10 * 60
end

.get_overpass_instance_urlObject



70
71
72
# File 'lib/cartocss_helper/overpass_downloader.rb', line 70

def self.get_overpass_instance_url
  return CartoCSSHelper::Configuration.get_overpass_instance_url
end

.run_overpass_query(query, description, invalidate_cache: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cartocss_helper/overpass_downloader.rb', line 23

def self.run_overpass_query(query, description, invalidate_cache: false)
  url = OverpassDownloader.format_query_into_url(query)
  timeout = OverpassDownloader.get_allowed_timeout_in_seconds
  downloader = GenericCachedDownloader.new(timeout: timeout, stop_on_timeout: false)
  return downloader.get_specified_resource(url, cache_filename(query), description: description, invalidate_cache: invalidate_cache)
rescue RequestTimeout => e
  puts 'Overpass API refused to process this request. It will be not attempted again, most likely query is too complex. It is also possible that Overpass servers are unavailable'
  puts
  puts query
  puts
  puts url
  puts
  puts e
  raise OverpassRefusedResponse
rescue ExceptionWithResponse => e
  if e.http_code == 400
    puts "invalid query"
    puts
    puts query
    puts
    puts url
    puts
    puts e
  elsif e.http_code == 414
    puts 'see https://github.com/matkoniecz/CartoCSSHelper/issues/35'
  end
  raise e
end