Skip to content

Commit

Permalink
create json output for alertings. Issue #14
Browse files Browse the repository at this point in the history
  • Loading branch information
shua123 committed Dec 16, 2016
1 parent 2a8f2a6 commit 3f53933
Showing 1 changed file with 59 additions and 3 deletions.
62 changes: 59 additions & 3 deletions app/controllers/alertings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
class AlertingsController < ApplicationController
before_action :set_alerting, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :authenticate_user!, except: [:index]

# GET /alertings
# GET /alertings.json
# def index
# authenticate_user!
# @alertings = Alerting.all
# end

def index
authenticate_user!
@alertings = Alerting.all
respond_to do |format|
format.html do
authenticate_user!
@alertings = Alerting.all
end
format.json do
@alertings = Alerting.current
entity_factory = ::RGeo::GeoJSON::EntityFactory.instance
# if (params[:loc])
# @alertings = sort_by_distance(@alertings)
# end
features = []
@alertings.each do |alerting|
json_attributes = create_json_attributes(alerting)
if alerting.geom.present?
alerting_geom = alerting.geom
else
alerting_geom = RGeo::Geographic.spherical_factory.point(0,0)
end
feature = entity_factory.feature(alerting_geom, alerting.id, json_attributes)
features.push(feature)
end
collection = entity_factory.feature_collection(features)
my_geojson = RGeo::GeoJSON::encode(collection)
render json: Oj.dump(my_geojson)
#cache_page(@response, "/alertings.json")
end
end
end

# GET /alertings/1
Expand Down Expand Up @@ -88,6 +119,31 @@ def destroy
end
end

def create_json_attributes(alerting)
json_attributes = alerting.attributes.except!("id", "alert_id", "alertable_id","alertable_type","starts_at", "ends_at", "created_by", "created_at", "updated_at", "alerting_id", "geom")

json_attributes["starts_at"] = alerting.starts_at.strftime('%m/%d/%Y')
if alerting.ends_at.present?
ends_at = alerting.ends_at.strftime('%m/%d/%Y')
else
ends_at = ""
end
json_attributes["ends_at"] = ends_at
if (alerting.alert)
json_attributes['description'] = alerting.alert.description
json_attributes['link'] = alerting.alert.link
json_attributes['alert_type'] = alerting.alert.alert_type
end
if (alerting.alertable)
json_attributes['feature_name'] = alerting.alertable.name
json_attributes['feature_id'] = alerting.alertable.id
json_attributes['feature_type'] = alerting.alertable_type
else
json_attributes['feature_type'] = "All"
end
json_attributes
end

private
# Use callbacks to share common setup or constraints between actions.
def set_alerting
Expand Down

0 comments on commit 3f53933

Please sign in to comment.