Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 66 lines (53 sloc) 1.457 kB
71e2258 @Sutto First part of WPF
authored
1 require 'dm-core'
2 require 'dm-timestamps'
3 require 'dm-validations'
4 require 'dm-aggregates'
5 require 'dm-serializer'
6
7 class SMS
8 include DataMapper::Resource
9
10 property :id, Serial
11 property :contents, String
12 property :number, String
13 property :status, String
14 property :type, String
15 property :created_at, DateTime
16 property :updated_at, DateTime
17
18 validates_present :contents, :number, :status, :type
19
20 def send_with_modem(modem)
21 contents = self.contents.to_s[0, 160]
22 if modem && modem.send_sms(number, contents)
23 self.update_attributes :status => "sent"
24 return true
25 else
26 return false
27 end
28 end
29
30 def self.setup!
31 DataMapper.setup(:default, "sqlite3:///#{File.expand_path(File.dirname(__FILE__))}/messages.sqlite3")
32 self.auto_upgrade!
33 end
34
35 def self.receive(params = {})
36 sms = SMS.new(params)
37 sms.type = "incoming"
38 sms.status = "received"
39 return sms
40 end
41
42 def self.queue(params = {})
43 params[:number] = params.delete(:from) if params[:from]
44 sms = SMS.new(params)
45 sms.type = "outgoing"
46 sms.status = "pending"
47 return sms
48 end
49
50 def self.send_pending(modem)
51 unsent = []
52 self.queued.each do |pending|
53 unsent << pending unless pending.send_with_modem(modem)
54 end
55 return unsent
56 end
57
58 def self.queued
59 self.all(:conditions => ['type = ? AND status = ?', "outgoing", "pending"])
60 end
61
62 end
63
64 SMS.setup!
65
Something went wrong with that request. Please try again.