Class: Emarsys::Broadcast::API

Inherits:
Object
  • Object
show all
Defined in:
lib/emarsys/broadcast/api.rb

Instance Method Summary collapse

Constructor Details

#initializeAPI

Returns a new instance of API.



5
6
7
8
9
10
# File 'lib/emarsys/broadcast/api.rb', line 5

def initialize
  @config = Emarsys::Broadcast.configuration
  @sftp = SFTP.new @config
  @http = HTTP.new @config
  @xml_builder = XmlBuilder.new
end

Instance Method Details

#create_batch(batch) ⇒ Object



21
22
23
24
25
26
# File 'lib/emarsys/broadcast/api.rb', line 21

def create_batch(batch)
  emarsys_sender = get_sender(batch.sender)
  batch.sender_id = emarsys_sender.id
  batch_xml = BatchXmlBuilder.new.build(batch)
  @http.post("#{@config.api_base_path}/batches/#{batch.name}", batch_xml)
end

#create_sender(sender) ⇒ Object



48
49
50
51
# File 'lib/emarsys/broadcast/api.rb', line 48

def create_sender(sender)
  sender_xml = @xml_builder.sender_xml(sender)
  @http.put("#{@config.api_base_path}/senders/#{sender.id}", sender_xml)
end

#get_sender(email) ⇒ Object



44
45
46
# File 'lib/emarsys/broadcast/api.rb', line 44

def get_sender(email)
  get_senders.find{|s| s.address == email}
end

#get_sendersObject



37
38
39
40
41
42
# File 'lib/emarsys/broadcast/api.rb', line 37

def get_senders
  response = @http.get("#{@config.api_base_path}/senders")
  Nokogiri::XML(response).xpath('//sender').map do |node|
    Sender.new(node.attr('id'), node.xpath('name').text, node.xpath('address').text)
  end
end

#send_batch(batch) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/emarsys/broadcast/api.rb', line 12

def send_batch(batch)
  batch = supplement_batch_from_config(batch)
  validate_batch(batch)
  validate_sender(batch.sender)
  create_batch(batch)
  upload_recipients(batch.recipients_path)
  trigger_import(batch)
end

#sender_exists?(email) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/emarsys/broadcast/api.rb', line 53

def sender_exists?(email)
  get_senders.any?{|s|s.address == email}
end

#trigger_import(batch) ⇒ Object



32
33
34
35
# File 'lib/emarsys/broadcast/api.rb', line 32

def trigger_import(batch)
  import_xml = XmlBuilder.new.import_xml(File.basename(batch.recipients_path))
  @http.post("#{@config.api_base_path}/batches/#{batch.name}/import", import_xml)
end

#upload_recipients(recipients_path) ⇒ Object



28
29
30
# File 'lib/emarsys/broadcast/api.rb', line 28

def upload_recipients(recipients_path)
  @sftp.upload_file(recipients_path, File.basename(recipients_path))
end