Giter Site home page Giter Site logo

perfectexamples / perfect-weather Goto Github PK

View Code? Open in Web Editor NEW
32.0 3.0 6.0 89 KB

Demonstrate using URL Routes & variables, Fetching of remote data from API's as JSON, reading and transforming to data more appropriately consumable by an API client.

Swift 100.00%
weather json server-side-swift swift perfect

perfect-weather's Introduction

Perfect Weather App Example简体中文

Get Involed with Perfect!

Star Perfect On Github Stack Overflow Follow Perfect on Twitter Join the Perfect Slack

Swift 3.0 Platforms OS X | Linux License Apache PerfectlySoft Twitter Slack Status

An Example Weather App Backend for Perfect

The purpose of this example is to demonstrate using URL Routes & variables, Fetching of remote data from API's as JSON, reading and transforming to data more appropriately consumable by an API client.

Packages demonstrated:

  • PerfectLib (JSON)
  • HTTPServer
  • CURL
  • JSONConfig (3rd party library for reading configurations)

This package builds with Swift Package Manager and is part of the Perfect project.

Ensure you have installed Xcode 8.0 or later.

Obtaining an API Key

This example project requires a valid API key from https://www.wunderground.com/weather/api/ to function. Use this link to sign up for a developer account, and obtain an API key.

This key will be added to the ApplicationConfiguration.json file as described below.

Setup - Xcode 8

  • Check out or download the project;
  • In terminal, navigate to the directory and execute
swift package generate-xcodeproj
  • Open Perfect-Weather.xcodeproj
  • Add to the "Library Search Paths" in "Project Settings" $(PROJECT_DIR), recursive. (This step will be unneeded in a future release of Xcode 8.)
  • Rename the ApplicationConfiguration.json.example file to ApplicationConfiguration.json.
  • Open this file and change the token value to the WeatherUnderground token obtained above.
  • Add the file to a new "Copy Files" build phase as shown:

https://github.com/PerfectExamples/Perfect-Weather/raw/master/Resource/CopyFilesPhase.png

  • Select the Executable build target from the build targets dropdown in Xcode
  • Run (cmd-R) to build & run in Xcode.

In Xcode's console output pane you will see:

[INFO] Starting HTTP server on 0.0.0.0:8181 with document root ./webroot

Setup - Terminal

  • Check out or download the project;
  • In terminal, navigate to the directory
    • Rename the ApplicationConfiguration.json.example file to ApplicationConfiguration.json.
  • Open this file and change the token value to the WeatherUnderground token obtained above.
  • Execute swift build
  • Once the project has compiled, execute ./.build/debug/Perfect-Weather

The output you will see:

[INFO] Starting HTTP server on 0.0.0.0:8181 with document root ./webroot

Included Routes

The following routes are included in this API for demonstration purposes:

Current conditions sample response:

{
  "observation_time": "Last Updated on September 27, 12:00 PM ADT",
  "weather": "Light Rain Showers",
  "temperature_string": "54 F (12 C)"
}

Forecast sample response:

[
	{
		"icon": "rain",
		"icon_url": "http://icons.wxug.com/i/c/k/rain.gif",
		"pop": "100",
		"title": "Tuesday",
		"period": 0,
		"fcttext": "Rain likely. High 54F. Winds SE at 5 to 10 mph. Chance of rain 100%. Rainfall near a half an inch.",
		"fcttext_metric": "Rain likely. High 12C. Winds SE at 10 to 15 km/h. Chance of rain 100%. Rainfall near 12mm."
	},
	{
		"icon": "nt_rain",
		"icon_url": "http://icons.wxug.com/i/c/k/nt_rain.gif",
		"pop": "100",
		"title": "Tuesday Night",
		"period": 1,
		"fcttext": "Rain early...then remaining cloudy with showers overnight. Low 48F. Winds NE at 10 to 15 mph. Chance of rain 100%. Rainfall near a quarter of an inch.",
		"fcttext_metric": "Rain early...then remaining cloudy with showers overnight. Low 9C. Winds NE at 15 to 25 km/h. Chance of rain 100%. Rainfall around 6mm."
	},
	{
		"icon": "cloudy",
		"icon_url": "http://icons.wxug.com/i/c/k/cloudy.gif",
		"pop": "20",
		"title": "Wednesday",
		"period": 2,
		"fcttext": "Overcast. High 59F. Winds NNE at 10 to 15 mph.",
		"fcttext_metric": "Cloudy. High near 15C. Winds NNE at 15 to 25 km/h."
	},
	{
		"icon": "nt_chancerain",
		"icon_url": "http://icons.wxug.com/i/c/k/nt_chancerain.gif",
		"pop": "40",
		"title": "Wednesday Night",
		"period": 3,
		"fcttext": "Considerable cloudiness. Occasional rain showers later at night. Low 52F. Winds NNE at 10 to 15 mph. Chance of rain 40%.",
		"fcttext_metric": "Cloudy with occasional showers late at night. Low 11C. Winds NNE at 10 to 15 km/h. Chance of rain 40%."
	},
	{
		"icon": "cloudy",
		"icon_url": "http://icons.wxug.com/i/c/k/cloudy.gif",
		"pop": "20",
		"title": "Thursday",
		"period": 4,
		"fcttext": "Cloudy. High around 60F. Winds N at 10 to 20 mph.",
		"fcttext_metric": "Cloudy. High 16C. Winds N at 15 to 30 km/h."
	},
	{
		"icon": "nt_mostlycloudy",
		"icon_url": "http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
		"pop": "10",
		"title": "Thursday Night",
		"period": 5,
		"fcttext": "Mostly cloudy skies. Low around 45F. Winds N at 10 to 20 mph.",
		"fcttext_metric": "Mostly cloudy. Low 7C. Winds N at 15 to 30 km/h."
	},
	{
		"icon": "partlycloudy",
		"icon_url": "http://icons.wxug.com/i/c/k/partlycloudy.gif",
		"pop": "20",
		"title": "Friday",
		"period": 6,
		"fcttext": "Intervals of clouds and sunshine. High 63F. Winds NNE at 10 to 15 mph.",
		"fcttext_metric": "Intervals of clouds and sunshine. High 17C. Winds NNE at 10 to 15 km/h."
	},
	{
		"icon": "nt_partlycloudy",
		"icon_url": "http://icons.wxug.com/i/c/k/nt_partlycloudy.gif",
		"pop": "10",
		"title": "Friday Night",
		"period": 7,
		"fcttext": "Partly cloudy skies. Low 47F. Winds NNE at 5 to 10 mph.",
		"fcttext_metric": "A few clouds. Low 8C. Winds NNE at 10 to 15 km/h."
	}
]

Postman Collection

The repo includes a file Example-WeatherAPI.postman_collection which is a Postman URL collection.

With Postman installed, import and use to easily query the routes.

Further Information

For more information on the Perfect project, please visit perfect.org.

perfect-weather's People

Contributors

iamjono avatar kjessup avatar moon1102 avatar rockfordwei avatar rubenroques avatar waltertyree avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.