Skip to content

Commit

Permalink
Added formatting for different content types, and edited fields to be…
Browse files Browse the repository at this point in the history
… output
  • Loading branch information
root authored and root committed May 2, 2017
1 parent 7371587 commit 15fae71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
16 changes: 15 additions & 1 deletion app/controllers/adopted_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions app/views/adopted/index.csv.erb
Original file line number Diff line number Diff line change
@@ -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 -%>

0 comments on commit 15fae71

Please sign in to comment.