Consumerable

Quickly write API object mapper libraries without having to re-invent the wheel

Installation

Add this line to your application's Gemfile:

gem 'consumerable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install consumerable

Usage

First configure consumerable for the REST API that you would like to consume

Consumerable.configure do |config|
  config.endpoint               = 'https://api.heroku.com'
  config.content_type           = :json
  config.accept_header          = 'application/vnd.heroku+json; version=3'
  config.basic_auth_username    = ENV['HEROKU_USERNAME']
  config.basic_auth_password    = ENV['HEROKU_PASSWORD']
  config.logger                 = my_logger
end

Then in your gem/application create the classes to match the API

class HerokuPlatform::App
  include Consumerable

  attribute :name,  String

  consumerable path: '/apps'

  has_many :collaborators, class_name: 'HerokuPlatform::Collaborators',
    source: :app
end

class HerokuPlatform::Collaborator
  include Consumerable

  attribute :email, String

  consumerable path: "/apps/:app_id/collaborators"

  belongs_to :app, class_name: 'HerokuPlatform::App'
end

Profit

app = HerokuPlatform::App.create!(name: 'some app')
app.collaborators.create(email: '[email protected]')

Contributing

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