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

.escape_query(query) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cartocss_helper/overpass_downloader.rb', line 59

def self.escape_query(query)
  # code causing bug - (// inside quotes, as predicted) - why it was even added?
  #query = query.gsub(/\/\/.*\n/, '') # add proper parsing - it will mutilate // inside quotes etc
  # TODO: replace complaint above by a test
  # maybe URI.escape(query, "/") is sufficient?

  # escape backslash - turns \ into \\
  query = query.gsub('\\', '\\\\')

  # newlines, tabs added in query for readability may be safely deleted
  #query = query.delete("\n") - can be escaped! Results in nicer Overpass failures
  query = query.delete("\t")

  #query = URI.escape(query) # no escaping for / [add require 'uri' to use it]
  #query = URI.escape(query, "/") # escapes only / [add require 'uri' to use it]
  #query = CGI.escape(query) # escapes spaces to + sign
  query = Addressable::URI.encode_component(query, Addressable::URI::CharacterClasses::QUERY)
  query = query.gsub("/", "%2F") # escape slashes manually

  # inside query also & and + must be escaped (entire query is an url parameter)
  query = query.gsub("&", "%26")
  query = query.gsub('+', '%2B')
  return query
end

.format_query_into_url(query) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/cartocss_helper/overpass_downloader.rb', line 84

def self.format_query_into_url(query)
  query = escape_query(query)
  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



55
56
57
# File 'lib/cartocss_helper/overpass_downloader.rb', line 55

def self.get_allowed_timeout_in_seconds
  return 10 * 60
end

.get_overpass_instance_urlObject



93
94
95
# File 'lib/cartocss_helper/overpass_downloader.rb', line 93

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
51
52
53
# 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 "url with %20 replaced back by spaces, %22 by \", newline by %0A"
    puts url.replace("%20", " ").replace("%22", '"').replace("\n", "%0A")
    puts
    puts e
  elsif e.http_code == 414
    puts 'see https://github.com/matkoniecz/CartoCSSHelper/issues/35'
  end
  raise e
end