class DoList::PushEntry

Public Class Methods

new(params = {}) click to toggle source
# File lib/do-list/push_entry.rb, line 5
def initialize(params = {})
  @headers = params[:headers]
  @account_id = params[:account_id]
  @auth_token = params[:auth_token]
  @uri = DoList::UrlParser.parse_url(params[:url])
  @http = DoList::UrlParser.request_http(@uri)
end

Public Instance Methods

push_entry(fields) click to toggle source
# File lib/do-list/push_entry.rb, line 13
def push_entry(fields)
  return false unless fields[:email]

  body = create_body(fields)
  DoList::UrlParser.post_http(@http, @uri, body, @headers)
end

Private Instance Methods

body_fields(database_fields) click to toggle source
# File lib/do-list/push_entry.rb, line 30
    def body_fields(database_fields)
      database_fields = database_fields.map do |name, value|
        <<-FIELD
          <ns2:Field>
            <ns2:Name>#{name}</ns2:Name>
            <ns2:Value>#{value}</ns2:Value>
          </ns2:Field>
        FIELD
      end
      return '<ns2:Fields/>' if database_fields.empty?

      '<ns2:Fields>' + database_fields.join('') + '</ns2:Fields>'
    end
create_body(fields) click to toggle source
# File lib/do-list/push_entry.rb, line 22
def create_body(fields)
  email = fields.delete(:email).to_s
  mail_opt_out = fields.delete(:mail_opt_out) == 1 ? 1 : 0
  mobile_opt_out = fields.delete(:mobile_opt_out) == 1 ? 1 : 0
  fields = body_fields(fields)
  xml_body(fields, email, mail_opt_out, mobile_opt_out)
end
xml_body(fields, email, mail_opt_out, mobile_opt_out) click to toggle source
# File lib/do-list/push_entry.rb, line 44
    def xml_body(fields, email, mail_opt_out, mobile_opt_out)
      <<-ADDENTRYBODY
        <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.dolist.net/v2" xmlns:ns2="http://schemas.datacontract.org/2004/07/Dolist.ContactManagement.API.DataContract.Management">
          <SOAP-ENV:Body>
            <ns1:SaveContact>
              <ns1:token>
                <ns1:AccountID>#{@account_id}</ns1:AccountID>
                <ns1:Key>#{@auth_token}</ns1:Key>
              </ns1:token>
              <ns1:contact>
              <ns2:Email>#{email}</ns2:Email>
              #{fields}
              <ns2:InterestsToAdd/>
              <ns2:InterestsToDelete/>
              <ns2:OptoutEmail>#{mail_opt_out}</ns2:OptoutEmail>
              <ns2:OptoutMobile>#{mobile_opt_out}</ns2:OptoutMobile>
              </ns1:contact>
            </ns1:SaveContact>
          </SOAP-ENV:Body>
        </SOAP-ENV:Envelope>
      ADDENTRYBODY
    end