Beispiel #1
0
// PublicDataMap calls PublicData and returns the result after type asserting to objects.Map
func PublicDataMap(object interface{}, options map[string]interface{}) (objects.Map, error) {

	data, err := publicData(object, 0, options)

	if err != nil {
		return nil, err
	}

	if data == nil {
		return nil, nil
	}

	switch data.(type) {
	case map[string]interface{}:
		return objects.Map(data.(map[string]interface{})), nil
	case objects.Map:
		return data.(objects.Map), nil
	default:
		if dataMap, ok := data.(objects.Map); ok {
			return dataMap, nil
		} else {
			panic(fmt.Sprintf("codecs: PublicDataMap must refer to a map[string]interface{} or objects.Map, not %s.  Did you mean to implement the Facade interface?", reflect.TypeOf(data)))
		}
	}

	// assume we have an objects.Map
	mapData := data.(objects.Map)

	return mapData, nil
}
Beispiel #2
0
func Router(r *gin.Engine) {

	r.Use(ValidCityMiddleware())

	r.GET("/temperature/:city", func(c *gin.Context) {
		city := c.Param("city")

		url := fmt.Sprintf("%s/weather?q=%s&units=imperial&appid=%s",
			apiBaseUrl, city, appId)

		GetOpenWeatherMapData(url, func(data gin.H, err error) {
			if err != nil {
				c.JSON(404, gin.H{
					"message": "Data Not Available",
				})
			} else {
				dataMap := objects.Map(data)
				data = gin.H{
					"temperature": gin.H{
						"units":   "Fahrenheit",
						"low":     dataMap.Get("main.temp_min"),
						"high":    dataMap.Get("main.temp_max"),
						"current": dataMap.Get("main.temp"),
					},
				}
				c.JSON(200, data)
			}
		})
	})

	r.GET("/wind/:city", func(c *gin.Context) {
		city := c.Param("city")

		url := fmt.Sprintf("%s/weather?q=%s&units=imperial&appid=%s",
			apiBaseUrl, city, appId)

		GetOpenWeatherMapData(url, func(data gin.H, err error) {
			if err != nil {
				c.JSON(404, gin.H{
					"message": "Data Not Available",
				})
			} else {
				dataMap := objects.Map(data)
				data = gin.H{
					"wind": gin.H{
						"speed": gin.H{
							"value": dataMap.Get("wind.speed"),
							"units": "mph",
						},
						"direction": gin.H{
							"value": dataMap.Get("wind.deg"),
							"units": "degrees",
						},
					},
				}
				c.JSON(200, data)
			}
		})
	})

}