Skip to content

Instantly share code, notes, and snippets.

@Ross-Hunter
Created January 9, 2018 13:10
Show Gist options
  • Save Ross-Hunter/c51804e43ee813bd1a003a1fec12b442 to your computer and use it in GitHub Desktop.
Save Ross-Hunter/c51804e43ee813bd1a003a1fec12b442 to your computer and use it in GitHub Desktop.
Notes from JSONAPI Ruby Meetup

Who am I? http://ross-hunter.com/

What is JSON API? http://jsonapi.org/format/

What is JSONAPI::Resources http://jsonapi-resources.com/

What is jsonapi_expectations https://ross-hunter.github.io/jsonapi_expectations/

We also interact with Pundit https://github.com/varvet/pundit/

JSONAPI::Resources allows you to define Resources that kind of act like views on top of your existing ActiveRecord models. These can be very simple and "just work" if you have simple CRUD - but they also give you a place to customize behavior.

You also create controllers, but these will be completely empty if you are just doing JSONAPI compliant CRUD. Just need to set up authorization and error handling in the base controller.

Set up Pundit with Pundit::Resources. Need a basic application policy, and then create a policy for each resource. It usually makes sense to create abstractions like "OwnershipPolicy". Then most of the policies will be empty.

We create a request spec to test our api, start with the following matchers

expect_status :ok
expect_item_count widgets.size
expect_record widgets.first

Request Spec Helpers https://gist.github.com/Ross-Hunter/a14ff663d79b42dfec5d2a7c3b94c4b0

rails g model widget name:string site:references (type: :int)

add spec - run the spec after each step
add route
add resource
add controller
add policy
add actual behavior tests

Check out all the other cool matchers!

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