https://github.com/kfox/example-rails5-rest-api
An example RESTful API using Rails 5 API mode
https://github.com/kfox/example-rails5-rest-api
Last synced: about 1 year ago
JSON representation
An example RESTful API using Rails 5 API mode
- Host: GitHub
- URL: https://github.com/kfox/example-rails5-rest-api
- Owner: kfox
- License: mit
- Created: 2017-03-07T04:22:20.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-19T16:38:00.000Z (about 9 years ago)
- Last Synced: 2025-02-07T14:45:40.070Z (over 1 year ago)
- Language: Ruby
- Size: 37.1 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Example RESTful API using Rails 5 in API mode
## Notes
* Webhook calls aren't actually executed, but they will be logged
in the Rails server output.
* Do NOT use this code for production since the
application secrets are stored in the repository.
## Running the application using Docker and docker-compose
These instructions are for MacOS.
### Installing required packages using Homebrew
1. `brew cask install virtualbox`
2. `brew install docker docker-compose docker-machine`
### Building and running the application in the background
1. `docker-compose up -d`
2. `docker-compose run api rails db:setup db:migrate`
### Tailing the logs
`docker-compose logs -f -t`
### Determining the IP address of the API
`docker-machine ip default`
The API will be listening on port 80 inside the Docker container.
### Running the Rails console in the Docker environment
`docker-compose run api rails c`
### Stopping Docker and deleting old local images
`docker-compose down --rmi local`
## Local Development Setup
1. `brew install rbenv rbenv-binstubs ruby-build postgresql`
2. Initialize postgres per instructions.
3. `eval "$(rbenv init -)" && rbenv install 2.4.1 && rbenv local 2.4.1`
4. `gem install rails bundler`
5. `rails db:setup db:migrate`
6. `rails s -d`
### Inspect the available API routes
`rake routes`
## Curl command examples
*NOTE:* Replace `localhost:3000` in the examples below with `localhost`
if you are running the API in the Docker container.
For prettier (i.e., readable) output:
1. Install [jq](https://stedolan.github.io/jq/), e.g. `brew install jq`
2. Add the `-s` flag to the `curl` commands below
3. Pipe the output of each `curl` command to `jq .`
### Obtain a JSON Web Token (JWT)
`curl -XPOST -H "Content-type: application/json" -d '{"auth": {"email": "admin@example.com", "password": "password"}}' 'http://localhost:3000/user_token'`
Replace the string `YOUR_TOKEN_HERE` in the examples below with the
token received from the response.
### Get a list of brands
`curl -XGET -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/brands'`
### Get a specific brand
`curl -XGET -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/brands/SOME-BRAND-UUID'`
### Add a new brand
`curl -XPOST -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" -d '{"name": "Acme Towels"}' 'http://localhost:3000/brands'`
### Remove a brand
`curl -XDELETE -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/brands/SOME-BRAND-UUID'`
### Get a list of consumers
`curl -XGET -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/consumers'`
### Get a specific consumer
`curl -XGET -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/consumers/SOME-CONSUMER-UUID'`
### Get a list of brand affinities for a given consumer
`curl -XGET -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/consumers/SOME-CONSUMER-UUID/brand_affinities'`
### Add an affinity for one (or more) brands to a consumer
`curl -XPUT -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" -d '{"brand_affinity_ids": [ "SOME-BRAND-UUID", "ANOTHER-BRAND-UUID" ]}' 'http://localhost:3000/consumers/SOME-CONSUMER-UUID'`
### Remove a consumer
`curl -XDELETE -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/consumers/SOME-CONSUMER-UUID'`
### Change a brand's list of followers
`curl -XPUT -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" -d '{"follower_ids": [ "SOME-FOLLOWER-UUID", "ANOTHER-FOLLOWER-UUID" ]}' 'http://localhost:3000/brands/SOME-BRAND-UUID'`
### Get a list of webhooks
`curl -XGET -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/hooks'`
### Add a new webhook
`curl -XPOST -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" -d '{"threshold": "50", "callback": "https://foo.example.com/bar"}' 'http://localhost:3000/hooks'`
### Remove a webhook
`curl -XDELETE -H 'Authorization: Bearer YOUR_TOKEN_HERE' -H "Content-type: application/json" 'http://localhost:3000/hooks/SOME-WEBHOOK-UUID'`