Sweater Weather is a Turing Module 3 project that provides the developer with a series of wireframes and instructions to produce endpoints that the project front-end developers (ficticious) could use.
Using request such as GET /api/v1/forecast?location=denver,co
the app can
- retrieve the weather for a city,
- retrieve a background image for that city,
- register and user,
- log an existing user and
- plan a road trip (duration of the trip and weather at arrival)
The projects asks for T.D.D. (test driven development) to develop the project functionality. Gems such as simplecov, webmock, and vcr, among others help us drive this process.
The testing requirements expect 'sad path' path testing along with functionality. This means to test for when the user doesn't do what is expected. For example, entering the wrong password or omitting the confirmation password.
This project runs on Rails: 6.0.34
and Ruby: 2.5.3
To check your current versions, run:
$ ruby -v
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin19]
$ rails -v
Rails 6.0.3.4
This project uses the following additional gems:
You can find the most current version at Rubygems.org
- bcrypt: The bcrypt Ruby gem provides a simple wrapper for safely handling passwords.
- faraday: HTTP/REST API client library
- figaro: Simple, Heroku-friendly Rails app configuration using ENV and a single YAML file
- jsonapi-serializer: Fast, simple and easy to use JSON:API serialization library (also known as fast_jsonapi).
- pry: Pry is a runtime developer console and IRB alternative that attempts to bring REPL driven programming to the Ruby language
- rspec-rails: rspec-rails is a testing framework for Rails 5+
- rubocop: RuboCop is a Ruby code style checking and code formatting tool. It aims to enforce the community-driven Ruby Style Guide.
- simplecov: Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites
- shoulda-matchers: Shoulda Matchers provides RSpec compatible one-liners to test common Rails functionality
- webmock: WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.
- vcr: Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests
In this project we used the following APIs:
- OpenWeather: Weather information for any location on the globe. Some apis are paid. We used OneCall
- MapQuest Developer: Mapping, Geocoding, Directions, and Search provider. Free and paid APIs
- Pixabay Developer API: Access to over 2.1 million photos, illustrations, vector graphics, and videos.
Fork this Github repository: Sweater_weather
Clone your 'Sweater_weather' repository using SSH:
$ git clone [email protected]:<your-github-username>/Sweater_weather.git
In the command line run the following commands in order:
$ rails db:{create,migrate}
$ rails generate rspec:install
$ bundle exec figaro install
$ bundle install
Find the application.yml
file in the config folder and add your API keys
example:
MAP_API_KEY: <api key here without strings>
WEATHER_API_KEY: <api key here without strings>
IMAGE_API_KEY: <api key here without strings>
We can check the test on the spec folder by running rspec
In the command line run
$ bundle exec rspec
All tests should be passing.
If you get a nil
or a vcr
error,
you may need to delete vcr_cassettes
in the /spec/fixtures
folder.
After running the tests simplecov gathers the coverage and neatly reports them in an html page.
In the command line you should see something like this:
Coverage report generated for RSpec to /Users/nicomacbook/turing/3module/sweater_weather/coverage. 196 / 196 LOC (100.0%) covered.
sample rspec/simplecov terminal output
Rubocop helps us clean up the code.
In the command line run:
$ rubocop
Rubocop will highlight styling errors - according to rails standards - and make recommendations on how to improve the code.
The exposing these endpoints is the chief purpose of this application. To test this out your self in your computer follow the instructions below.
- Open the terminal in your computer and navigate to where
sweater_weather
is located andcd
into it. - Once in the directory run the following command in your terminal:
$ rails server
- Open Postman and type the following in the address bar:
http://localhost:3000/
- Add the requests below to call each endpoint.
example:
http://localhost:3000/api/v1/backgrounds?location=denver,co
- Change the verb to one corresponding to the call
for example:
Post
- Send your request by clicking
Send
Notice that some of the call require you add the paramteres to the body. In postman:
- Click on
body
- Select
raw
- Select
json
(by default show astext
)
Shows the current, hourly, and daily forecast for the location queried. It accepts city and state as parameters.
Request:
- Content-Type: application/json
- Accept: application/json
get api/v1/forecast?location=denver, co
Using Pixabay's API, retrieves an image that is related to the searched location and outputs a serialized json.
Request:
- Content-Type: application/json
- Accept: application/json
get api/v1/backgrounds?location=denver, co
Registers a new user and generates a unique API key for the user
Request:
- Content-Type: application/json
- Accept: application/json
The params are passed on in the body of the request not in the uri:
{
"email": "[email protected]",
"password": "password",
"password_confirmation": "password"
}
post /api/v1/users
This request authenticates a user and logs it into the application
Request:
- Content-Type: application/json
- Accept: application/json
{
"email": "[email protected]",
"password": "password"
}
post /api/v1/sessions
This request produces trip duration and destination weather forecast at eta.
Request:
- Content-Type: application/json
- Accept: application/json
{
"origin": "Denver,CO",
"destination": "Pueblo,CO",
"api_key": "jgn983hy48thw9begh98h4539h4"
}
post /api/v1/road_trip
This api-application has been deployed to Heroku. You can test the endpoints in postman by following the link below and running postman in your browser.
If you have postman installed locally you can run it in the app directly
https://sweater-weather-api-nico.herokuapp.com/api/v1/<type here the desired endpoint>