Wednesday, September 23, 2015

haiku - add users endpoints

create, index, show, update, using json_api_org serializers, with request specs

An endpoint is simply a route defined by your rails application. In terms of an API hitting that endpoint will serve up a resource from your application, or perform some form of action. An example may explain this better.

Say we have an application that handles users and we want our API to expose the users resource. If we follow RESTful convention for our API we will expose seven distinct 'endpoints' linked to seven distinct 'actions' (index, show, create, update, destroy, new, edit) surrounding users.

When building our API, we would make is so anyone who visits "www.myapp.com/users" via a get request is returned some data representation of all users in our application. "/users" is the endpoint. Likewise performing a post action to "/users" with valid data is how we create new users. "/users" is still the endpoint but under different context. If you wanted data for just a single user, it may look something like "www.myapp.com/users/1" in which case "/users/1" is the endpoint.

Request specs provide a thin wrapper around Rails' integration tests, and are designed to drive behavior through the full stack, including routing (provided by Rails) and without stubbing (that's up to you).

Request specs are marked by :type => :request or if you have set
config.infer_spec_type_from_file_location! by placing them in spec/requests.

With request specs, you can:

  • specify a single request
  • specify multiple requests across multiple controllers
  • specify multiple requests across multiple sessions
Check the rails documentation on integration tests for more information.

RSpec provides two matchers that delegate to Rails assertions:

ruby render_template # delegates to assert_template
redirect_to # delegates to assert_redirected_to

JSONAPI::Serializers is a simple library for serializing Ruby objects and their relationships into the JSON:API format.