class Atom::Content

Content class

Public Class Methods

new(params={}) click to toggle source
Calls superclass method Atom::Element::new
# File lib/atomutil.rb, line 753
def initialize(params={})
  super(params)
  #self.body = params[:body] if params.has_key?(:body)
  self.type = params[:type] if params.has_key?(:type)
end

Public Instance Methods

body() click to toggle source
# File lib/atomutil.rb, line 781
def body
  if @body.nil?
    mode = self.type == 'xhtml'       ? 'xml'\
         : self.type =~ %r{[\/+]xml$} ? 'xml'\
         : self.type == 'html'        ? 'escaped'\
         : self.type == 'text'        ? 'escaped'\
         : self.type =~ %r{^text}     ? 'escaped'\
         :                              'base64'
    case(mode)
      when 'xml'
        unless @elem.elements.empty?
          if @elem.elements.size == 1 && @elem.elements[1].name == 'div'
            @body = @elem.elements[1].collect{ |c| c.to_s }.join('')
          else
            @body = @elem.collect{ |c| c.to_s }.join('')
          end
        else
          @body = @elem.text
        end
      when 'escaped'
        @body = @elem.text
      when 'base64'
        text = @elem.text
        @body = text.nil?? nil : text.unpack('m').first
      else
        @body = nil
    end
  end
  @body
end
body=(value) click to toggle source
# File lib/atomutil.rb, line 759
def body=(value)

  if is_utf8?(value)
    copy = "<div xmlns=\"http://www.w3.org/1999/xhtml\">#{value}</div>"
    is_valid = true
    begin
      node = REXML::Document.new(copy).elements[1][0]
    rescue
      is_valid = false
    end
    if is_valid && node.instance_of?(REXML::Element)
      @elem.add_element node
      self.type = 'xhtml'
    else
      @elem.add_text value
      self.type = (value =~ /^\s*</) ? 'html' : 'text'
    end
  else
    @elem.add_text([value].pack('m').chomp)
  end
end

Private Instance Methods

is_utf8?(str) click to toggle source
# File lib/atomutil.rb, line 814
def is_utf8?(str)
  case str.encoding
  when Encoding::UTF_8
    str.valid_encoding?
  when Encoding::ASCII_8BIT, Encoding::US_ASCII
    str.dup.force_encoding(Encoding::UTF_8).valid_encoding?
   else
     false
  end
end