Class: ApiGuides::Section

Inherits:
Object
  • Object
show all
Defined in:
lib/api_guides/section.rb

Overview

This class is simply a structure to hold the docs and the examples.

Sections require docs, but do not require any examples. Set examples when you want those on the right side of the docs.

You never interact with this class directly.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Section

Returns a new instance of Section.



15
16
17
18
19
# File 'lib/api_guides/section.rb', line 15

def initialize(attributes = {})
  attributes.each_pair do |attr, value|
    send "#{attr}=", value
  end
end

Instance Attribute Details

#docsObject

Returns the value of attribute docs.



13
14
15
# File 'lib/api_guides/section.rb', line 13

def docs
  @docs
end

#examplesObject

Returns the value of attribute examples.



13
14
15
# File 'lib/api_guides/section.rb', line 13

def examples
  @examples
end

#referenceObject

Returns the value of attribute reference.



13
14
15
# File 'lib/api_guides/section.rb', line 13

def reference
  @reference
end

#titleObject

Returns the value of attribute title.



13
14
15
# File 'lib/api_guides/section.rb', line 13

def title
  @title
end

Class Method Details

.from_xml(xml) ⇒ Object

Takes an XML representation and parse it into a section instance.

Here is XML format expected:

<section title="Foo">
  <docs>
    <![CDATA[
    Insert your markdown here
    ]]>
  </docs>
  <reference title="Bar">
    <![CDATA[
    Insert your markdown here
    ]]>
  </reference>
  <examples>
    <example language="ruby">
      <![CDATA[
      Insert your markdown here>
      ]]>
    </example>
  </example>
</section>

It also loops instantiates the Reference and examples if they are given using their ‘from_xml` methods as well



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/api_guides/section.rb', line 49

def self.from_xml(xml)
  doc = Nokogiri::XML.parse(xml).at_xpath('//section')
  section = Section.new :title => doc.attributes['title'].try(:value)
  section.docs = doc.at_xpath('./docs').content if doc.at_xpath('./docs')

  if reference_xml = doc.at_xpath('./reference')
    section.reference = Reference.from_xml(reference_xml.to_s)
  end

  section.examples = doc.xpath('//example').map do |example_xml|
    Example.from_xml example_xml.to_s
  end

  section
end