novuhq / novu-ruby

Ruby SDK for Novu - The open-source notification infrastructure for engineers. 🚀
https://rubygems.org/gems/novu
MIT License
23 stars 10 forks source link
api-client api-wrapper hacktoberfest novu novu-api ruby-gem
Logo

Novu Ruby client library

Gem Version Gem

This is a Ruby client library for communicating with the Novu API.

Installation

You can install the client library via RubyGems:

gem install novu

Or add it to your Gemfile:

gem 'novu'

Then run bundle install.

Usage

To use the library, first initialize the client with your API token:

require 'novu'

client = Novu::Client.new(access_token: 'YOUR_NOVU_API_TOKEN')

You can then call methods on the client to interact with the Novu API:

client.notifications

List of all methods

The client methods map directly to the Novu API endpoints. Here's a list of all the available methods. Check the API docs for list of available methods.

Blueprints

client.get_blueprint('<insert-template-id>')
client.group_blueprints_by_category()

Changes

client.changes({
    'page' => 1, # optional
    'limit' => 10, # optional
    'promoted' => 'hello'
})
client.count_changes()
client.apply_change('7789')

Environments

payload = {
    'name' => 'Staging',
    'parentId' => '7789' # optional
}
client.create_environment(payload)
client.environments()
client.update_environment('64a713bdexxxxxx', {
    'name' => 'Local', # optional
    'identifier' => 'local', # optional
    'parentId' => '7789', # optional
    'dns' => { # optional
        'inboundParseDomain' => 'dev.test' # optional
    }
})
client.api_keys()
client.regenerate_api_keys()
client.update_widget_settings({ 'notificationCenterEncryption' => true})

Events

payload = {
    'name' => 'Trigger1',
    'payload' => { # optional
        'first-name' => 'Adam' # optional
    },
    'to' => {
        'subscriberId' => '7789'
    },
    'transactionId' => '89kjfke9893' #optional
}
client.trigger_event(payload)
payload = {
    'events' => [
        {
            'name' => 'Trigger1',
            'payload' => { # optional
                'first-name' => 'Adam' # optional
            },
            'to' => {
                'subscriberId' => '7789'
            },
            'transactionId' => '89kjfke9893' #optional
        },
        {
            'name' => 'Trigger2',
            'payload' => { # optional
                'last-name' => 'Eve' # optional
            },
            'to' => {
                'subscriberId' => '7789'
            },
            'transactionId' => 'sw900999as' #optional
        }
    ]
}
client.trigger_bulk_event(payload)
payload = {
    'name' => 'Trigger',
    'payload' => {
        'first-name' => 'Adam',
        'last-name' => 'Eve'
    },
    'transactionId' => 'sw900999as' #optional
}
client.broadcast_event(payload)
client.cancel_triggered_event('8fxxxee-xxx-4f2b-a0e8-xxxxx')

Execution Details

client.execution_details({
    'notificationId' => '8fxxx-xxx-ef9xxxxce66',
    'subscriberId' => '7789'
})

Feeds

client.create_feed({
    'name' => 'New feed'
})
client.feeds()
client.delete_feed('xxxx714xxa8cbxxxxx2d932')

Inbound Parse

client.validate_mx_record_setup_for_inbound_parse()

Integrations

client.integrations()
body = {
    'providerId' => '<insert-provider-id>',
    'channel' => '<insert-channel>',
    'credentials' => {
        # insert all the fields
    },
    'active' => true,
    'check' => true
}
client.create_integration(body)
client.active_integrations()
client.webhook_provider_status('<insert-provider-id>')
body = {
    'active' => true,
    'check' => true
    'credentials' => {
        # insert all the fields
    },
}
client.update_integration('<insert-provider-id>', body)
client.delete_integration('<insert-provider-id>')
client.channel_limit('<insert-channel-type>')
client.in_app_status()
client.set_integration_as_primary('<insert-integration-id>')

Layouts

payload = {
    'name' => 'New layout',
    'content' => '{{{body}}}',
    'variables' => ['<list-variables-here>'], # optional
    'description' => 'This is a description for the new layout', # optional
    'isDefault' => true # optional
}
client.create_layout(payload)
client.layouts({
    'page' => 1, # optional
    'pageSize' => 10, # optional
    'sortBy' => 'createdAt', # optional
    'orderBy' => 1 # optional
})
client.layout('<insert-layout-id>')
client.delete_layout('<insert-layout-id>')
payload = {
    'name' => 'Update layout', # optional
    'content' => '{{{body}}}', # optional
    'description' => 'This is a description for the new layout', # optional
    'isDefault' => true # optional
    'variables' => ['<list-variables-here>'], # optional
}
client.update_layout('<insert-layout-id>', payload)
client.make_default_layout('<insert-layout-id>')

Messages

payload = {
    'channel' => 'slack', # optional
    'subscriberId' => '7789', # optional
    'transactionId' =>'sw900999as', # optional
    'page' => 4, # optional
    'limit' => 10, # optional
}
client.messages(payload)
client.delete_message('<insert-message-id>')

Notification Groups

client.create_notification_group({
    'name' => '<insert-name>'
})
client.notification_groups()

Notification Templates

client.notification_templates({
    'page' => 4, # optional
    'limit' => 10, # optional
})
body = {
    'name' => '<insert-name>',
    'notificationGroupId' => 'notificationGroupId',
    'tags' => ['tags'], # optional
    'description' => 'description', # optional
    'steps' => [ # optional
       # insert all fields here
    ], 
    'active' => true, # optional
    'draft' => true, # optional
    'critical' => true, # optional
    'preferenceSettings' => { # optional
       # insert all fields here
    },
    'blueprintId' => 'blueprintId' # optional
}
client.create_notification_template(body)
body = {
    'name' => '<insert-name>',
    'notificationGroupId' => 'notificationGroupId',
    'tags' => ['tags'], # optional
    'description' => 'description', # optional
    'steps' => [ # optional
       # insert all fields here
    ], 
    'active' => true, # optional
    'draft' => true, # optional
    'critical' => true, # optional
    'preferenceSettings' => { # optional
       # insert all fields here
    },
    'blueprintId' => 'blueprintId' # optional
}
client.update_notification_template('<insert-template-id>', body)
client.delete_notification_template('<insert-template-id>')
client.notification_template('<insert-template-id>')
client.notification_template_blueprint('<insert-template-id>')
client.create_notification_template_blueprint('<insert-template-id>')
client.update_notification_template_status('<insert-template-id>', { 'active' => true })

Notification

body = {
    'channels' => ['<insert-channels>'],
    'templates' => ['<insert-templates>'],
    'emails' => ['<insert-emails>'],
    'search' => '<insert-search-string>'
    'page' => 2 , # optional
    'transactionId' =>'sw900999as', # optional
}
client.notifications(body)
client.notifications_stats()
client.notifications_graph_stats({
    'days' => 5 # optional
})
client.notification('<insert-notification-id>')

Organizations

client.create_organization({
    'logo' => '<insert-organization-logo>',
    'name' => '<insert-organization-name>', 
}) 
client.organizations() 
client.current_organization() 
client.current_organization_members() 
client.rename_organization({
    name: '<insert-organization-name>'
}) 
client.organization_branding({
    logo: '<insert-organization-logo>',
    color: '<insert-organization-color>',
    fontColor: '<insert-organization-fontColor>',
    contentBackground: '<insert-organization-contentBackground>',
    fontFamily: '<insert-organization-fontFamily>'
}) 
client.delete_organization_member('<insert-member-id>')

Subscribers

client.subscribers({
    'page' => 1, # optional
    'limit' => 15, # optional
}) 
payload = {
    'subscriberId' => '7789',
    'email' => '<insert-email>', # optional
    'firstName' => '<insert-firstName>', # optional
    'lastName' => '<insert-lastName>', # optional
    'phone' => '<insert-phone>', # optional
    'avatar' => '<insert-profile-avatar>' # optional
}
client.create_subscriber(payload)
client.subscriber('<insert-subscriber-id>')
payload = {
    'email' => '<insert-email>', # optional
    'firstName' => '<insert-firstName>', # optional
    'lastName' => '<insert-lastName>', # optional
    'phone' => '<insert-phone>', # optional
    'avatar' => '<insert-profile-avatar>' # optional
}
client.update_subscriber('<insert-subscriber-id>', payload)
client.delete_subscriber('<insert-subscriber-id>')
body = {
    'providerId' => '<insert-provider-id>',
    'credentials' => {
        # Insert all fields here 
    } 
}
client.update_subscriber_credentials('<insert-subscriber-id>', body)
client.delete_subscriber_credentials('<insert-subscriber-id>', '<insert-provider-id>')
client.update_subscriber_online_status('<insert-subscriber-id>', { 'isOnline' => true })
client.subscriber_preferences('<insert-subscriber-id>')
body = {
    'channel' => {
        # Insert all fields here
    },
    'enabled' => true
}
client.update_subscriber_preference('<insert-subscriber-id>', '<insert-template-id>', body)
client.subscriber_notification_feed('<insert-subscriber-id>', {
    'page' => 3, # optional
    'limit' => 15, # optional
    'read' => true, # optional
    'seen' => true, # optional
})
client.subscriber_unseen_notification_count('<insert-subscriber-id>', {
    'limit' => 15, # optional
    'seen' => false, # optional
})
client.mark_subscriber_feed_seen('<insert-subscriber-id>', {
    'messageId' => '<insert-message-id>',
    'mark' => {
        'seen' => true,
        'read' => true
    }
})
client.mark_message_action_seen('<insert-subscriber-id>', '<insert-message-id>', '<insert-type>')
body = {
  'markAs' => 'seen'
}
client.mark_all_subscriber_messages('<insert-subscriber-id>', body)

Tenants

client.create_tenant({
    'identifier' => '<a-unique-identifier>',
    'name' => '<name-of-the-tenant>',
    'data' => {
        'company' => 'a company',
        'date-registered' => '2023-0-15'
    }
})
client.tenants({
    'page' => 1, # optional
    'limit' => 10, # optional
})
client.tenant('<a-unique-identifier>')
client.update_tenant('<a-unique-identifier>', {
    'identifier' => '<a-unique-identifier>',
    'name' => '<name-of-the-tenant>',
})
client.delete_tenant('<a-unique-identifier>')

Topics

client.create_topic({
    'key' => 'key',
    'name' => 'name'
})
client.topics({
    'name' => 'name', # optional
    'page' => 1, # optional
    'pageSize' => 10, # optional
})
client.add_subscribers('<insert-topic-key>', {
    'subscribers' => ['<insert-list-of-subscribers>']
})
client.
client.topic('<insert-topic-key>')
client.rename_topic('<insert-topic-key>', {
    'name' => 'new name'
})
client.delete_topic('<insert-topic-key>')
client.subscriber_topic('<insert-topic-key>', '<insert-externalSubscriberId>')

Idempotent Request

This SDK allows you to perform idempotent requests, that is safely retrying requests without accidentally performing the same operation twice. To achieve this, provide an idempotency_key argument during initialization of the NOVU client. We strongly recommend that you use UUID format when you're generating your idempotency key.

client = Novu::Client.new(
  access_token: '<your-novu-api_key>', 
  idempotency_key: '<your-idempotency-key>'
)

Exponential Retry Mechanism

You can configure this SDK to retry failed requests. This is done by using Exponential backoff strategy. It is a common algorithm for retrying requests. The retries gradually extend the waiting time until reaching a specific limit. The concept is to prevent overloading the server with simultaneous requests once it is back online, especially in cases of temporary downtime.

client = Novu::Client.new(
  access_token: '<your-novu-api_key>', 
  idempotency_key: '<your-idempotency-key>',
  enable_retry: true, # it is disabled by default,
  retry_config: {
    max_retries: 3, 
    initial_delay: 4, 
    max_delay: 60
  }
)

The retry configuration is explained in the following table:

Options Detail Default Value Data Type
max_retries Specifies total number of retries to perform in case of failure 1 Integer
initial_delay Specifies the minimum time to wait before retrying (in seconds) 4 Integer
max_delay Specifies the maximum time to wait before retrying (in seconds) 60 Integer
enable_retry enabling/disable the Exponential Retry mechanism false Boolean

Custom backend URL

To use a custom backend URL, for example to be able to use the EU Novu API, you can pass a custom backend_url when initializing the client.

client = Novu::Client.new(
  access_token: '<your-novu-api_key>', 
  backend_url: 'https://eu.api.novu.co/v1'
)

For more information about these methods and their parameters, see the API documentation.

Contributing

Bug reports and pull requests are welcome here.

Support and Feedback

Be sure to visit the Novu official documentation website for additional information about our API. If you need additional assistance, join our Discord server here.

License

Novu Ruby SDK is licensed under the MIT License - see the LICENSE file for details.