Skip to content

Instantly share code, notes, and snippets.

@felipero
Created July 15, 2011 07:08
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 felipero/1084227 to your computer and use it in GitHub Desktop.
Save felipero/1084227 to your computer and use it in GitHub Desktop.
Bad Single Responsibility Principle Example
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