Class: MaropostApi::TransactionalCampaigns

Inherits:
Object
  • Object
show all
Defined in:
lib/maropost_api/transactional_campaigns.rb

Instance Method Summary collapse

Constructor Details

#initialize(account = ENV["ACCOUNT"], api_key = ENV["API_KEY"]) ⇒ TransactionalCampaigns

Returns a new instance of TransactionalCampaigns.



4
5
6
7
# File 'lib/maropost_api/transactional_campaigns.rb', line 4

def initialize( = ENV["ACCOUNT"], api_key = ENV["API_KEY"])
  MaropostApi.instance_variable_set(:@api_key, api_key)
  MaropostApi.instance_variable_set(:@account, )
end

Instance Method Details

#create(name:, subject:, preheader:, from_name:, from_email:, reply_to:, content_id:, email_preview_link:, address:, language:, add_ctags: []) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/maropost_api/transactional_campaigns.rb', line 16

def create(name:, subject:, preheader:, from_name:, from_email:, reply_to:, content_id:, email_preview_link:, address:, language:, add_ctags: [])
  params = {}
  method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
  params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
  
  full_path = full_resource_path
  
  MaropostApi.post_result(full_path, :campaign => params)
end

#get(page) ⇒ Object



9
10
11
12
13
14
# File 'lib/maropost_api/transactional_campaigns.rb', line 9

def get(page)
  full_path = full_resource_path
  query_params = MaropostApi.set_query_params({:page => page})
  
  MaropostApi.get_result(full_path, query_params)
end

#send_email(campaign_id:, content: {}, contact: {}, send_time: {}, ignore_dnm: nil, bcc: nil, from_name: nil, from_email: nil, subject: nil, reply_to: nil, address: nil, tags: {}, add_ctags: []) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/maropost_api/transactional_campaigns.rb', line 26

def send_email(
      campaign_id:,
      content: {},
      contact: {},
      send_time: {},
      ignore_dnm: nil,
      bcc: nil,
      from_name: nil,
      from_email: nil,
      subject: nil,
      reply_to: nil,
      address: nil,
      tags: {},
      add_ctags: []
  )
    email_regex = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
    # check for content field values or content id
    raise ArgumentError.new('Content must be a type of Integer (content_id) or a Hash (content field values)') until content.kind_of? Hash or content.kind_of? Integer
    if (content.kind_of? Hash)
      content = content.slice(:name, :html_part, :text_part)
      raise ArgumentError.new('Content field values must have all or some of :name, :html_part and :text_part as keys') until content.empty? || content.count > 0          
    end
    # check for contact field values or contact_id
    raise ArgumentError.new('Contact must be a type of Integer (contact_id) or a Hash (contact field values)') until contact.kind_of? Hash or contact.kind_of? Integer
    if (contact.kind_of? Hash)
      contact = contact.slice(:email, :first_name, :last_name, :custom_field)
      raise ArgumentError.new('Contact field values must have :email and any or all of :first_name and :last_name :custom_field as keys') if contact.empty? || contact[:email].nil?
      raise ArgumentError.new("contact[:custom_field] must be a type of Hash - #{contact[:custom_field].class} given!") until contact[:custom_field].is_a?(Hash) || contact[:custom_field].nil?
    end
    raise ArgumentError.new('contact[:email] must be a valid email address') until contact.is_a? Integer or contact[:email].match? email_regex
    raise ArgumentError.new('bcc must be a valid email address') until bcc.nil? or bcc.match? email_regex
    raise ArgumentError.new('from_email must be a valid email address') until from_email.nil? or from_email.match? email_regex
    raise ArgumentError.new('reply_to must be a valid email address') until reply_to.nil? or reply_to.match? email_regex
    raise ArgumentError.new('add_ctags must be a valid Array of string') until add_ctags.is_a? Array and add_ctags.all?{|t| t.is_a? String }
    
    params = {}
    method(__method__).parameters.each{|p| params[p[1]] = eval(p[1].to_s)}
    params.reject!{|k,v| v.nil? or (v.respond_to? :empty? and v.empty?) }
    params[:content_id] = params.delete(:content) if params[:content].kind_of? Integer
    params[:contact_id] = params.delete(:contact) if params[:contact].kind_of? Integer
    
    full_path = full_resource_path('/deliver', 'emails')
    
    MaropostApi.post_result(full_path, :email => params)
end