Class: MailsocioQA::TestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/mailsocio_qa/test_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ TestClient

Returns a new instance of TestClient.



8
9
10
# File 'lib/mailsocio_qa/test_client.rb', line 8

def initialize(settings)
  self.settings = settings
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



5
6
7
# File 'lib/mailsocio_qa/test_client.rb', line 5

def 
  @account_id
end

#custom_tracking_domainObject

Returns the value of attribute custom_tracking_domain.



5
6
7
# File 'lib/mailsocio_qa/test_client.rb', line 5

def custom_tracking_domain
  @custom_tracking_domain
end

#settingsObject

Returns the value of attribute settings.



6
7
8
# File 'lib/mailsocio_qa/test_client.rb', line 6

def settings
  @settings
end

Instance Method Details

#accounts_url(account_id = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/mailsocio_qa/test_client.rb', line 76

def accounts_url( = nil)
  if .blank?
    app_url('/api/accounts')
  else
    app_url("/api/accounts/#{}")
  end
end

#app_url(path) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/mailsocio_qa/test_client.rb', line 88

def app_url(path)
  if custom_tracking_domain.blank?
    "http://app.#{settings.mailsocio.base_domain}#{path}"
  else
    "http://#{self.custom_tracking_domain}#{path}"
  end
end

#clean_messages(user = 'john') ⇒ Object



63
64
65
# File 'lib/mailsocio_qa/test_client.rb', line 63

def clean_messages(user = 'john')
  retriever(user).delete_all
end

#create_account(attrs = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/mailsocio_qa/test_client.rb', line 12

def (attrs = {})
  attrs.reverse_merge!(api_key: settings.mailsocio.api_key)
  response = HTTPI.post(accounts_url, { account: attrs}.to_query)
  raise "Could not create account:\n#{response.body}" if response.error?

  JSON.parse(response.body).tap do |json|
    self. = json['id']
  end
end

#deliver(fields = {}, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mailsocio_qa/test_client.rb', line 36

def deliver(fields = {}, &block)
  config = settings.mailsocio.mail
  fields.reverse_merge!(
    'X-Account-Id' => ,
    'X-Api-Key' => settings.mailsocio.api_key
  )
  fields.reverse_merge!(config.message)

  Mail::Message.new(fields, &block).tap do |mail|
    mail.subject ||= 'Good news, everyone!'
    delivery = config.delivery
    mail.delivery_method(delivery.method.to_sym, delivery.settings.symbolize_keys)
    mail.deliver
  end
end

#newsletter_statistics(newsletter_id) ⇒ Object



29
30
31
32
33
34
# File 'lib/mailsocio_qa/test_client.rb', line 29

def newsletter_statistics(newsletter_id)
  response = HTTPI.get(newsletter_statistics_url(newsletter_id))
  raise "Could not get newsletter statistics:\n#{response.body}" if response.error?

  JSON.parse(response.body)
end

#newsletter_statistics_url(newsletter_id) ⇒ Object



84
85
86
# File 'lib/mailsocio_qa/test_client.rb', line 84

def newsletter_statistics_url(newsletter_id)
  app_url("/api/accounts/#{}/newsletters/#{newsletter_id}/statistics")
end

#read_accountObject



22
23
24
25
26
27
# File 'lib/mailsocio_qa/test_client.rb', line 22

def 
  response = HTTPI.get(accounts_url(self.))
  raise "Could not read account:\n#{response.body}" if response.error?

  JSON.parse(response.body)
end

#read_message(user = 'john') ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/mailsocio_qa/test_client.rb', line 52

def read_message(user = 'john')
  10.times do
    message = retriever(user).first(:delete_after_find => true)
    return message if message.present?

    sleep 10
  end

  raise 'Could not read message'
end

#retriever(user) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/mailsocio_qa/test_client.rb', line 67

def retriever(user)
  address = settings.trap.addresses[user]
  server = settings.trap.server
  settings = server.settings.symbolize_keys
  settings.merge!(user_name: address)

  Mail::Configuration.instance.retriever_method(server.method.to_sym, settings)
end