Skip to content
Matthew Werner edited this page Mar 18, 2014 · 5 revisions

Format

Each Spigot map file represents one service, with as many resources defined as you like.

Basic Map
Spigot.resource(:user) do
  name :full_name
  email :login
  username :username
end
Multiple Resources

Spigot will look up the map for the name of the class implementing the method. This let's you map several of your resources that you're getting from the same service.

Spigot.define do
  resource(:user) do
    name :full_name
    email :login
    username :username
  end

  resource(:post) do
    title :title
    body :description
    timestamp :created_at
  end
end
Nested Attributes

Sometimes your data comes back in nested hashes. No problem! Just nest your map as well, and Spigot will traverse its way down to the attributes you're looking for.

Spigot.resource(:user) do
  name :full_name
  username :username
  contact do
    email :login
    phone :telephone
  end
end
Nested Resources

If the API data you are receiving has an associated resource which you would also like to capture, you can pass a class inside the resource block to use that resource's Spigot map to parse the data.

Spigot.resource(:pull_request) do
  id      :github_id
  number  :number
  author  User
  created :created_at
end

Spigot will recognize that User has a defined Spigot map and will use it to create a user. Once the nested data has been parsed and used to create a user, Spigot will merge a key value pair associating the created user's id to the API data which PullRequest will use.

# Original
{ id: 123, number: 456, author: { id: 987, login: 'mwerner' }, created: '2000-04-01 00:01:00' }

# Parsed data, passed to PullRequest
{ github_id: 123, number: 456, user_id: 1, created_at: '2000-04-01 00:01:00' }

This let's you easily parse complex subsets of data and associate it using the same single Spigot command.

Clone this wiki locally