Class: MailService::Member

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/services/mail_service/member.rb

Direct Known Subclasses

Customer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) {|_self| ... } ⇒ Member

Constructor


Yields:

  • (_self)

Yield Parameters:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/services/mail_service/member.rb', line 74

def initialize(attributes = {})
  defaults = {
    locale: I18n.locale,
    double_optin: true
  }
  
  attributes = defaults.merge(attributes).symbolize_keys

  @email = attributes[:email]
  @locale = attributes[:locale]
  @connection = self.class.parent.connection
  @double_optin = attributes[:double_optin]
  @list_id = attributes[:list_id]
  
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



156
157
158
159
160
161
162
# File 'app/services/mail_service/member.rb', line 156

def method_missing(method_name, *args, &block)
  if info.keys.include?(method_name.to_s)
    info[method_name]
  else
    super
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



40
41
42
# File 'app/services/mail_service/member.rb', line 40

def connection
  @connection
end

#double_optinObject (readonly)

Returns the value of attribute double_optin.



40
41
42
# File 'app/services/mail_service/member.rb', line 40

def double_optin
  @double_optin
end

#emailObject Also known as: name

Attribute Settings




38
39
40
# File 'app/services/mail_service/member.rb', line 38

def email
  @email
end

#list_idObject

Attribute Settings




38
39
40
# File 'app/services/mail_service/member.rb', line 38

def list_id
  @list_id
end

#localeObject

Attribute Settings




38
39
40
# File 'app/services/mail_service/member.rb', line 38

def locale
  @locale
end

#subscriber_idObject (readonly)

Returns the value of attribute subscriber_id.



40
41
42
# File 'app/services/mail_service/member.rb', line 40

def subscriber_id
  @subscriber_id
end

Class Method Details

.create(attributes = {}) ⇒ Object

Class Methods




172
173
174
175
176
# File 'app/services/mail_service/member.rb', line 172

def self.create(attributes = {})
  mailservice = new(attributes)
  mailservice.save
  mailservice
end

.exists?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
192
# File 'app/services/mail_service/member.rb', line 182

def self.exists?(options = {})
  return false if options.blank?

  begin
    new(options.slice(:email, :list_id)).info.present?
  rescue => e  
    Rails.logger.info "**[MailService Error][Exists] #{e.message}"   
    Rails.logger.warn "**[MailService Error][Exists] #{e.backtrace.join("\n")}" if Rails.env.development?   
    e.message
  end
end

.find_by_email(email) ⇒ Object



178
179
180
# File 'app/services/mail_service/member.rb', line 178

def self.find_by_email(email)
  new(new(email: email).info.symbolize_keys)
end

Instance Method Details

#confirmed?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'app/services/mail_service/member.rb', line 152

def confirmed?
  info[:timestamp_opt].present?
end

#destroyObject



140
141
142
143
144
145
146
147
148
149
150
# File 'app/services/mail_service/member.rb', line 140

def destroy
  begin
    connection.lists.unsubscribe(
      id: list_id, 
      email: { email: email }
    )
  rescue => e
    Rails.logger.info "**[MailService Error][Destroy] #{e.message}"
    Rails.logger.warn "**[MailService Error][Destroy] #{e.backtrace.join("\n")}" if Rails.env.development?
  end
end

#infoObject



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/mail_service/member.rb', line 103

def info
  begin
    unless @info
      response = connection.lists.member_info(id: list_id, emails: [{ email: email}]).with_indifferent_access
      @info = response[:data].first if response[:success_count] && response[:success_count] >= 1
    end
  rescue => e
    Rails.logger.info "**[MailService Error][Info] #{e.message}"
    Rails.logger.warn "**[MailService Error][Info] #{e.backtrace.join("\n")}" if Rails.env.development?
  end
  
  @info || {}
end

#listObject

Instance Methods




99
100
101
# File 'app/services/mail_service/member.rb', line 99

def list
  List.find_by_list_id(list_id) if list_id
end

#persisted?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'app/services/mail_service/member.rb', line 117

def persisted?
  false
end

#saveObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/services/mail_service/member.rb', line 121

def save
  return false unless valid?

  return true if Rails.env.test?

  begin
    connection.lists.subscribe(
      id: list_id, 
      email: { email: email },
      double_optin: double_optin,
      merge_vars: { mc_language: locale }
    )
  rescue => e
    Rails.logger.info "**[MailService Error][Save] #{e.message}"
    Rails.logger.warn "**[MailService Error][Save] #{e.backtrace.join("\n")}" if Rails.env.development?
    false
  end
end