Skip to content

cshea93/phumblr-api

Repository files navigation

Phumblr API

URls

https://cpstrillvt93.github.io/phumblr/

https://lit-beyond-80860.herokuapp.com/

https://github.com/cpstrillvt93/phumblr

https://github.com/cpstrillvt93/phumblr-api

ERDs, User Stories, and Wireframes

http://i.imgur.com/ecllvpO.jpg

http://i.imgur.com/nI2fd9v.jpg

http://i.imgur.com/zQT4gXY.jpg

Dependencies

Install with bundle install.

Until Rails 5 is released, this template should follow the most recent released version of Rails 4, as well as track master branches for rails-api and active_model_serializers.

Installation

  1. Download this template.
  2. Unzip and rename the template directory.
  3. Empty README.md and fill with your own content.
  4. Move into the new project and git init.
  5. Install dependencies with bundle install.
  6. Rename your app module in config/application.rb (change RailsApiTemplate).
  7. Rename your project database in config/database.yml (change 'rails-api-template').
  8. Create a .env for sensitive settings (touch .env).
  9. Generate new development and test secrets (bundle exec rake secret).
  10. Store them in .env with keys SECRET_KEY_BASE_<DEVELOPMENT|TEST> respectively.
  11. In order to make requests to your deployed API, you will need to set SECRET_KEY_BASE in the environment of the production API (using heroku config:set or the Heroku dashboard).
  12. In order to make requests from your deployed client application, you will need to set CLIENT_ORIGIN in the environment of the production API (e.g. heroku config:set CLIENT_ORIGIN https://<github-username>.github.io).
  13. Setup your database with bin/rake db:nuke_pave or bundle exec rake db:nuke_pave.
  14. Run the API server with bin/rails server or bundle exec rails server.

Structure

This template follows the standard project structure in Rails 4.

curl command scripts are stored in scripts with names that correspond to API actions.

User authentication is built-in.

Tasks

Developers should run these often!

  • bin/rake routes lists the endpoints available in your API.
  • bin/rake test runs automated tests.
  • bin/rails console opens a REPL that pre-loads the API.
  • bin/rails db opens your database client and loads the correct database.
  • bin/rails server starts the API.
  • scripts/*.sh run various curl commands to test the API. See below.

Approach

I started the project by focusing on my user storires, my ERD, and scoping my project down to something that I was confident I could accomplish in the alotted time. To start off on the right foot, I deployed both my heroku back-end and the ember auth template. With slightly more experience using Rails than ember, I decided it would be best to get my rails server up and running first. Though it took longer than the last time I used rails, I was able to get all my curl scripts running within a day and a half. I then moved on to my first ember experience.

Similar to how I approached using the browser-template, I first broke ember down into a diagram so that I could at least go into building my application with something to go off of. My first goal was to get phumblr crudding. Unlike using the browser template however, this was much easier with ember, as it seemed to seemelessly pull the data from my server. The 'magic' aspect of Ember certainly caused quite a few mishaps and frustrating bugs, however I am happy I decided to give it a try.

Major Hurdles/Unsolved Problems

Getting around the ember stuff that I did(and may still) not understand was probably made up the majority of the largest hurdles I made. When I had code that to me looked perfect, or at least should have worked, I spent hours combing through it, only to find that it was one parameter being required in my back-end, or a '/' precedeing my 'page-not-found' route. While I am proud of my ability to learn a front-end framework like Ember, I definitely felt a bit discouraged at certain times throughout the framework-learning proccess. I plan to continue working on this app after GA ends.

API

Use this as the basis for your own API documentation. Add a new third-level heading for your custom entities, and follow the pattern provided for the built-in user authentication documentation.

Scripts are included in scripts to test built-in actions. Add your own scripts to test your custom API. As an alternative, you can write automated tests in RSpec to test your API.

Authentication

Verb URI Pattern Controller#Action
POST /sign-up users#signup
POST /sign-in users#signin
PATCH /change-password/:id users#changepw
DELETE /sign-out/:id users#signout

POST /sign-up

Request:

curl http://localhost:4741/sign-up \
  --include \
  --request POST \
  --header "Content-Type: application/json" \
  --data '{
    "credentials": {
      "email": "'"${EMAIL}"'",
      "password": "'"${PASSWORD}"'",
      "password_confirmation": "'"${PASSWORD}"'"
    }
  }'
EMAIL=ava@bob.com PASSWORD=hannah scripts/sign-up.sh

Response:

HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8

{
  "user": {
    "id": 1,
    "email": "ava@bob.com"
  }
}

POST /sign-in

Request:

curl http://localhost:4741/sign-in \
  --include \
  --request POST \
  --header "Content-Type: application/json" \
  --data '{
    "credentials": {
      "email": "'"${EMAIL}"'",
      "password": "'"${PASSWORD}"'"
    }
  }'
EMAIL=ava@bob.com PASSWORD=hannah scripts/sign-in.sh

Response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "user": {
    "id": 1,
    "email": "ava@bob.com",
    "token": "BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f"
  }
}

PATCH /change-password/:id

Request:

curl --include --request PATCH "http://localhost:4741/change-password/$ID" \
  --header "Authorization: Token token=$TOKEN" \
  --header "Content-Type: application/json" \
  --data '{
    "passwords": {
      "old": "'"${OLDPW}"'",
      "new": "'"${NEWPW}"'"
    }
  }'
ID=1 OLDPW=hannah NEWPW=elle TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/change-password.sh

Response:

HTTP/1.1 204 No Content

DELETE /sign-out/:id

Request:

curl http://localhost:4741/sign-out/$ID \
  --include \
  --request DELETE \
  --header "Authorization: Token token=$TOKEN"
ID=1 TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/sign-out.sh

Response:

HTTP/1.1 204 No Content

Users

Verb URI Pattern Controller#Action
GET /users users#index
GET /users/1 users#show

GET /users

Request:

curl http://localhost:4741/users \
  --include \
  --request GET \
  --header "Authorization: Token token=$TOKEN"
TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/users.sh

Response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "users": [
    {
      "id": 2,
      "email": "bob@ava.com"
    },
    {
      "id": 1,
      "email": "ava@bob.com"
    }
  ]
}

GET /users/:id

Request:

curl --include --request GET http://localhost:4741/users/$ID \
  --header "Authorization: Token token=$TOKEN"
ID=2 TOKEN=BAhJIiVlZDIwZTMzMzQzODg5NTBmYjZlNjRlZDZlNzYxYzU2ZAY6BkVG--7e7f77f974edcf5e4887b56918f34cd9fe293b9f scripts/user.sh

Response:

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8

{
  "user": {
    "id": 2,
    "email": "bob@ava.com"
  }
}

Posts

Verb URI Pattern Controller#Action POST /posts posts#create GET /posts posts#index GET /posts/:id posts#show PATCH /posts/:id posts#update DELETE /posts/:id posts#destroy

Reset Database without dropping

This is not a task developers should run often, but it is sometimes necessary.

locally

bin/rake db:migrate VERSION=0
bin/rake db:migrate db:seed db:examples

heroku

heroku run rake db:migrate VERSION=0
heroku run rake db:migrate db:seed db:examples
  1. All content is licensed under a CC­BY­NC­SA 4.0 license.
  2. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact legal@ga.co.

About

Phumblr Rails Back-end

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published