Class: Brightbytes::Sendgrid::SmtpApiHeader

Inherits:
Object
  • Object
show all
Includes:
SubstPattern
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
]

Constants included from SubstPattern

Brightbytes::Sendgrid::SubstPattern::DEFAULT_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_data = nil) ⇒ SmtpApiHeader

Returns a new instance of SmtpApiHeader.



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

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

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

Instance Method Details

#add_categories(*categories) ⇒ Object



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

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

#add_recipients(*recipients) ⇒ Object



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

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

#add_substitute(key, values) ⇒ Object



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

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



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

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

#categories(*categories) ⇒ Object

getter/setter



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

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

#disable(*filters) ⇒ Object



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

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

#enable(*filters) ⇒ Object



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

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

#filter_setting(filter, setting, value) ⇒ Object



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

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

#ganalytics_options(options = {}) ⇒ Object



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

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



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

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

#section(key, value) ⇒ Object



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

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

#substitute(key, values) ⇒ Object



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

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

#to_jsonObject



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

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

#unique_args(value) ⇒ Object



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

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