class SocketLabs::InjectionApi::Core::InjectionRequestFactory

Attributes

api_key[R]
server_id[R]

Public Class Methods

new(server_id, api_key) click to toggle source

Creates a new instance of the InjectionRequestFactory. @param [String] server_id @return [String] api_key

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 29
def initialize(server_id, api_key)

  @server_id = server_id
  @api_key = api_key

end

Public Instance Methods

generate_request(message) click to toggle source

Generate the InjectionRequest for sending to the Injection Api. @param [BasicMessage, BulkMessage] message: the message object to convert @return [InjectionRequest] the converted InjectionRequest

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 39
def generate_request(message)

  request = InjectionRequest.new

  if message.instance_of? BasicMessage
    request = generate_basic_message_request(message)
  end

  if message.instance_of? BulkMessage
    request = generate_bulk_message_request(message)
  end

  request

end

Private Instance Methods

bulk_recipient_to_address_json(address) click to toggle source

Simple converter from BulkRecipient to AddressJson @param [BulkRecipient] address: the BulkRecipient object to convert @return [AddressJson] the converted AddressJson object

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 226
def bulk_recipient_to_address_json(address)

  if address.friendly_name.nil? || address.friendly_name.empty?
    AddressJson.new(address.email_address)
  else
    AddressJson.new(address.email_address, address.friendly_name)
  end

end
email_address_to_address_json(address) click to toggle source

Simple converter from EmailAddress to AddressJson @param [EmailAddress] address: the EmailAddress object to convert @return [AddressJson] the converted AddressJson object

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 197
def email_address_to_address_json(address)

  unless address.friendly_name.nil? || address.friendly_name.empty?
    AddressJson.new(address.email_address, address.friendly_name)
  else
    AddressJson.new(address.email_address)
  end

end
generate_base_message(message) click to toggle source

Converts MessageBase object to a MessageJson object @param [MessageBase] message: the message to convert @return [MessageJson] the convert MessageJson object

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 59
def generate_base_message(message)

  message_json = MessageJson.new
  message_json.subject = message.subject
  message_json.plain_text_body = message.plain_text_body
  message_json.html_body = message.html_body
  message_json.amp_body = message.amp_body
  message_json.mailing_id = message.mailing_id
  message_json.message_id = message.message_id
  message_json.charset = message.charset
  message_json.from_email_address = email_address_to_address_json(message.from_email_address)
  message_json.custom_headers = populate_custom_headers(message.custom_headers)
  message_json.attachments = populate_attachments(message.attachments)

  unless message.api_template.nil?
      message_json.api_template = message.api_template
  end
  unless message.reply_to_email_address.nil?
      message_json.reply_to = email_address_to_address_json(message.reply_to_email_address)
  end
  message_json

end
generate_basic_message_request(message) click to toggle source

Generate the InjectionRequest for sending to the Injection Api. @param [BasicMessage] message: the basic message object to convert @return [InjectionRequest] the converted InjectionRequest

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 140
def generate_basic_message_request(message)

  message_json = generate_base_message(message)

  unless message.to_email_address.nil? || message.to_email_address.empty?
    message_json.to_email_address = populate_email_list(message.to_email_address)
  end

  unless message.cc_email_address.nil? || message.cc_email_address.empty?
    message_json.cc_email_address = populate_email_list(message.cc_email_address)
  end

  unless message.bcc_email_address.nil? || message.bcc_email_address.empty?
    message_json.bcc_email_address = populate_email_list(message.bcc_email_address)
  end

  messages_json = []
  messages_json.push(message_json)

  InjectionRequest.new(@server_id, @api_key, messages_json)

end
generate_bulk_message_request(message) click to toggle source

Generate the InjectionRequest for sending to the Injection Api. @param [BulkMessage] message: the bulk message object to convert @return [InjectionRequest] the converted InjectionRequest

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 210
def generate_bulk_message_request(message)

  message_json = generate_base_message(message)
  message_json.to_email_address.push(AddressJson.new("%%DeliveryAddress%%", "%%RecipientName%%"))
  message_json.merge_data = populate_merge_data(message.global_merge_data, message.to_recipient)

  messages_json = []
  messages_json.push(message_json)

  InjectionRequest.new(@server_id, @api_key, messages_json)

end
generate_merge_field_list(merge_data) click to toggle source

