Created
March 22, 2010 22:35
-
-
Save watson/340621 to your computer and use it in GitHub Desktop.
Hurtig pseudo kode for håndtering af ind- og udgående beskeder fra og til flere forskellige gateways
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Gateway | |
UNWIRE = 'unwire' | |
FOO = 'foo' | |
BAR = 'bar' | |
class << self | |
def all | |
[UNWIRE, FOO, BAR] | |
end | |
def send_message(message) | |
get(message.type).send_message message | |
end | |
def get(type) | |
eval type.to_s.camelcase | |
end | |
end | |
class Base | |
# generic methods used by all gateways - maybe not needed | |
end | |
class Unwire < Base | |
# custom or overridden methods for specific gateways | |
class << self | |
def parse_params(params) | |
{ | |
:type => params[:type] | |
'foo' => params['abc'] | |
'bar' => params['def'] | |
} | |
end | |
def send_message(message) | |
# do the actial sending specific for this gateway | |
end | |
end | |
end | |
# ... other gateways | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IncommingMessage < ActiveRecord::Base | |
validates_presence_of :msgid, :message | |
validates_inclusion_of :gateway, :in => Gateway.all | |
# Attributes: | |
# gateway (e.g. Gateway::UNWIRE) | |
# msgid (tlf.nummer) | |
# body (besked) | |
class < self | |
def create(params, *args) | |
super Gateway.get(params[:type]).parse_params(params), *args | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OutgoingMessage < ActiveRecord::Base | |
validates_presence_of :sender, :recipients, :body | |
validates_inclusion_of :gateway, :in => Gateway.all | |
named_scope :unsend, :conditions => {:send_at => nil} | |
named_scope :send, :conditions => 'send_at IS NOT NULL' | |
serialize :recipients | |
# Attributes: | |
# gateway (e.g. Gateway::UNWIRE) | |
# sender | |
# recipients | |
# body | |
# send_at | |
def send_message | |
if valid? | |
Gateway.send_message self | |
else | |
# handle errors | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sending outgoing message | |
OutgoingMessage.unsend.each &:send_message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# catching and storing incomming message | |
class UnwireController < ActionController::Base | |
def action | |
if IncommingMessage.create(params.merge(:type => Gateway::UNWIRE)) | |
render :text => "SUCCESS" | |
else | |
# handle errors | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment