diff options
author | Jochen Topf <jochen@topf.org> | 2014-05-18 08:22:43 +0200 |
---|---|---|
committer | Jochen Topf <jochen@topf.org> | 2014-05-18 08:22:43 +0200 |
commit | 1364cfa69ec4f865109ac1006b562b47f17f739f (patch) | |
tree | 24447c7d2a2589bdea8b78db091ed990ba064979 /web | |
parent | ce772fe43ba7c67703368ab120d7fe5b64481ee7 (diff) | |
download | taginfo-1364cfa69ec4f865109ac1006b562b47f17f739f.tar taginfo-1364cfa69ec4f865109ac1006b562b47f17f739f.tar.gz |
Add format=json_pretty to API calls.
Diffstat (limited to 'web')
-rw-r--r-- | web/lib/api/test/langtag.rb | 8 | ||||
-rw-r--r-- | web/lib/api/v4/key.rb | 12 | ||||
-rw-r--r-- | web/lib/api/v4/keys.rb | 12 | ||||
-rw-r--r-- | web/lib/api/v4/relation.rb | 12 | ||||
-rw-r--r-- | web/lib/api/v4/relations.rb | 4 | ||||
-rw-r--r-- | web/lib/api/v4/search.rb | 16 | ||||
-rw-r--r-- | web/lib/api/v4/site.rb | 6 | ||||
-rw-r--r-- | web/lib/api/v4/tag.rb | 8 | ||||
-rw-r--r-- | web/lib/api/v4/tags.rb | 8 | ||||
-rw-r--r-- | web/lib/api/v4/wiki.rb | 4 | ||||
-rw-r--r-- | web/lib/utils.rb | 16 |
11 files changed, 57 insertions, 49 deletions
diff --git a/web/lib/api/test/langtag.rb b/web/lib/api/test/langtag.rb index 6cc42e5..8fbc998 100644 --- a/web/lib/api/test/langtag.rb +++ b/web/lib/api/test/langtag.rb @@ -42,7 +42,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -67,7 +67,7 @@ class Taginfo < Sinatra::Base :region_note => nt.region_note, :notes => nt.notes }} - }.to_json + }, json_opts(params[:format])) end @@bcp47_filters = {}; @@ -109,7 +109,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -128,7 +128,7 @@ class Taginfo < Sinatra::Base :added => row['added'], :notes => notes }} - }.to_json + }, json_opts(params[:format])) end end diff --git a/web/lib/api/v4/key.rb b/web/lib/api/v4/key.rb index eb3cb84..57c6c6e 100644 --- a/web/lib/api/v4/key.rb +++ b/web/lib/api/v4/key.rb @@ -55,7 +55,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -66,7 +66,7 @@ class Taginfo < Sinatra::Base :to_fraction => (row['together_count'].to_f / has_this_key.to_f).round_to(4), :from_fraction => row['from_fraction'].to_f.round_to(4) } } - }.to_json + }, json_opts(params[:format])) end api(4, 'key/distribution/nodes', { @@ -183,11 +183,11 @@ class Taginfo < Sinatra::Base end end - return { + return JSON.generate({ :total => 4, :url => request.url, :data => out - }.to_json + }, json_opts(params[:format])) end api(4, 'key/values', { @@ -270,7 +270,7 @@ class Taginfo < Sinatra::Base end end - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total.to_i, @@ -282,7 +282,7 @@ class Taginfo < Sinatra::Base :in_wiki => row['in_wiki'] == 1, :description => wikidesc[row['value']] || '' } } - }.to_json + }, json_opts(params[:format])) end api(4, 'key/wiki_pages', { diff --git a/web/lib/api/v4/keys.rb b/web/lib/api/v4/keys.rb index 39131c6..b6189d5 100644 --- a/web/lib/api/v4/keys.rb +++ b/web/lib/api/v4/keys.rb @@ -117,7 +117,7 @@ class Taginfo < Sinatra::Base end end - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -140,7 +140,7 @@ class Taginfo < Sinatra::Base h[:wikipages] = row['wikipages'] if row['wikipages'] h[:prevalent_values] = row['prevalent_values'][0,10] if row['prevalent_values'] h } - }.to_json + }, json_opts(params[:format])) end api(4, 'keys/wiki_pages', { @@ -171,7 +171,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -184,7 +184,7 @@ class Taginfo < Sinatra::Base } { :key => row['key'], :lang => lang_hash } } - }.to_json + }, json_opts(params[:format])) end api(4, 'keys/without_wiki_page', { @@ -257,7 +257,7 @@ class Taginfo < Sinatra::Base reshash[key]['prevalent_values'] << pv end - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -270,7 +270,7 @@ class Taginfo < Sinatra::Base :users_all => row['users_all'].to_i, :prevalent_values => row['prevalent_values'] } } - }.to_json + }, json_opts(params[:format])) end end diff --git a/web/lib/api/v4/relation.rb b/web/lib/api/v4/relation.rb index 6388caf..32af96c 100644 --- a/web/lib/api/v4/relation.rb +++ b/web/lib/api/v4/relation.rb @@ -49,7 +49,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -66,7 +66,7 @@ class Taginfo < Sinatra::Base :count_relation_members => row['count_relations'].to_i, :count_relation_members_fraction => relation_type_info['members_relations'].to_i == 0 ? 0 : (row['count_relations'].to_f / relation_type_info['members_relations'].to_i).round_to(4), } } - }.to_json + }, json_opts(params[:format])) end api(4, 'relation/stats', { @@ -98,11 +98,11 @@ class Taginfo < Sinatra::Base end end - return { + return JSON.generate({ :total => 4, :url => request.url, :data => out - }.to_json + }, json_opts(params[:format])) end api(4, 'relation/wiki_pages', { @@ -133,7 +133,7 @@ class Taginfo < Sinatra::Base res = @db.execute('SELECT * FROM wiki.relation_pages LEFT OUTER JOIN wiki.wiki_images USING (image) WHERE rtype = ? ORDER BY lang', rtype) - return res.map{ |row| { + return JSON.generate(res.map{ |row| { :lang => row['lang'], :language => ::Language[row['lang']].native_name, :language_en => ::Language[row['lang']].english_name, @@ -149,7 +149,7 @@ class Taginfo < Sinatra::Base :thumb_url_suffix => row['thumb_url_suffix'] } } - }.to_json + }, json_opts(params[:format])) end end diff --git a/web/lib/api/v4/relations.rb b/web/lib/api/v4/relations.rb index 3296239..2d3935b 100644 --- a/web/lib/api/v4/relations.rb +++ b/web/lib/api/v4/relations.rb @@ -56,7 +56,7 @@ class Taginfo < Sinatra::Base pr[rtype] << pv end - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -67,7 +67,7 @@ class Taginfo < Sinatra::Base :count_fraction => row['count'].to_f / all_relations, :prevalent_roles => row['members_all'] ? pr[row['rtype']][0,10] : nil } } - }.to_json + }, json_opts(params[:format])) end end diff --git a/web/lib/api/v4/search.rb b/web/lib/api/v4/search.rb index 7cc6e66..e2015c5 100644 --- a/web/lib/api/v4/search.rb +++ b/web/lib/api/v4/search.rb @@ -37,7 +37,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -47,7 +47,7 @@ class Taginfo < Sinatra::Base :value => row['value'], :count_all => row['count_all'].to_i, }} - }.to_json + }, json_opts(params[:format])) end api(4, 'search/by_keyword', { @@ -74,7 +74,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -83,7 +83,7 @@ class Taginfo < Sinatra::Base :key => row['key'], :value => row['value'] }} - }.to_json + }, json_opts(params[:format])) end api(4, 'search/by_role', { @@ -115,7 +115,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -125,7 +125,7 @@ class Taginfo < Sinatra::Base :role => row['role'], :count_all => row['count_all'].to_i, }} - }.to_json + }, json_opts(params[:format])) end api(4, 'search/by_value', { @@ -157,7 +157,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -167,7 +167,7 @@ class Taginfo < Sinatra::Base :value => row['value'], :count_all => row['count_all'].to_i, }} - }.to_json + }, json_opts(params[:format])) end end diff --git a/web/lib/api/v4/site.rb b/web/lib/api/v4/site.rb index 7db0173..6c7a24e 100644 --- a/web/lib/api/v4/site.rb +++ b/web/lib/api/v4/site.rb @@ -17,7 +17,7 @@ class Taginfo < Sinatra::Base [:url, :name, :description, :icon, :contact, :area].each do |k| data[k] = TaginfoConfig.get("instance.#{k}") end - return data.to_json + return JSON.generate(data, json_opts(params[:format])) end api(4, 'site/sources', { @@ -31,12 +31,12 @@ class Taginfo < Sinatra::Base :example => { }, :ui => '/sources' }) do - return Source.visible.map{ |source| { + return JSON.generate(Source.visible.map{ |source| { :name => source.name, :data_until => source.data_until, :update_start => source.update_start, :update_end => source.update_end - }}.to_json + }}, json_opts(params[:format])) end end diff --git a/web/lib/api/v4/tag.rb b/web/lib/api/v4/tag.rb index 4554cb6..152569a 100644 --- a/web/lib/api/v4/tag.rb +++ b/web/lib/api/v4/tag.rb @@ -70,7 +70,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -82,7 +82,7 @@ class Taginfo < Sinatra::Base :to_fraction => (row['together_count'].to_f / has_this_key.to_f).round_to(4), :from_fraction => row['from_fraction'].to_f.round_to(4) } } - }.to_json + }, json_opts(params[:format])) end api(4, 'tag/distribution/nodes', { @@ -202,11 +202,11 @@ class Taginfo < Sinatra::Base end end - return { + return JSON.generate({ :total => 4, :url => request.url, :data => out - }.to_json + }, json_opts(params[:format])) end api(4, 'tag/wiki_pages', { diff --git a/web/lib/api/v4/tags.rb b/web/lib/api/v4/tags.rb index a5e2c0f..7fe5561 100644 --- a/web/lib/api/v4/tags.rb +++ b/web/lib/api/v4/tags.rb @@ -82,7 +82,7 @@ class Taginfo < Sinatra::Base end end - return { + return JSON.generate({ :total => res.size, :url => request.url, :data => res.map{ |row| { @@ -103,7 +103,7 @@ class Taginfo < Sinatra::Base :on_area => row['on_area'].to_i == 1, :on_relation => row['on_relation'].to_i == 1, } } - }.to_json + }, json_opts(params[:format])) end api(4, 'tags/popular', { @@ -146,7 +146,7 @@ class Taginfo < Sinatra::Base paging(@ap). execute() - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -164,7 +164,7 @@ class Taginfo < Sinatra::Base :count_relations => row['count_relations'].to_i, :count_relations_fraction => (row['count_relations'].to_f / @db.stats('relations')).round_to(4), } } - }.to_json + }, json_opts(params[:format])) end end diff --git a/web/lib/api/v4/wiki.rb b/web/lib/api/v4/wiki.rb index 9c9be4f..16e44bc 100644 --- a/web/lib/api/v4/wiki.rb +++ b/web/lib/api/v4/wiki.rb @@ -27,7 +27,7 @@ class Taginfo < Sinatra::Base }. execute() - return { + return JSON.generate({ :total => res.size, :url => request.url, :data => res.map{ |row| { @@ -39,7 +39,7 @@ class Taginfo < Sinatra::Base :wiki_tag_pages => row['wiki_tag_pages'].to_i, :wiki_tag_pages_fraction => row['wiki_tag_pages'].to_f / @db.stats('wiki_tags_described'), } } - }.to_json + }, json_opts(params[:format])) end end diff --git a/web/lib/utils.rb b/web/lib/utils.rb index 6d2d270..fc0d395 100644 --- a/web/lib/utils.rb +++ b/web/lib/utils.rb @@ -59,6 +59,14 @@ def section(id) @section_title = (@section =~ /^(keys|tags)$/) ? t.osm[@section] : t.taginfo[@section] end +def json_opts(format) + if format == 'json_pretty' + return { :indent => ' ', :space => ' ', :object_nl => "\n" } + else + return {} + end +end + # ------------------------------------------------------------------------------ # Escape tag key or value for XAPI according to @@ -196,7 +204,7 @@ end # Used in wiki api calls def get_wiki_result(res) - return res.map{ |row| { + return JSON.generate(res.map{ |row| { :lang => row['lang'], :language => ::Language[row['lang']].native_name, :language_en => ::Language[row['lang']].english_name, @@ -219,12 +227,12 @@ def get_wiki_result(res) :tags_combination => row['tags_combination'].split(','), :tags_linked => row['tags_linked' ].split(',') } - }.to_json + }, json_opts(params[:format])) end # Used in josm api calls def get_josm_style_rules_result(total, res) - return { + return JSON.generate({ :page => @ap.page, :rp => @ap.results_per_page, :total => total, @@ -239,7 +247,7 @@ def get_josm_style_rules_result(total, res) :line_width => row['line_width'] ? row['line_width'].to_i : 0, :icon => row['icon_source'] && row['icon_source'] != 'misc/deprecated.png' && row['icon_source'] != 'misc/no_icon.png' ? row['icon_source'] : '' } } - }.to_json + }, json_opts(params[:format])) end def paging_results(array) |