Giter Site home page Giter Site logo

emaildirect-ruby's Introduction

emaildirect

A ruby library which implements the complete functionality of the REST (v5) Email Direct API.

Installation

Plain ruby

gem install emaildirect
require 'emaildirect'
EmailDirect.api_key = 'your_api_key'

Rails integration

In your gemfile:

gem 'emaildirect'

In an initializer:

EmailDirect.api_key = 'your_api_key'

Examples

Retrieve a list of all your publications.

EmailDirect::Publication.all.Publications.each do |pub|
  puts "#{pub.PublicationID}: #{pub.Name}"
end

Results in:

1: Publication One
2: Publication Two

Create, then remove a Publication

response = EmailDirect::Publication.create('Test', :Description => 'Test Publication')
sub = EmailDirect::Publication.new(response.publicationID)
sub.delete

Updating a subscriber's custom fields

A single attribute:

EmailDirect::Subscriber.new(email).update_custom_field :FirstName, 'Pat'

Multiple attributes:

EmailDirect::Subscriber.new(email).update_custom_fields :FirstName => 'Pam', :LastName => 'Sinivas'

When creating a subscriber

EmailDirect::Subscriber.create(email, :Publications => [1], :CustomFields => { :FirstName => 'Pam', :LastName => 'Sinivas' }

ActionMailer integration

You can use send your ActionMailer email through Email Direct using their Relay Send functionality by setting up a new delivery method in an initalizer:

 ActionMailer::Base.add_delivery_method :emaildirect, EmailDirect::Mailer,
                                        :category_id => 1,
                                        :options   => { :Force => true }

And in your ActionMailer class:

defaults :delivery_method => :emaildirect

or for just a particular message:

def welcome(user) do
  mail :to => user.email,
       :delivery_method => :emaildirect
end

Handling errors

If the emaildirect API returns an error, an exception will be thrown. For example, if you attempt to create a subscriber with an invalid email address:

begin
  response = EmailDirect::Subscriber.create('bademail')
  rescue EmailDirect::BadRequest => br
    puts "Bad request error: #{br}"
    puts "Error Code:    #{br.data.ErrorCode}"
    puts "Error Message: #{br.data.Message}"
  rescue Exception => e
    puts "Error: #{e}"
end

Results in:

Bad request error: The EmailDirect API responded with the following error - 101: Invalid Email Address
Error Code:    101
Error Message: Invalid Email Address

Disabling for development/testing

A helper method is provided to disable talking to the EmailDirect REST server (requires the Fakeweb gem)

EmailDirect.disable

Expected input and output

The best way of finding out the expected input and output of a particular method in a particular class is to read the API docs and take a look at the code for that function.

Credits

  • Jason Rust
  • createsend-ruby library for inspiration on how to write a decent REST API wrapper.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.