示例#1
0
// findAlertsByDate is an http.Handler that will expect a 'start' and 'end' date in the URL
// and will return a list of News Alerts that occured in that timeframe.
func (n NewshoundAPI) findAlertsByDate(w http.ResponseWriter, r *http.Request) {
	setCommonHeaders(w, r, "")
	vars := mux.Vars(r)
	startTime, endTime, err := web.ParseDateRangeFullDay(vars)
	if err != nil {
		web.ErrorResponse(w, err, http.StatusBadRequest)
		return
	}

	s, db := n.getDB()
	defer s.Close()

	alerts, err := FindAlertsByDate(db, startTime, endTime)
	if err != nil {
		log.Printf("Unable to access alerts by date! - %s", err.Error())
		web.ErrorResponse(w, ErrDB, http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, web.JsonResponseWrapper{Response: alerts})
}
示例#2
0
func (n NewshoundAPI) getEventsPerWeek(w http.ResponseWriter, r *http.Request) {
	setCommonHeaders(w, r, "")
	s, db := n.getDB()
	defer s.Close()

	sendersReport, err := GetEventsPerWeek(db)
	if err != nil {
		log.Printf("Unable to retrieve sender events per week! - %s", err.Error())
		web.ErrorResponse(w, ErrDB, http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, web.JsonResponseWrapper{Response: sendersReport})
}
示例#3
0
func (s *SubwayAPI) nextTrains(w http.ResponseWriter, r *http.Request) {
	setCommonHeaders(w, r)
	vars := mux.Vars(r)
	stop := vars["stopId"]
	feedType := vars["feed"]
	feed, err := gosubway.GetFeed(s.key, (feedType == ltrain))
	if err != nil {
		web.ErrorResponse(w, ErrFeed, http.StatusBadRequest)
		return
	}

	north, south := feed.NextTrainTimes(stop)
	resp := nextTrainResp{north, south}

	fmt.Fprint(w, web.JsonResponseWrapper{resp})
}
示例#4
0
// findOrderedAlerts is an http.Handler that will expect a comma delmited list of News Alert IDs
// in the URL and will return a chronologically ordered of those News Alerts' information.
func (n NewshoundAPI) findOrderedAlerts(w http.ResponseWriter, r *http.Request) {
	setCommonHeaders(w, r, "")
	vars := mux.Vars(r)
	alertIDs := strings.Split(vars["alert_ids"], ",")

	s, db := n.getDB()
	defer s.Close()

	alerts, err := FindOrderedAlerts(db, alertIDs)
	if err != nil {
		log.Printf("Unable to access alerts by multiple alert_id's! - %s", err.Error())
		web.ErrorResponse(w, ErrDB, http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, web.JsonResponseWrapper{Response: alerts})
}
示例#5
0
// findSenderInfo is an http.Handler that will expect a Sender name in the URL and if
// the sender exists, it will return the Sender Info report for the past 3 months.
func (n NewshoundAPI) findSenderInfo(w http.ResponseWriter, r *http.Request) {
	setCommonHeaders(w, r, "")
	vars := mux.Vars(r)
	sender := vars["sender"]

	s, db := n.getDB()
	defer s.Close()

	senderInfo, err := FindSenderInfo(db, sender)
	if err != nil {
		log.Printf("Unable to retrieve sender info report! - %s", err.Error())
		web.ErrorResponse(w, ErrDB, http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, web.JsonResponseWrapper{Response: senderInfo})
}
示例#6
0
// findEvent is an http.Handler that expects a News Event ID in the URL and if the
// event exists, it will return it's information.
func (n NewshoundAPI) findEvent(w http.ResponseWriter, r *http.Request) {
	setCommonHeaders(w, r, "")
	vars := mux.Vars(r)
	eventID := vars["event_id"]

	s, db := n.getDB()
	defer s.Close()

	event, err := FindEventByID(db, eventID)
	if err != nil {
		log.Printf("Unable to access event by event_id! - %s", err.Error())
		web.ErrorResponse(w, ErrDB, http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, web.JsonResponseWrapper{Response: event})
}
示例#7
0
// findAlertHtml is an http.Handler that expects a News Alert ID in the URL and if the
// alert exists, it will return it's HTML with a 'text/html' content-type.
func (n NewshoundAPI) findAlertHtml(w http.ResponseWriter, r *http.Request) {
	setCommonHeaders(w, r, "text/html; charset=UTF-8")
	vars := mux.Vars(r)
	alertID := vars["alert_id"]

	s, db := n.getDB()
	defer s.Close()

	alertHtml, err := FindAlertHtmlByID(db, alertID)
	if err != nil {
		log.Printf("Unable to access alerts by alert_id! - %s", err.Error())
		web.ErrorResponse(w, ErrDB, http.StatusBadRequest)
		return
	}

	fmt.Fprint(w, alertHtml)
}