Class: Brightbytes::Sendgrid::SmtpApiHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/brightbytes/sendgrid/smtp_api_header.rb

Constant Summary collapse

DELEGATE_METHODS =
[
  :substitute,
  :add_substitute,
  :section, 
  :unique_args,
  :categories, 
  :add_categories,
  :recipients,
  :add_recipients,
  :filter_setting, 
  :enable, 
  :disable, 
  :ganalytics_options,
  :bcc
]
VALID_FILTERS =
[
  :bcc,
  :dkim,
  :domainkeys,
  :forwardspam,
  :opentrack,
  :clicktrack,
  :ganalytics,
  :gravatar,
  :subscriptiontrack,
  :template,
  :footer,
  :spamcheck,
  :bypass_list_management
]
VALID_GANALYTICS_OPTIONS =
[
  :utm_source,
  :utm_medium,
  :utm_campaign,
  :utm_term,
  :utm_content
]
SUBST_PATTERN =
'{{\1}}'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_data = nil) ⇒ SmtpApiHeader

Returns a new instance of SmtpApiHeader.



51
52
53
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 51

def initialize(default_data = nil)
  @data = default_data.instance_of?(Hash) ? default_data.dup : Hash.new { |h,k| h[k] = Hash.new(&h.default_proc) }
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



49
50
51
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 49

def data
  @data
end

Instance Method Details

#add_categories(*categories) ⇒ Object



78
79
80
81
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 78

def add_categories(*categories)
  init_array_key :category
  @data[:category] |= categories.flatten.map(&:to_sym)
end

#add_recipients(*recipients) ⇒ Object



89
90
91
92
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 89

def add_recipients(*recipients)
  init_array_key :to
  @data[:to] += recipients.flatten
end

#add_substitute(key, values) ⇒ Object



59
60
61
62
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 59

def add_substitute(key, values)
  @data[:sub][key_to_tag(key)] = [] unless @data[:sub][key_to_tag(key)].instance_of?(Array)
  @data[:sub][key_to_tag(key)] += Array.wrap(values)
end

#bcc(email) ⇒ Object



115
116
117
118
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 115

def bcc(email)
  filter_setting :bcc, :email, email
  enable :bcc
end

#categories(*categories) ⇒ Object

getter/setter



73
74
75
76
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 73

def categories(*categories)
  @data[:category] = categories.flatten.map(&:to_sym) if categories.flatten.present?
  @data.fetch(:category, [])
end

#disable(*filters) ⇒ Object



103
104
105
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 103

def disable(*filters)
  filters.flatten.each { |filter| filter_setting filter, :enabled, 0 }
end

#enable(*filters) ⇒ Object



99
100
101
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 99

def enable(*filters)
  filters.flatten.each { |filter| filter_setting filter, :enabled, 1 }
end

#filter_setting(filter, setting, value) ⇒ Object



94
95
96
97
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 94

def filter_setting(filter, setting, value)
  return unless VALID_FILTERS.include? filter
  @data[:filters][filter][:settings][setting] = value
end

#ganalytics_options(options = {}) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 107

def ganalytics_options(options = {})
  options.reject! { |k,v| !VALID_GANALYTICS_OPTIONS.include?(k) }
  if options.present?
    options.each { |setting, value| filter_setting :ganalytics, setting, value }
    enable :ganalytics
  end
end

#recipients(*recipients) ⇒ Object

getter/setter



84
85
86
87
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 84

def recipients(*recipients)
  @data[:to] = recipients.flatten if recipients.flatten.present?
  @data.fetch(:to, [])
end

#section(key, value) ⇒ Object



64
65
66
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 64

def section(key, value)
  @data[:section][key_to_tag(key)] = value.to_s
end

#substitute(key, values) ⇒ Object



55
56
57
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 55

def substitute(key, values)
  @data[:sub][key_to_tag(key)] = Array.wrap(values)
end

#to_jsonObject



120
121
122
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 120

def to_json
  JSON.generate(@data, {indent: '', space: ' ', space_before: '', object_nl: '', array_nl: ''})
end

#unique_args(value) ⇒ Object



68
69
70
# File 'lib/brightbytes/sendgrid/smtp_api_header.rb', line 68

def unique_args(value)
  @data[:unique_args] = value if value.instance_of?(Hash)
end