diff options
-rwxr-xr-x | examples/tapi | 2 | ||||
-rw-r--r-- | web/lib/api.rb (renamed from web/lib/apidoc.rb) | 32 | ||||
-rw-r--r-- | web/lib/api/db.rb | 36 | ||||
-rw-r--r-- | web/lib/api/josm.rb | 10 | ||||
-rw-r--r-- | web/lib/api/langtag.rb | 15 | ||||
-rw-r--r-- | web/lib/api/reports.rb | 6 | ||||
-rw-r--r-- | web/lib/api/search.rb | 18 | ||||
-rw-r--r-- | web/lib/api/wiki.rb | 6 | ||||
-rw-r--r-- | web/lib/sql.rb | 12 | ||||
-rw-r--r-- | web/lib/utils.rb | 2 | ||||
-rwxr-xr-x | web/taginfo.rb | 7 | ||||
-rw-r--r-- | web/views/taginfo/apidoc.erb | 12 |
12 files changed, 93 insertions, 65 deletions
diff --git a/examples/tapi b/examples/tapi index 651b267..cb37c03 100755 --- a/examples/tapi +++ b/examples/tapi @@ -45,10 +45,10 @@ def api_call(path, params) end if result.code != '200' STDERR.puts "#{result.code} #{result.message}" + STDERR.puts result.body exit 1 end -# puts result.body puts JSON.pretty_generate(JSON.parse(result.body)) end diff --git a/web/lib/apidoc.rb b/web/lib/api.rb index 2486e5b..28fe786 100644 --- a/web/lib/apidoc.rb +++ b/web/lib/api.rb @@ -1,5 +1,5 @@ -class APIDoc +class API @@paths = {} @@ -79,3 +79,33 @@ class APIDoc end +class APIParameters + + attr_reader :page, :results_per_page + + def initialize(p) + if p[:rp].nil? || p[:rp] == '0' || p[:rp] == '' || p[:page].nil? || p[:page] == '0' || p[:page] == '' + @page = 0 + @results_per_page = 0 + else + if p[:rp] !~ /^[0-9]{1,3}$/ + raise ArgumentError, 'results per page must be integer between 0 and 999' + end + if p[:page] !~ /^[0-9]{1,4}$/ + raise ArgumentError, 'page must be integer between 0 and 9999' + end + @page = p[:page].to_i + @results_per_page = p[:rp].to_i + end + end + + def do_paging? + @results_per_page != 0 + end + + def first_result + @results_per_page * (@page - 1) + end + +end + diff --git a/web/lib/api/db.rb b/web/lib/api/db.rb index e9239f4..f4f14fb 100644 --- a/web/lib/api/db.rb +++ b/web/lib/api/db.rb @@ -67,7 +67,7 @@ class Taginfo < Sinatra::Base o.length 'length(key)' o.length :key }. - paging(params[:rp], params[:page]). + paging(@ap). execute() if include_data[:wikipages] @@ -112,8 +112,8 @@ class Taginfo < Sinatra::Base end return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| h = { :key => row['key'], @@ -172,12 +172,12 @@ class Taginfo < Sinatra::Base o.count_ways o.count_relations }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :key => row['skey'], @@ -378,7 +378,7 @@ class Taginfo < Sinatra::Base o.count_ways o.count_relations }. - paging(params[:rp], params[:page]). + paging(@ap). execute() # Read description for tag from wikipages, first in English then in the chosen @@ -396,8 +396,8 @@ class Taginfo < Sinatra::Base end return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total.to_i, :data => res.map{ |row| { :value => row['value'], @@ -459,12 +459,12 @@ class Taginfo < Sinatra::Base o.other_key o.from_fraction }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :other_key => row['other_key'], @@ -491,12 +491,12 @@ class Taginfo < Sinatra::Base o.scale1 o.scale2 }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :key => row['key'], @@ -654,12 +654,12 @@ class Taginfo < Sinatra::Base o.other_value o.from_fraction }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :other_key => row['other_key'], diff --git a/web/lib/api/josm.rb b/web/lib/api/josm.rb index 11a004a..06a3a6c 100644 --- a/web/lib/api/josm.rb +++ b/web/lib/api/josm.rb @@ -26,8 +26,8 @@ class Taginfo < Sinatra::Base def get_josm_result(total, res) return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :k => row['k'], @@ -58,7 +58,7 @@ class Taginfo < Sinatra::Base o.v :k o.b }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return get_josm_result(total, res); @@ -81,7 +81,7 @@ class Taginfo < Sinatra::Base o.v :b o.b }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return get_josm_result(total, res); @@ -100,7 +100,7 @@ class Taginfo < Sinatra::Base condition('k = ?', key). condition('v = ?', value). order_by([:k, :v]). - paging(params[:rp], params[:page]). + paging(@ap). execute() return get_josm_result(total, res); diff --git a/web/lib/api/langtag.rb b/web/lib/api/langtag.rb index d48425a..2adf326 100644 --- a/web/lib/api/langtag.rb +++ b/web/lib/api/langtag.rb @@ -26,12 +26,12 @@ class Taginfo < Sinatra::Base o.key o.count_all }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :key => row['key'], @@ -84,14 +84,13 @@ class Taginfo < Sinatra::Base end end - if params[:page] - start = (params[:page].to_i - 1) * params[:rp].to_i - entries = entries[start, params[:rp].to_i] + if @ap.do_paging? + entries = entries[@ap.first_result, @ap.results_per_page] end return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => entries.map{ |entry| h = { :type => entry.type.titlecase, diff --git a/web/lib/api/reports.rb b/web/lib/api/reports.rb index a0dc606..84e0306 100644 --- a/web/lib/api/reports.rb +++ b/web/lib/api/reports.rb @@ -50,7 +50,7 @@ class Taginfo < Sinatra::Base o.values_all o.users_all }. - paging(params[:rp], params[:page]). + paging(@ap). execute() reshash = Hash.new @@ -73,8 +73,8 @@ class Taginfo < Sinatra::Base end return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :key => row['key'], diff --git a/web/lib/api/search.rb b/web/lib/api/search.rb index eca1ed8..87d3433 100644 --- a/web/lib/api/search.rb +++ b/web/lib/api/search.rb @@ -27,12 +27,12 @@ class Taginfo < Sinatra::Base o.key o.value }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :key => row['key'], @@ -63,12 +63,12 @@ class Taginfo < Sinatra::Base o.key o.value }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :key => row['key'], @@ -89,12 +89,12 @@ class Taginfo < Sinatra::Base o.key o.value }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| { :key => row['key'], diff --git a/web/lib/api/wiki.rb b/web/lib/api/wiki.rb index 7525428..6e5d9de 100644 --- a/web/lib/api/wiki.rb +++ b/web/lib/api/wiki.rb @@ -38,12 +38,12 @@ class Taginfo < Sinatra::Base order_by(params[:sortname], params[:sortorder]){ |o| o.key }. - paging(params[:rp], params[:page]). + paging(@ap). execute() return { - :page => params[:page].to_i, - :rp => params[:rp].to_i, + :page => @ap.page, + :rp => @ap.results_per_page, :total => total, :data => res.map{ |row| lang_hash = Hash.new diff --git a/web/lib/sql.rb b/web/lib/sql.rb index 5f57fb3..55e4985 100644 --- a/web/lib/sql.rb +++ b/web/lib/sql.rb @@ -125,15 +125,9 @@ module SQL self end - def paging(results_per_page, page) - unless results_per_page.nil? || page.nil? - if results_per_page !~ /^[0-9]{1,3}$/ - raise ArgumentError, 'results per page must be integer between 0 and 999' - end - if page !~ /^[0-9]{1,4}$/ - raise ArgumentError, 'page must be integer between 0 and 9999' - end - limit(results_per_page.to_i, results_per_page.to_i * (page.to_i - 1)) + def paging(ap) + if ap.do_paging? + limit(ap.results_per_page, ap.first_result) end self end diff --git a/web/lib/utils.rb b/web/lib/utils.rb index 274b172..56a5934 100644 --- a/web/lib/utils.rb +++ b/web/lib/utils.rb @@ -172,7 +172,7 @@ end # Like the 'get' method but specific for API calls, includes documentation for API calls def api(version, path, doc=nil, &block) - APIDoc.new(version, path, doc) unless doc.nil? + API.new(version, path, doc) unless doc.nil? get("/api/#{version}/#{path}", &block) end diff --git a/web/taginfo.rb b/web/taginfo.rb index 1f74141..6beadb7 100755 --- a/web/taginfo.rb +++ b/web/taginfo.rb @@ -45,7 +45,7 @@ require 'lib/language.rb' require 'lib/sql.rb' require 'lib/sources.rb' require 'lib/reports.rb' -require 'lib/apidoc.rb' +require 'lib/api.rb' require 'lib/langtag/bcp47.rb' #------------------------------------------------------------------------------ @@ -139,6 +139,11 @@ class Taginfo < Sinatra::Base content_type :json expires next_update headers['Access-Control-Allow-Origin'] = '*' + begin + @ap = APIParameters.new(params) + rescue ArgumentError => ex + halt 412, { :error => ex.message }.to_json + end end #------------------------------------- diff --git a/web/views/taginfo/apidoc.erb b/web/views/taginfo/apidoc.erb index d3685b3..3dab6d2 100644 --- a/web/views/taginfo/apidoc.erb +++ b/web/views/taginfo/apidoc.erb @@ -7,17 +7,17 @@ <p style="color: red;">This list of API calls and their descriptions are not complete!</p> <ul> -<% APIDoc.paths.keys.sort.each do |version| - APIDoc.paths[version].keys.sort.each do |path| - doc = APIDoc.paths[version][path] %> +<% API.paths.keys.sort.each do |version| + API.paths[version].keys.sort.each do |path| + doc = API.paths[version][path] %> <li><a href="#<%= doc.complete_path.tr('/', '_')[1,1000] %>"><%= doc.complete_path %></li> <% end end %> </ul> -<% APIDoc.paths.keys.sort.each do |version| - APIDoc.paths[version].keys.sort.each do |path| - doc = APIDoc.paths[version][path] %> +<% API.paths.keys.sort.each do |version| + API.paths[version].keys.sort.each do |path| + doc = API.paths[version][path] %> <a name="<%= doc.complete_path.tr('/', '_')[1,1000] %>"></a> <h2><%= doc.complete_path %></h2> <table class="desc"> |