-
-
Save diegodfsd/1085735 to your computer and use it in GitHub Desktop.
Bad Single Responsibility Principle Example
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
#params = Parameter.new "registration", "0000000000000000" | |
#transaction = Transaction.create! params, connection | |
module Integration | |
class Transaction | |
attr_reader :data | |
def initialize(data) | |
@data = data | |
end | |
def self.create!(params, connection) | |
response = connection.make_request! params.to_s | |
Transaction.new response | |
end | |
end | |
class Parameter | |
def initialize(registration, card) | |
@registration = registration | |
@card = card | |
end | |
def to_s | |
serialize | |
end | |
private | |
def serialize | |
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 WrapperConnection | |
def initialize(connection = Integration::Connection.new, xml_parser = XmlParser.new) | |
@connection = connection | |
end | |
def make_request!(message) | |
params = { mensagem : message.target! } | |
response = @connection.request! params | |
raise_exception response | |
xml_parser.parse response | |
end | |
def raise_exception(response) | |
unless response == Net::HTTPSuccess | |
raise "Impossível contactar o servidor" | |
end | |
end | |
end | |
class XmlParser | |
def parse(response) | |
document = REXML::Document.new(response.body) | |
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 | |
map.symbolize_keys | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment