Braque
Braque aims to provide a simple and familiar interface for setting up clients to interact with Hypermedia (hal+json) API services. It is a lightweight wrapper around Hyperclient and ActiveAttr.
Braque is an early-stage and exploratory project.
Model setup
Braque::Model
is ActiveSupport concern. You can use Braque::Model to map a remote resource to a class in your application. Do so by including Braque::Model in the class, defining the API service's root url and authentication details, and listing attributes which we expect from the API.
class Article
include Braque::Model
self.api_root = Rails.application.config_for(:article_service)['url']
self.api_token = Rails.application.config_for(:article_service)['token']
attribute :id
attribute :title
attribute :body
attribute :summary
attribute :created_at
attribute :updated_at
end
In a Rails app, you can then use this model simply:
class ArticlesController < ApplicationController
before_filter :find_article, except: [:index, :new]
def index
@articles = Article.list(page: params[:page], size: params[:size])
end
def new
end
def create
@article = Article.create params[:article]
redirect_to article_path(@article)
end
def show
end
def edit
end
def update
@article = @article.save params[:article]
redirect_to article_path(@article.id)
end
def destroy
@article.destroy
redirect_to articles_path
end
private
def find_article
@article = Article.find(id: params[:id])
end
end