Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created July 15, 2011 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save juanplopes/1084911 to your computer and use it in GitHub Desktop.
Save juanplopes/1084911 to your computer and use it in GitHub Desktop.
Sugestão de refactoring
# adapter = ConnectionAdapter.new(connection
# request = Request.new(registration, card)
# transaction = request.execute_at!(adapter)
module Integration
class Transaction
def initialize(data)
@data = data
end
end
class Request
def initialize(registration, card)
@message = xml_builder("requisicao-transacao") do |xml|
xml.numero registration.id
xml.valor registration.training_class.price.to_i * 100
xml.moeda "986"
# ... MONTA UM XML COMPLEXO ...
end
end
def execute_at!(connection)
data = connection.request! @message
return Transaction.new(data)
end
end
class ConnectionAdapter
def initialize(connection, response_parser = XmlResponseParser.new)
@connection = connection
@parser = response_parser
end
def request!(message)
params = { mensagem: message.target! }
# ... FAZ MAIS COISAS AQUI ...
response = @connection.request! params
throw_if_needed(response)
return @parser.parse(response.body)
end
private
def throw_if_needed(response)
if not reponse == Net::HTTPSuccess
raise "Impossível contactar o servidor"
end
end
end
class XmlResponseParser
def parse(response)
document = REXML::Document.new(response.body)
return parse_elements(document.elements)
end
private
def parse_elements(elements)
map={}
elements.each do |element|
element_map = {}
# ... FAZ ALGO COM OS ELEMENTOS DO XML ...
map.merge!(element.name => element_map)
end
return map.symbolize_keys
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment