evgenyantipin / notiesend-ruby

NotiSend Official Ruby Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Notisend-Ruby Build Status Gem Version

This is the NotiSend Ruby Library. This library contains methods for easily interacting with the NotiSend API. Below are examples to get you started.

Installation

Run in console:

gem install notisend-ruby

or add to Gemfile:

gem 'notisend-ruby'

Configuration

First, you need to set up you api token. You can do that by using the environment varialble NOTISEND_API_TOKEN or set it in code:

require 'notisend'

Notisend.configure do |config|
  config.api_token = 'my-secret-token'
end

Messages

Send a message from sidekiq job

class ExampleJob
  include Sidekiq::Worker
  def perform(id)
    Notisend::Message.deliver(from_email: 'from@gmail.com',
                              to: 'to@gmail.com',
                              subject: "text subject",
                              html: ApplicationController.new.render_to_string(template: 'mailer/template_example.slim',
                                                                               layout: 'email',
                                                                               locals: { })
                              )
  end
end
message = Notisend::Message.deliver(
  from_email: 'sender@mail.com',
  to: 'recipient@mail.com',
  subject: 'Hello',
  html: '<h1>World</h1>',
  text: 'World'
)

### Send a message

```ruby
message = Notisend::Message.deliver(
  from_email: 'sender@mail.com',
  to: 'recipient@mail.com',
  subject: 'Hello',
  html: '<h1>World</h1>',
  text: 'World'
)
message # => #<Notisend::Message id=1, payment="subscriber", from_email="sender@mail.com", from_name=nil, to="recipient@mail.com", subject="Hello", text="World", html="<h1>World</h1>", attachments=[], status="queued", events={}>

Get message's info

message = Notisend::Message.get(id: 1)
message # => #<Notisend::Message id=1, payment="subscriber", from_email="sender@mail.com", from_name=nil, to="recipient@mail.com", subject="Hello", text="World", html="<h1>World</h1>", attachments=[], status="queued", events={}>

Send a message using a template

message = Notisend::Message.deliver_template(template_id: 1, to: 'recipient@mail.com')
message # => #<Notisend::Message id=1, payment="subscriber", from_email=nil, from_name=nil, to="recipient_mail.com", subject=nil, text=nil, html=nil, attachments=[], status="queued", events={}>

Lists

Get the lists

result = Notisend::List.get_all # get all lists paginated
result = Notisend::List.get_all(params: { page_number: 1, page_size: 1 }) # or get the specific page
result # =>  #<Notisend::Collection total_count=12, total_pages=12, page_number=1, page_size=1, collection=[#<Notisend::List id=1, title="My List">]>
result.collection.first # => #<Notisend::List id=1, title="My List">

Create a list

list = Notisend::List.create(title: 'New List')
list # => #<Notisend::List id=2, title="New List">

Get list's info

list = Notisend::List.get(id: 2)
list # => #<Notisend::List id=2, title="New List">

Parameters

Get list's parameters

result = Notisend::Parameter.get_all(list_id: 1)
result = Notisend::Parameter.get_all(list_id: 1, params: { page_number: 1, page_size: 1 })
result # => #<Notisend::Collection total_count=1, total_pages=1, page_number=1, page_size=1, collection=[#<Notisend::Parameter id=1, title="Full Name", kind="string", list_id=1>]>
result.collection.first # => #<Notisend::Parameter id=1, title="Full Name", kind="string", list_id=1>

Create a list's parameter

parameter = Notisend::Parameter.create(title: 'Age', kind: 'numeric', list_id: 1)
parameter # => #<Notisend::Parameter id=2, title="Age", kind="numeric", list_id=1>

Recipients

Get list's recipients

result = Notisend::Recipient.get_all(list_id: 1)
result = Notisend::Recipient.get_all(list_id: 1, params: { page_number: 1, page_size: 1 })
result # => #<Notisend::Collection total_count=3, total_pages=3, page_number=1, page_size=1, collection=[#<Notisend::Recipient id=1, email="recipient@mail.com", list_id=1, confirmed=true, values=[]>]>
result.collection.first # => #<Notisend::Recipient id=1, email="recipient@mail.com", list_id=1, confirmed=true, values=[]>

Create a recipient

recipient = Notisend::Recipient.create(email: 'recipient@mail.com', list_id: 1) # create a recipient without values
recipient = Notisend::Recipient.create(email: 'recipient1@mail.com', list_id: 1, values: [{parameter_id: 1, value: "foobar"}])
recipient # => #<Notisend::Recipient id=1, email="recipient@mail.com", list_id=1, confirmed=true, values=[{"value"=>"foobar", "kind"=>"string", "parameter_id"=>1}]>

Update a recipient

recipient = Notisend::Recipient.update(id: 1, list_id: 1, email: 'new.recipient@mail.com')
recipient = Notisend::Recipient.update(id: 1, list_id: 1, values: [{parameter_id: 1, value: "barbaz"}])
recipient # => #<Notisend::Recipient id=1, email="new.Recipient@mail.com", list_id=1, confirmed=true, values=[{"value"=>"barbaz", "kind"=>"string", "parameter_id"=>1}]>

Import a batch of recipients

result = Notisend::Recipient.import(
  list_id: 1,
  recipients: [
    { email: 'recipient@mail.com' },
    { email: 'recipient1@mail.com', values: { [{ parameter_id: 1, value: 'some value' }] } },
  ]
)
result # => #<Notisend::RecipientsImport id=1, status="queued">

Delete a recipient

Notisend::Recipient.delete(list_id: 1, id: 1) # => nil

Contributing

  1. Fork it
  2. Create your feature branch
  3. Test your feature and follow style guide (feel free to run rake, rspec and rubocop)
  4. Commit your changes
  5. Push to the branch
  6. Create a new Pull Request

About

NotiSend Official Ruby Library


Languages

Language:Ruby 100.0%