Ruby Brainstem Adaptor

Build Status Dependency Status Gem Version

Brainstem Adaptor provides an easy to use interface for Brainstem API.

BrainstemAdaptor.load_specification('my_api_service.yml')
response_data = MyApi.get('/users.json')

# response_data can be a JSON string or Hash
response = BrainstemAdaptor::Response.new(response_data)

response.count                 # returns total count as Integer
# ...
response.results               # returns Array<BrainstemAdaptor::Record>
response.results[0]['friends'] # returns Array<BrainstemAdaptor::Record>

response.results[0]['friends'].last['name']

response.results[0]['name']    # returns String
response.results[0].id         # returns String or Integer
response.results[0]['mom']     # returns BrainstemAdaptor::Record

response.results[0]['mom']['name']
# ...
response['users']              # returns plain Hash as is from response
response['count']              # same here, Integer

Installation

Run:

gem install brainstem-adaptor

Or put in your Gemfile:

gem 'brainstem-adaptor'

Configuration

Create specification for your API.

The only missing thing in Brainstem responses is an associations information. Having customer_ids field does not guarantee that customer ids are related to customers collection, those may also be ids of users collection. Specification is the thing which describes missing information about responses you receive. You can also put any additional information you need in your specification (see details below).

---
# my_api_service.yml file
users:
  associations:
    friends:
      foreign_key: friend_ids
      collection: 'users'
    mom:
      foreign_key: mom_id
      collection: 'users'

projects:
  fields:
    name:
      type: string
      required: true
    employee_ids:
      type: array

  associations:
    employees:
      foreign_key: employee_ids
      collection: users

Multiple specifications

You may also have multiple APIs which have different specificaitons.

BrainstemAdaptor::Specification[:my_tracker_api] = { 'users' => {} } # ...
response_data = MyApi.get('/users.json')

response = BrainstemAdaptor::Response.new(response_data, BrainstemAdaptor::Specification[:my_tracker_api])
# ...

Contributing

  1. Fork
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request (git pull-request)

License

Brainstem Adaptor was created by Mavenlink, Inc. and available under the MIT License.