Skip to content

Instantly share code, notes, and snippets.

@henrik
Created January 11, 2012 14:59

Revisions

  1. henrik created this gist Jan 11, 2012.
    24 changes: 24 additions & 0 deletions example_output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    --------------------------------------------------

    lib/foo.rb:22:in `save'

    >> REQUEST:
    {
    "CashBook_Book": {
    "cashBookHandle": {
    "Number": "14"
    }
    }
    }

    << RESPONSE:
    {
    "CashBook_BookResponse": {
    "xmlns": "http://e-conomic.com",
    "CashBook_BookResult": {
    "Number": "14"
    }
    }
    }

    --------------------------------------------------
    37 changes: 37 additions & 0 deletions savon.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # config/initializers/savon.rb

    # Don't mention the adapter!
    HTTPI.log = false

    # Log pretty JSON instead of XML, and never mind URL and headers.
    class Savon::SOAP::Request
    def log_request(url, headers, body)
    Savon.log ""
    log_calling_line
    Savon.log ""
    Savon.log ">> REQUEST:"
    log_soap_prettily body
    end

    def log_response(code, body)
    Savon.log ""
    Savon.log "<< RESPONSE:"
    log_soap_prettily body
    Savon.log ""
    Savon.log "-"*50
    end

    def log_calling_line
    calling_line = caller[1..-1].find { |line| line.include?(Rails.root.to_s) }
    calling_line.sub!(Rails.root.to_s + "/", "")
    Savon.log calling_line
    end

    def log_soap_prettily(soap)
    # depends on json gem
    hash = Hash.from_xml(soap)
    body = hash["Envelope"]["Body"]
    json = JSON.pretty_generate(body)
    Savon.log json
    end
    end