-
-
Save felipero/1084997 to your computer and use it in GitHub Desktop.
Sugestão de refactoring
This file contains hidden or 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
# adapter = ConnectionAdapter.new(connection | |
# request = Request.new(registration, card) | |
# transaction = request.execute_at!(adapter) | |
module Integration | |
class Transaction | |
def initialize(data) | |
@data = data | |
end | |
def self.create!(connection) | |
data = connection.request_with! Request.new | |
return Transaction.new(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 | |
end | |
class ConnectionAdapter | |
def initialize(connection, response_parser = XmlResponseParser.new) | |
@connection = connection | |
@parser = response_parser | |
end | |
def request_with!(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
Só uma melhorada. Não mexi muito pra não descaracterizar seu código. :)