mfuery / ruby

backend rails api for sendchinatownlove.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SendChinatownLoveAPI

Setup 🔧

For ubuntu try using ... https://gorails.com/setup/ubuntu/19.10

  1. Download rails, gem install rails — brew install gem
  2. brew install postgresql
  3. Install heroku cli
  4. bundle install --path vendor/bundle
  5. rails db:create
  6. rails db:migrate
  7. heroku local web -> localhost:5000
  8. test: heroku local:run bundle exec rspec

NOTE: Please clone and create your branches in the main repo, PR's from forked repos will have tests fail due to missing CI environmental variables

on machine

Installation

  • Download Ruby 2.6.6
  • rvm install ruby-X.X.X If you don't have rvm, gem install rvm: \curl -sSL https://get.rvm.io | bash -s stable --ruby
  • Download postgres brew install postgresql
  • cd to ruby/ and run bundle install. You might have to gem install bundler
  • Create a .env file (the secret sauce) by running this command cp .env.example .env
  • Follow the following guide to fill the .env file in https://docs.google.com/document/d/1UPNCwjWS_T7XT5AXsewphu6NvNdV7TQLSJub-RBRAG0/edit?ts=5ec88e82

Create and migrate database

  • Run the server (see below) and create and migrate your DB: rails db:create && rails db:migrate && rails db:seed If you see an error like Couldn't create 'myapp_development' database. Please check your configuration. rails aborted! PG::ConnectionBad: could not connect to server: No such file or directory, try running brew services restart postgresql source

If you get a migration error like PG::UndefinedTable: ERROR: or PG::NotNullViolation, try running rails db:environment:set RAILS_ENV=development --> rails db:reset --> rails db:migrate

Useful commands

  • Run the server: heroku local web If you see an error that looks like No such file or directory @ rb_sysopen - tmp/pids/puma.pid, run:

    mkdir tmp

    mkdir tmp/pids

    source

  • Run the server on port 5000: heroku local web -p 5000

  • Run all tests: heroku local:run bundle exec rspec

  • Run specific tests: heroku local:run bundle exec "rspec -e '<insert string from test description>'"

  • Run console: heroku local:run rails console

  • Run local migrations in prod heroku run rake db:migrate (do this after submitting a change with a local migration)

  • Install annotate gem install annotate

  • Annotate models annotate --models

  • Annotate everything annotate

Development

  • rails generate model <ModelName>
  • rails generate controller <ControllerName>
  • rails generate migration <MigrationName>

Enabling Webhooks locally

For you to work with webhooks, you need a domain for square to send events to. To do this without a hosted service, you can forward your localhost port using something like ngrok or localhost.run. This also enables you to share your local environment in pull requests for additional validations.

  1. Setup localhost.run or ngrok or any derivatives to forward your localhost port to the web.
  2. Make the port forwarder point to your local port. localhost.run cmd: ssh -R 80:localhost:5000 ssh.localhost.run
  3. Make sure to append /webhooks to the forwarding URL so you're hitting the correct route
  4. Go to: https://developer.squareup.com/apps
  5. Go to the webhooks pane and create a sandbox api to point to domain created above.
  6. Enable the following events:
    • refund.created
    • refund.updated
    • payment.updated
  7. Edit .env file.
    1. Edit SQUARE_WEBHOOK_SIGNATURE_KEY to the signature key from when you created the webhooks.
    2. Edit RAILS_WEBHOOK_URL to your domain + webhooks url. For instance https://example.com/webhooks. This is used for validating requests.

Seeding Gift Cards

First evaluate which seller the gift cards are going to go towards, how many are needed, and who is going to distribute the gift cards.

Run the command below

NOTE: A dollar is 100. If you wish to seed $5, put 500 as the amount.

heroku run -a {HEROKU APP} 'rake gift_cards:create -s {SELLER_ID} -m {DISTRIBUTOR_EMAIL} -q {QUANTITY} -a {AMOUNT}'

Reference Links

Troubleshooting

If you're getting errors related to your local environment variables not being set, you probably need to create a .env file https://docs.google.com/document/d/1UPNCwjWS_T7XT5AXsewphu6NvNdV7TQLSJub-RBRAG0/edit?ts=5ec88e82

Anytime a new migration is created, you'll have to run rails db:migrate for your local dev environment, and rails db:migrate RAILS_ENV=test for your local test environment

containerized

Running a containerized version of the app requires Docker and Docker Compose. In addition, some updates will be required to the config/database.yml file for the app to work within Docker. You may also need to enable Square webhooks, so reference the Enabling Webhooks section.

docker volume create --name=postgres-data-volume
cd <local repo directory name> && docker-compose up

accessing rail CLI after docker-compose is running.

# check what containers are being run by Docker Compose
cd <local repo directory name> && docker-compose ps
docker exec -it <name of the container with "web_1" in it> bash

# now inside docker container example of commands that can be run
bundle exec rake db:create
bundle exec rake db:migrate

bundle exec rake db:seed

bundle exec rails g model Todo title:string created_by:string

testing 🔬

Tests can be run locally with:

  RAILS_ENV=test bundle exec rspec

Or you can run a specific test with:

  RAILS_ENV=test bundle exec rspec path/to/specific/test-file

deployment 🚀

install heroku-cli

After heroku login and you are added the project if you setup your heroku app, you should have a remote added; if not explicitly run

git remote add heroku https://git.heroku.com/sendchinatownlove.git` git subtree push --prefix api heroku master

or if pipelines already setup, push naturally to branches

DNS & SSL

Follow instructions to enable DNS. https://devcenter.heroku.com/articles/custom-domains

heroku comes with auto-ssl for all hobby and up dynos

run heroku certs:auto:enable

About

backend rails api for sendchinatownlove.com


Languages

Language:Ruby 94.3%Language:HTML 3.7%Language:JavaScript 1.6%Language:Shell 0.3%Language:Dockerfile 0.1%