Day 05 - Account Subscriptions [Rails 5]
- Application: Complete and tested with one endpoint
- CI: Complete
- Dev: Complete
- Production: Complete
Week two started off with a trip back to Rails 5. Carson Stauffer was able to pair with me for an account subscription api. We started off with API design which was really insightful since the previous model locked our subscriptions to only an email. Carson had a great idea to increase flexibilty of contact channels by altering the table structure to handle dynamic channels such as SMS or messengers. After a week of iterations, the process became pretty smooth and looked like this:
Design: * Interface * methods * inputs * schema * Naming (not as easy as it sounds)
Skeleton: * Github repository * Docker hub repository * Choose service port * Generate application using our CLI
Configure: * DataDog * dotenv * database * honeybadger * health check endpoint
Implement: * generate endpoints * write more tests * add pagination * json schema for jsonapi.org 1.0
Deployment: * circle CI * Docker * Dockerfile * rancher configuration
Now that we've done this a few times, maybe we should flip the deployment and implement since thats our goal. As an engineering organization we could work more efficiently if the application is deployed with minimum effort and we begin iterating immediately.
Learning for the day: API Generators
There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton
Naming is one of the two hard things to do in Computer Science. Today we started with something as simple as
email subscriptions but since we generalized it,
We leverage rails scaffolding to generate endpoints quickly, but they always seem to need work to get them perfect. In particular, we need proper namespacing for versioning in the future. Although not an extreme amount of work to hand build, we saved a lot of time by namespacing when generating the scaffolding.
In rails you can add namespaces to your objects and it will automatically add those namespaces to your files.
$ rails g scaffold AccountSubscriptons::V1::Subscriptions name:string invoke active_record create db/migrate/20170613064344_create_account_subscription_v1_subscriptions.rb create app/models/account_subscription/v1/subscription.rb create app/models/account_subscription/v1.rb invoke test_unit create test/models/account_subscription/v1/subscription_test.rb create test/fixtures/account_subscription/v1/subscriptions.yml invoke resource_route route namespace :account_subscription do namespace :v1 do resources :subscriptions end end invoke scaffold_controller create app/controllers/account_subscription/v1/subscriptions_controller.rb invoke test_unit create test/controllers/account_subscription/v1/subscriptions_controller_test.rb invoke jbuilder create app/views/account_subscription/v1/subscriptions create app/views/account_subscription/v1/subscriptions/index.json.jbuilder create app/views/account_subscription/v1/subscriptions/show.json.jbuilder create app/views/account_subscription/v1/subscriptions/_account_subscription_v1_subscription.json.jbuilder
This is nice but not ideal. It has segmented off the models into a namespace as well, when all we needed was the controller to be namespace.
More customized generators will be a perfect addition to our CLI.