Skip to content

Instantly share code, notes, and snippets.

@watson
Created March 22, 2010 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watson/340621 to your computer and use it in GitHub Desktop.
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
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
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
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
# sending outgoing message
OutgoingMessage.unsend.each &:send_message
# 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