Skip to content

Instantly share code, notes, and snippets.

@dwayne
Last active October 3, 2022 16:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dwayne/9902605 to your computer and use it in GitHub Desktop.
Save dwayne/9902605 to your computer and use it in GitHub Desktop.
Surviving APIs with Rails Notes

Recap

http://railsapis.codeschool.com/levels/1/challenges/1

Topics

  • REST
  • Routes
  • Constraints
  • Namespaces

REST

REST stands for REpresentational State Transfer.

By following a strict set of operations, REST allows building an infrastructure that can support different types of applications.

Routes

The rake routes command lists all the routes, URIs, controller#actions, and URL helpers available on our app.

Tip: You can use with_options to factor duplication out of options passed to a series of method calls.

Resources

Constraints

You can use constraints to enforce a subdomain. Keeping our API under its own subdomain allows load balancing traffic at the DNS level.

Using Subdomains in Development

resources :posts,    constraints: { subdomain: 'api' }
resources :comments, constraints: { subdomain: 'api' }

Edit the /etc/hosts file. For example:

127.0.0.1   my-app-dev.com
127.0.0.1   api.my-app-dev.com

This would then map to:

Resources

Namespaces

Tip: You can pass a constraints option to the namespace method.

Resources

API not Api

This is a sweet tip.

If we want to write an acronym in the traditional ALL CAPS then Rails inflections can help us out.

# config/initializers/inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment