PageRenderTime 68ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/api/collections_controller.rb

https://bitbucket.org/acbarbosa/resource_map
Ruby | 121 lines | 95 code | 26 blank | 0 comment | 19 complexity | 34a881d1b83241295f8464a5e03c2a4a MD5 | raw file
  1. class Api::CollectionsController < ApplicationController
  2. include Api::JsonHelper
  3. include Api::GeoJsonHelper
  4. before_filter :authenticate_user!
  5. around_filter :rescue_with_check_api_docs
  6. def show
  7. options = [:sort]
  8. if params[:format] == 'csv' || params[:page] == 'all'
  9. options << :all
  10. params.delete(:page)
  11. else
  12. options << :page
  13. end
  14. @results = perform_search *options
  15. respond_to do |format|
  16. format.rss { render :show, layout: false }
  17. format.csv { collection_csv(collection, @results) }
  18. format.json { render json: collection_json(collection, @results) }
  19. end
  20. end
  21. def sample_csv
  22. respond_to do |format|
  23. format.csv { collection_sample_csv(collection) }
  24. end
  25. end
  26. def collection_sample_csv(collection)
  27. sample_csv = collection.sample_csv current_user
  28. send_data sample_csv, type: 'text/csv', filename: "#{collection.name}_sites.csv"
  29. end
  30. def count
  31. render json: perform_search(:count).total
  32. end
  33. def geo_json
  34. @results = perform_search :page, :sort, :require_location
  35. render json: collection_geo_json(collection, @results)
  36. end
  37. private
  38. def perform_search(*options)
  39. except_params = [:action, :controller, :format, :id, :updated_since, :search, :box, :lat, :lng, :radius]
  40. if current_snapshot
  41. search = collection.new_search snapshot_id: current_snapshot.id, current_user_id: current_user.id
  42. else
  43. search = collection.new_search current_user_id: current_user.id
  44. end
  45. search.use_codes_instead_of_es_codes
  46. if options.include? :page
  47. search.page params[:page].to_i if params[:page]
  48. except_params << :page
  49. elsif options.include? :count
  50. search.offset 0
  51. search.limit 0
  52. elsif options.include? :all
  53. search.unlimited
  54. end
  55. search.after params[:updated_since] if params[:updated_since]
  56. search.full_text_search params[:search] if params[:search]
  57. search.box *valid_box_coordinates if params[:box]
  58. if params[:lat] || params[:lng] || params[:radius]
  59. [:lat, :lng, :radius].each do |key|
  60. raise "Missing '#{key}' parameter" unless params[key]
  61. raise "Missing '#{key}' value" unless !params[key].blank?
  62. end
  63. search.radius params[:lat], params[:lng], params[:radius]
  64. end
  65. if options.include? :require_location
  66. search.require_location
  67. end
  68. if options.include? :sort
  69. search.sort params[:sort], params[:sort_direction] != 'desc' if params[:sort]
  70. except_params << :sort
  71. except_params << :sort_direction
  72. end
  73. search.where params.except(*except_params)
  74. search.api_results
  75. end
  76. def valid_box_coordinates
  77. coords = params[:box].split ','
  78. raise "Expected the 'box' parameter to be four comma-separated numbers" if coords.length != 4
  79. coords.each_with_index do |coord, i|
  80. Float(coord) rescue raise "Expected #{(i + 1).ordinalize} value of 'box' parameter to be a number, not '#{coord}'"
  81. end
  82. coords
  83. end
  84. def collection_csv(collection, results)
  85. sites_csv = collection.to_csv results
  86. send_data sites_csv, type: 'text/csv', filename: "#{collection.name}_sites.csv"
  87. end
  88. def rescue_with_check_api_docs
  89. yield
  90. rescue => ex
  91. Rails.logger.info ex.message
  92. Rails.logger.info ex.backtrace
  93. render text: "#{ex.message} - Check the API documentation: https://bitbucket.org/instedd/resource_map/wiki/API", status: 400
  94. end
  95. end