Created
July 15, 2011 07:08
-
-
Save felipero/1084227 to your computer and use it in GitHub Desktop.
Bad Single Responsibility Principle Example
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
module Integration | |
class Transaction | |
def initialize | |
@connection = Integration::Connection.new | |
end | |
def create!(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 | |
make_request! message | |
end | |
private | |
def make_request!(message) | |
params = { mensagem: message.target! } | |
# ... FAZ MAIS COISAS AQUI ... | |
result = @connection.request! params | |
parse_response(result) | |
end | |
def parse_response(response) | |
case response | |
when Net::HTTPSuccess | |
document = REXML::Document.new(response.body) | |
parse_elements(document.elements) | |
else | |
{:erro => { :mensagem => "Impossível contactar o servidor"}} | |
end | |
end | |
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 | |
map.symbolize_keys | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment