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



10
11
12
13
14
# File 'lib/cartocss_helper/overpass_downloader.rb', line 10

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



16
17
18
19
# File 'lib/cartocss_helper/overpass_downloader.rb', line 16

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

.format_query_into_url(query) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/cartocss_helper/overpass_downloader.rb', line 52

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")
  base_overpass_url = OverpassDownloader.get_overpass_instance_url
  return base_overpass_url + '/interpreter?data=' + URI.escape(query)
end

.get_allowed_timeout_in_secondsObject



48
49
50
# File 'lib/cartocss_helper/overpass_downloader.rb', line 48

def self.get_allowed_timeout_in_seconds
  return 10 * 60
end

.get_overpass_instance_urlObject



61
62
63
# File 'lib/cartocss_helper/overpass_downloader.rb', line 61

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

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



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

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
  end
  raise e
end