diff --git a/app/controllers/adopted_controller.rb b/app/controllers/adopted_controller.rb index 712a8a64..4fe1953c 100644 --- a/app/controllers/adopted_controller.rb +++ b/app/controllers/adopted_controller.rb @@ -3,7 +3,21 @@ class AdoptedController < ApplicationController def index @things = Thing.where.not(user_id: !nil) - render json: @things + respond_to do |format| + format.csv do + headers['Content-Type'] ||= 'text/csv' + headers['Content-Disposition'] = "attachment; filename=\"adopted_drains.csv\"" + end + format.xml do + render xml: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + end + format.json do + render json: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + end + format.all do + render json: @things.map { |thing| {latitude: thing.lat, longitude: thing.lng, city_id: 'N-' + thing.city_id.to_s} } + end + end end private diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c2c7448f..bb50b213 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +require 'csv' + class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. diff --git a/app/views/adopted/index.csv.erb b/app/views/adopted/index.csv.erb new file mode 100644 index 00000000..bab64908 --- /dev/null +++ b/app/views/adopted/index.csv.erb @@ -0,0 +1,5 @@ +<%- headers = ['Lat', 'Lng', 'City ID'] -%> +<%= CSV.generate_line headers %> +<%- @things.each do |thing| -%> +<%= CSV.generate_line([thing.lat, thing.lng, "N-" + thing.city_id.to_s]) -%> +<%- end -%>