Project

changebase

0.0
No release in over a year
Ruby library for integrating with Changebase.io
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.2, < 8
>= 5.2, < 8
>= 5.2, < 8
 Project Readme

Changebase Ruby Library

The Changebase Ruby library provides convenient access to the Changebase API from applications written in the Ruby language.

Installation

gem install changebase

If you are installing via bundler:

gem "changebase"

ActionController

In a controller you can use the following to log metadata with all updates during a request:

  class ApplicationController < ActionController::Base
    changebase do
      {
        request_id: request.uuid,
        user: {
          id: current_user.id
        }
      }
    end
  end

The changebase function can be called multiple times to include various data. To nest a value simply give it all the keys so it knows where to bury the value.

Below are several diffent way of including metadata:

  class ApplicationController < ActionController::Base

    # Just a block returning a hash of metadata.
    changebase do
        { my: data }
    end

    # Sets `release` in the metadata to the string RELEASE_SHA
    changebase :release, RELEASE_SHA

    # Sets `request_id` in the metadata to the value returned from the `Proc`
    changebase :request_id, -> { request.uuid }

    # Sets `user.id` in the metadata to the value returned from the
    # `current_user_id` function
    changebase :user, :id, :current_user_id

    # Sets `user.name` in the metadata to the value returned from the block
    changebase :user, :name do
      current_user.name
    end

    def current_user_id
      current_user.id
    end
  end

In the above example the following would be logged with all database changes:

{
  release:     'd5db29cd03a2ed055086cef9c31c252b4587d6d0',
  request_id:  'a39073a5-10b9-41b7-b5f0-06806853507b',
  user: {
    id:        'f06114cc-7819-4906-85dc-b93edb0fb08c',
    name:      'Tom'
  }
}

ActiveRecord

To include metadata when creating or modifying data with ActiveRecord:

  ActiveRecord::Base.with_metadata({user: {name: 'Tom'}}) do
    @post.update(title: "A new beging")
  end

Configuration

Replication Mode

The default mode for the changebase gem is replication. In this mode Changebase is setup to replicate your database and record events via the replication stream.

The default configuration in replication mode is to emit the metadata as messages, this requires no configuration. If this does not work you can configure metadata_mode to be "table". In table mode changebase will write metadata to the "changebase_metadata" table.

To configure the metadata table create an initializer at config/initializers/changebase.rb:

Rails.application.config.tap do |config|
  # Defaults:
  # config.changebase.metadata_mode = "message"
  # config.changebase.metadata_message_prefix = "changebase_metadata"
  config.changebase.metadata_mode = "table"
  config.changebase.metadata_table = "cool_custom_metadata_table"
end

If you are not using Rails you can configure Changebase directly via:

Changebase.metadata_mode = "table"
Changebase.metadata_table = "cool_custom_metadata_table"

Or

Changebase.configure(metadata_mode: "table", metadata_table: "cool_custom_metadata_table")

Inline Mode

If you are unable to setup database replication you can use inline mode. Events will be sent to through the Changebase API. You will collect roughly the same information, but potentionally to miss events and changes in your database if you are not careful, or if another application accesses the database directly.

Limitations

To configure Changebase in the "inline" mode create a initializer at config/initializers/changebase.rb with the following:

Rails.application.config.tap do |config|
  config.changebase.mode = "inline"
  config.changebase.connection = "https://#{ ENV.fetch('CHANGEBASE_API_KEY') }@changebase.io"
end

If you are not using Rails you can configure Changebase directly via:

Changebase.configure do |changebase|
  changebase.mode = "inline"
  changebase.connection = "https://#{ ENV.fetch('CHANGEBASE_API_KEY') }@changebase.io"
end

# Or

Changebase.configure(
    mode: "inline",
    connection: "https://API_KEY@chanbase.io"
)

Bugs

If you think you found a bug, please file a ticket on the issue tracker.