Sweater weather is an api application that exposes 5 endpoints for front end plug in. It has weather data by city, photo search by city and key word, user registration and login, and roadtrip creation.
There are 3 APIs consumed to build the engine endpoints. See details on the APIs below.
- Getting Started
- Prerequisites
- Installing
- Application Design
- Design drawing
- APIs consumed
- Database Schema
- Endpoints
- Application Features
- Testing
- How to Contribute
- Roadmap
- Contributors
- Acknowledgments
This project was tested with:
-
RSpec version 3.10
-
Ruby
- The project is built with rubyonrails using ruby version 2.5.3p105, you must install ruby on your local machine first. Please visit the ruby home page to get set up. Please ensure you install the version of ruby noted above.
-
Rails
gem install rails --version 5.2.5
-
Postgres database
- Visit the postgresapp homepage and follow their instructions to download the latest version of Postgres app.
- Clone the repo
$ git clone [email protected]:<YOUR_GITHUB_USER_NAME>/sweater_weather.git
- Bundle Install
$ bundle install
- Create, migrate and seed rails database
$ rails db:{create,migrate,seed}
- Set up Environment Variables
- run
bundle exec figaro install
- add the below variables to the
config/application.yml
- run
mapquest_api_key: '<YOU_KEY_HERE>' weather_api_key: '<YOU_KEY_HERE>' photo_api_key: '<YOU_KEY_HERE>'
Below is an example of how the API application is designed to consume multiple APIs so the front end can easily hook into the exposed endpoints (noted in black).
- For location coordinates and roadtrip data: MapQuest
- For location weather data: OpenWeather
- For location photo: Unsplash
View API endpoints consumed on Postman View API endpoints exposted on Postman
HTTP verbs | Paths | Used for | Output |
---|---|---|---|
GET | /api/v1/forecast?location=denver,co | Get the weather for a location | json |
GET | /api/v1/backgrounds?location=denver,co | Get a photo for a specific location | json |
POST | /api/v1/users | create a user account | json |
POST | /api/v1/sessions | create a user session (login a user) | json |
POST | /api/v1/road_trip | create a new road trip | json |
The application uses the Facade Design pattern. The controllers talk to the respective facades to get the necessary information packaged to the serializer.
#Controller
class Api::V1::SalariesController < ApplicationController
def city_info
if SalariesFacade.location_check?(params[:destination])
render json: SalariesSerializer.new(SalariesFacade.final_city_info(params[:destination]))
else
render json: NullSerializer.new, status: 204
end
end
end
#Facade
def self.location_check?(location)
if SalaryService.search_ua_id(location) == nil
false
else
true
end
end
def self.final_city_info(location)
city_info = {}
city_info[:destination] = location
city_info[:forecast] = city_forecast(location)
city_info[:salaries] = SalaryService.city_salary_info(location)
city_info
final_city_info = OpenStruct.new(city_info)
end
def self.city_forecast(location)
coords = MapQuestService.coordinates(SalaryService.full_name(location))
weather = WeatherService.get_city_info(coords)
forecast = {}
forecast[:summary] = weather[:current_weather][:conditions]
forecast[:temperature] = weather[:current_weather][:temperature]
forecast
end
end
#Service
def self.city_all_salary_info(location)
response = Faraday.get("https://api.teleport.org/api/urban_areas/teleport:#{search_ua_id(location)}/salaries/")
if response.body == "Sorry, but the page you were trying to view does not exist."
nil
else
raw_data = JSON.parse(response.body, symbolize_names: true)
salaries = []
raw_data[:salaries].map do |salary|
salaries << salary_info(salary)
end
salaries
end
end
...