diff options
author | Jochen Topf <jochen@topf.org> | 2011-03-05 12:40:43 +0100 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2011-03-05 12:40:43 +0100 |
commit | da9e383ad6ee910d762c4d78197abd350eec36c3 (patch) | |
tree | 472aa7d9b95a3e5bd725bc1fb90e3f508bae17a7 /examples | |
parent | b6dcb99e099a4a004dfa745d6f3b9f6e614e3895 (diff) | |
download | taginfo-da9e383ad6ee910d762c4d78197abd350eec36c3.tar taginfo-da9e383ad6ee910d762c4d78197abd350eec36c3.tar.gz |
added example tool to access Taginfo API
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/tapi | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/examples/tapi b/examples/tapi new file mode 100755 index 0000000..1ec6057 --- /dev/null +++ b/examples/tapi @@ -0,0 +1,64 @@ +#!/usr/bin/ruby +# +# tapi - Taginfo API +# +# Example utility for accessing the Taginfo API +# +# Usage: tapi PATH KEY=VAL KEY=VAL ... +# +# The tool will send the request to the server, get the JSON result and +# print it out in an easier to read form. +# +# You can set the following environment variables (default in brackets): +# TAGINFO_API_SERVER [taginfo.openstreetmap.de] +# TAGINFO_API_PORT [80] +# TAGINFO_API_VERSION [2] +# + +require 'rubygems' + +require 'net/http' +require 'uri' +require 'json' + +API_SERVER = ENV['TAGINFO_API_SERVER'] || 'taginfo.openstreetmap.de' +API_PORT = ENV['TAGINFO_API_PORT'] || 80 +API_VERSION = ENV['TAGINFO_API_VERSION'] || '2' + +def api_call(path, params) + query = [] + params.each_pair do |k,v| + query << k + '=' + v + end + uri = URI::HTTP.build({ + :host => API_SERVER, + :port => API_PORT, + :path => '/api/' + API_VERSION + '/' + path, + :query => query.join('&') + }) + STDERR.puts "URI=#{uri}" + + request = Net::HTTP::Get.new(uri.request_uri) + request['User-Agent'] = 'tapi/1.0 (Taginfo)' + result = Net::HTTP.start(uri.host, uri.port) do |http| + http.request(request) + end + if result.code != '200' + STDERR.puts "#{result.code} #{result.message}" + exit 1 + end + +# puts result.body + puts JSON.pretty_generate(JSON.parse(result.body)) +end + +path = ARGV.shift.sub(%r{^/}, '') + +options = {} +ARGV.each do |arg| + (k, v) = arg.split('=') + options[k] = v; +end + +api_call(path, options) + |