Class: HTTParty::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/httparty/patch.rb

Overview

In order to parse the HTML encoded documents returned by the YourMembership API we need to HTML decode the <![CDATA[ tags. This is a bug workaround.

Instance Method Summary collapse

Instance Method Details

#encode_body(body) ⇒ Object

This is the encode_body method from HTTParty’s Request Class adding an additional method call to fix the CDATA elements that are improperly formatted in the YourMembership API’s XML. This is done here to ensure that the fix is in place before the data is parsed.



11
12
13
14
15
16
17
18
# File 'lib/httparty/patch.rb', line 11

def encode_body(body)
  body = fix_cdata body
  if ''.respond_to?(:encoding)
    _encode_body(body)
  else
    body
  end
end

#fix_cdata(body) ⇒ String

Bug Fix for HTML encoded < and > in XML body.

Parameters:

  • body (String)

    an XML document that needs to be checked for this specific issue.

Returns:

  • (String)

    If the HTML encoding issue is found it is repaired and the document is returned.



23
24
25
26
27
28
29
30
31
# File 'lib/httparty/patch.rb', line 23

def fix_cdata(body)
  # <![CDATA[ = &lt;![CDATA[
  # ]]> =  ]]&gt;
  if body.include? '&lt;![CDATA['
    body.gsub! '&lt;![CDATA[', '<![CDATA['
    body.gsub! ']]&gt', ']]>'
  end
  body
end