Skip to content

Instantly share code, notes, and snippets.

@jwaldrip
Created June 17, 2016 03:58
Show Gist options
  • Save jwaldrip/f334ab1a5ba35b6c0113f3a08351f14b to your computer and use it in GitHub Desktop.
Save jwaldrip/f334ab1a5ba35b6c0113f3a08351f14b to your computer and use it in GitHub Desktop.
  1. The request enters the server: https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/server.rb#L15-L17

  2. The resouce and id (if present) are identified and used to lookup a resource. https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/server.rb#L31-L41

  3. The resource is found by its name. Resource definitions are not converted to classes until they are called the first time. When defining a resource you are actually just defining a block that is eventually passed to Class.new(parent, &resource block) https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/base/resource_definitions.rb#L11-L23

  4. process is called on the resource. Here we determine if an action on the main resource can fufill the request or if the request belongs to a relationship. The relationship lookup just includes an extra step to locate the relationship. https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/resource/definitions/actions.rb#L87-L95

    Actions are prefined mappings that the user defines on the resource as methods. They are list, create, read, update, delete. Additionally the actions show, add, remove are available on relationships.

  5. Once process locates an action it calls new on the resource. This sets up all the necessary context made available to the request. https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/action.rb#L97-L105 https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/resource.rb#L38-L62

    Contexts are defined as blocks and only invoked on first invocation, This is to prevent from invalid contexts from being called and to reduce the number of method calls.

  6. call is invoked on the resource. The result of which calls the commit block of the action. For example:

    create do |context|
      # create the resource
    end
  7. Once the commit block is run, the response is processed. This is done by using a series of builders. https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/resource/builders.rb https://github.com/brandfolder/jsonapionify/blob/master/lib/jsonapionify/api/resource/builders/resource_builder.rb

  8. Once this is complete, Oj builds the JSON object and sends it to the client.

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