Converts an Array of merge data into a List of MergeFieldJson objects. @param [Array] merge_data: Array to convert @return [Array] the converted list of MergeFieldJson

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 278
def generate_merge_field_list(merge_data)

  merge_field_json = Array.new

  merge_data.each do |item|

    if item.instance_of? MergeData
      json = MergeFieldJson.new(item.key, item.value)
      merge_field_json.push(json)

    elsif item.instance_of? Hash
      json = MergeFieldJson.new(item[:key], item[:value])
      merge_field_json.push(json)
    end
  end

  merge_field_json

end
populate_attachments(attachments) click to toggle source

Converts a list of Attachment objects to a List of AttachmentJson objects. @param [Attachment] attachments: list of Attachment to convert @return [Array] the converted list of AttachmentJson

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 109
def populate_attachments(attachments)

  if attachments.nil? || attachments.empty?
    nil
  end

  attachments_json = Array.new

  attachments.each do |item|
    if item.instance_of? Attachment

      at_json = AttachmentJson.new
      at_json.name = item.name
      at_json.name = item.name
      at_json.mime_type = item.mime_type
      at_json.content_id = item.content_id
      at_json.content = item.content
      at_json.custom_headers = populate_custom_headers(item.custom_headers)

      attachments_json.push(at_json)
    end
  end

  attachments_json

end
populate_custom_headers(custom_headers) click to toggle source

Converts a list of Attachment objects to a List of AttachmentJson objects. @param [Array] custom_headers: list of CustomHeader to convert @return [Array] the converted list of CustomHeaderJson

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 86
def populate_custom_headers(custom_headers)

  if custom_headers.nil? || custom_headers.empty?
    nil
  end

  headers_json = Array.new

  custom_headers.each do |header|
    if header.instance_of? CustomHeader
      headers_json.push(CustomHeaderJson.new(header.name, header.value))
    elsif header.instance_of? Hash
      headers_json.push(CustomHeaderJson.new(header[:name], header[:value]))
    end
  end

  headers_json

end
populate_email_list(addresses) click to toggle source

Converts a List of EmailAddress objects to a List of AddressJson objects. @param [EmailAddress] addresses: list of EmailAddress to convert @return [Array] of AddressJson - the converted list of AddressJson

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 166
def populate_email_list(addresses)

  if addresses.nil? || addresses.empty?
    nil
  end

  addresses_json = Array.new

  unless addresses.nil? || addresses.empty?
    addresses.each do |email|
      if email.instance_of? EmailAddress
        addresses_json.push(email_address_to_address_json(email))

      elsif email.instance_of? String
        str_email = EmailAddress.new(email)
        addresses_json.push(email_address_to_address_json(str_email))

      elsif email.instance_of? Hash
        hash_email = EmailAddress.new(email[:email_address], email[:friendly_name])
        addresses_json.push(email_address_to_address_json(hash_email))
      end
    end
  end

  addresses_json

end
populate_merge_data(global_md, recipients) click to toggle source

Populates global merge data and per message merge data to a MergeDataJson. @param [Array] global_md: array of global merge data to convert @param [Array] recipients: list of BulkRecipients to convert @return [MergeDataJson] the converted MergeDataJson object

# File lib/socketlabs/injectionapi/core/injection_request_factory.rb, line 240
def populate_merge_data(global_md, recipients)

  per_message_mf = Array.new
  global_mf = generate_merge_field_list(global_md)

  unless recipients.nil? || recipients.empty?
    recipients.each do |email|

      if email.instance_of? BulkRecipient
        recipient = email

      elsif email.instance_of? String
        recipient = BulkRecipient.new(email)

      elsif email.instance_of? Hash
        recipient = BulkRecipient.new(email[:email_address], { :friendly_name => email[:friendly_name], :merge_data => email[:merge_data] })

      end

      merge_field_json = generate_merge_field_list(recipient.merge_data)
      merge_field_json.push(MergeFieldJson.new("DeliveryAddress", recipient.email_address))

      unless recipient.friendly_name.nil? || recipient.friendly_name.empty?
        merge_field_json.push(MergeFieldJson.new("RecipientName", recipient.friendly_name))
      end

      per_message_mf.push(merge_field_json)

    end
  end

  MergeDataJson.new(per_message_mf, global_mf)

end