source: subversion/applications/utils/little-osm/tools.rb @ 34318

Last change on this file since 34318 was 1227, checked in by imi, 13 years ago

extracted little-osm's data access library to osm-data

File size: 1.1 KB
Line 
1module OSM
2
3  OK = "HTTP/1.1 200/OK\r\nServer: little-osm\r\nContent-type: text/plain\r\n\r\n"
4  HEADER = "<?xml version='1.0' encoding='UTF-8'?>\n<osm version='0.3' generator='little-osm'>\n"
5  FOOTER = '</osm>'
6
7  def bad_request reason = "Bad Request"
8    print "HTTP/1.1 400/#{reason}\r\n\r\n"
9    throw :little_osm_done
10  end
11
12  # shortcut to deliver the return of yield to the user. The return will be cached to send an error
13  # in case of failure.
14  def cached_deliver session
15    osm = yield
16    session << OK
17    session << HEADER
18    session << osm.to_xml
19    session << FOOTER
20  end
21
22  # Create and return an osm object out of the database query array
23  def make_osm q
24    tags = q[1] == "NULL" ? nil : Hash[*q[1].split("\n")]
25    case uid_to_class(q[0]).to_s
26    when "OSM::Node"
27      Node.new q[4], q[5], tags, uid_to_id(q[0]), q[2]
28    when "OSM::Segment"
29      from, to = q[3].split ','
30      Segment.new uid_to_id(from), uid_to_id(to), tags, uid_to_id(q[0]), q[2]
31    when "OSM::Way"
32      segs = q[3].split(',').collect do |x| uid_to_id(x) end
33      Way.new segs, tags, uid_to_id(q[0]), q[2]
34    end
35  end
36
37end
Note: See TracBrowser for help on using the repository browser.