Sha256: d03028a48e215ec1f5a2eaf7a692de380d121c54b6f9334a485ff92e86beaa0f

Contents?: true

Size: 1.86 KB

Versions: 7

Compression:

Stored size: 1.86 KB

Contents

require 'createsend'
require 'json'

module CreateSend
  # Represents a person and associated functionality.
  class Person
    attr_reader :client_id
    attr_reader :email_address

    def initialize(client_id, email_address)
      @client_id = client_id
      @email_address = email_address
    end

    # Gets a person by client ID and email address.
    def self.get(client_id, email_address)
      options = { :query => { :email => email_address } }
      response = CreateSend.get "/clients/#{client_id}/people.json", options
      Hashie::Mash.new(response)
    end

    # Adds a person to the client. password is optional. if ommitted, an
    # email invitation will be sent to the person
    def self.add(client_id, email_address, name, access_level, password)
      options = { :body => {
        :EmailAddress => email_address,
        :Name => name,
        :AccessLevel => access_level,
        :Password => password }.to_json }
      response = CreateSend.post "/clients/#{client_id}/people.json", options
      Hashie::Mash.new(response)
    end

    # Updates the person details. password is optional and will only be
    # updated if supplied
    def update(new_email_address, name, access_level, password)
      options = {
        :query => { :email => @email_address },
        :body => {
          :EmailAddress => new_email_address,
          :Name => name,
          :AccessLevel => access_level,
          :Password => password }.to_json }
      CreateSend.put uri_for(client_id), options
      # Update @email_address, so this object can continue to be used reliably
      @email_address = new_email_address
    end

    # deletes this person from the client
    def delete
      options = { :query => { :email => @email_address } }
      CreateSend.delete uri_for(client_id), options
    end

    def uri_for(client_id)
         "/clients/#{client_id}/people.json"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
createsend-2.5.1 lib/createsend/person.rb
createsend-2.5.0 lib/createsend/person.rb
createsend-2.4.0 lib/createsend/person.rb
createsend-2.3.0 lib/createsend/person.rb
createsend-2.2.0 lib/createsend/person.rb
createsend-2.1.0 lib/createsend/person.rb
createsend-2.0.0 lib/createsend/person.rb