Module: BetterOpener

Extended by:
BetterOpener
Included in:
BetterOpener
Defined in:
lib/better_opener.rb,
lib/better_opener/server.rb,
lib/better_opener/railtie.rb,
lib/better_opener/version.rb,
lib/better_opener/delivery_method.rb

Defined Under Namespace

Classes: DeliveryMethod, Notification, Railtie, Server

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#add_delayed_notification(category, klass, method, params) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/better_opener.rb', line 60

def add_delayed_notification(category, klass, method, params)
  db

  n = BetterOpener::Notification.new(:category => category,
                                     :subject => method,
                                     :klass => klass.name,
                                     :method => method,
                                     :params => BetterOpener.encode(params),
                                     :created_at => Time.now)
  n.save
end

#add_notification(category, subject, body) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/better_opener.rb', line 50

def add_notification(category, subject, body)
  db
  n = BetterOpener::Notification.new(:category => category,
                                     :subject => subject,
                                     :body => body,
                                     :created_at => Time.now,
                                     :rendered => true)
  n.save
end

#dbObject



34
35
36
37
38
39
# File 'lib/better_opener.rb', line 34

def db
  return @db if @db
  self.db = DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
  DataMapper.auto_upgrade!
  self.db
end

#db=(server) ⇒ Object



30
31
32
# File 'lib/better_opener.rb', line 30

def db=(server)
  @db = server
end

#decode(params) ⇒ Object



109
110
111
# File 'lib/better_opener.rb', line 109

def decode(params)
  MultiJson.decode(params)
end

#email_templateObject



76
77
78
# File 'lib/better_opener.rb', line 76

def email_template
  Tilt.new(email_template_path)
end

#email_template_pathObject



72
73
74
# File 'lib/better_opener.rb', line 72

def email_template_path
  File.expand_path('../better_opener/email.html.erb', __FILE__)
end

#encode(params) ⇒ Object



105
106
107
# File 'lib/better_opener.rb', line 105

def encode(params)
  MultiJson.encode(params)
end

#get_all_notificationsObject



41
42
43
# File 'lib/better_opener.rb', line 41

def get_all_notifications
  db && BetterOpener::Notification.all(:order => [:id.desc])
end

#get_notification(id) ⇒ Object



45
46
47
# File 'lib/better_opener.rb', line 45

def get_notification(id)
  db && BetterOpener::Notification.get(id)
end

#render_email(name, mail, format = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/better_opener.rb', line 80

def render_email(name, mail, format = nil)
  body_part = mail

  if mail.multipart?
    content_type = Rack::Mime.mime_type(format)
    body_part = mail.parts.find { |part| part.content_type.match(content_type) } || mail.parts.first
  end

  email_template.render(Object.new, :name => name, :mail => mail, :body_part => body_part)
end

#render_notification(n, format = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/better_opener.rb', line 113

def render_notification(n, format = nil)
  return n.body if n.rendered 
  klass = Kernel.const_get(n.klass.classify)
  if n.category == "email"
    m = klass.send(n.method, *BetterOpener.decode(n.params))
    BetterOpener.render_email(n.id, m, format)
  elsif n.category == "sms"
    m = klass.send(n.method, *BetterOpener.decode(n.params))
    BetterOpener.render_sms(n.id, m)
  end
end

#render_sms(name, sms, format = nil) ⇒ Object



100
101
102
# File 'lib/better_opener.rb', line 100

def render_sms(name, sms, format = nil)
  sms_template.render(Object.new, :name => name, :sms => sms)
end

#sms_templateObject



96
97
98
# File 'lib/better_opener.rb', line 96

def sms_template
  Tilt.new(sms_template_path)
end

#sms_template_pathObject



92
93
94
# File 'lib/better_opener.rb', line 92

def sms_template_path
  File.expand_path('../better_opener/sms.html.erb', __FILE__)
end