Class: WADL::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/wadl/generator.rb

Constant Summary collapse

VERSION =
'0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, options = {}) ⇒ Generator

Returns a new instance of Generator.



9
10
11
# File 'lib/wadl/generator.rb', line 9

def initialize(description, options = {})
  @description, @options, @wadl = description, options, ''
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/wadl/generator.rb', line 7

def description
  @description
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/wadl/generator.rb', line 7

def options
  @options
end

#wadlObject (readonly)

Returns the value of attribute wadl.



7
8
9
# File 'lib/wadl/generator.rb', line 7

def wadl
  @wadl
end

Instance Method Details

#generateObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wadl/generator.rb', line 18

def generate
  xml = Builder::XmlMarkup.new :indent => 2, :target => @wadl
  xml.instruct!

  namespaces = {
    'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
    'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
    'xsi:schemaLocation' => 'http://wadl.dev.java.net/2009/02',
    'xmlns' => 'http://wadl.dev.java.net/2009/02',
  }
  
  if options[:apigee]
    namespaces['xmlns:apigee'] = 'http://api.apigee.com/wadl/2010/07/'
    namespaces['xsi:schemaLocation'] << ' http://apigee.com/schemas/wadl-schema.xsd http://api.apigee.com/wadl/2010/07/ http://apigee.com/schemas/apigee-wadl-extensions.xsd'
  end

  xml.application(namespaces) do
    xml.resources(:base => description[:base]) do
      description[:resources].each do |name, config|
        # index
        if config[:index] && config[:index_formats].present?
          xml.resource(:path => "#{name.pluralize.underscore}.{format}") do
            xml.param(:name => 'format', :type => 'xsd:string', :style => 'template',
                :required => true, :default => config[:index_formats].keys.first) do
              config[:index_formats].each do |format, mime_type|
                xml.option(:value => format, :mediaType => mime_type)
              end
            end

            xml.method('id' => name.pluralize.underscore, 'name' => 'GET') do
              if options[:apigee]
                xml.tag!('apigee:tags') { xml.tag!('apigee:tag', name.singularize.camelcase, :primary => true) }
                xml.tag!('apigee:authentication', :required => false)
                xml.tag!('apigee:example', :url => "/#{name.pluralize.underscore}.#{config[:index_formats].keys.first}")
              end
            end
          end
        end

        # show
        if config[:show] && config[:show_formats].present?
          xml.resource(:path => "#{name.pluralize.underscore}/{id}.{format}") do
            xml.param(:name => 'format', :type => 'xsd:string', :style => 'template',
                :required => true, :default => config[:show_formats].keys.first) do
              config[:show_formats].each do |format, mime_type|
                xml.option(:value => format, :mediaType => mime_type)
              end
            end
            xml.param(:name => 'id', :type => 'xsd:string', :style => 'query', :required => true)

            xml.method('id' => name.singularize.underscore, 'name' => 'GET') do
              if options[:apigee]
                xml.tag!('apigee:tags') { xml.tag!('apigee:tag', name.singularize.camelcase, :primary => true) }
                xml.tag!('apigee:authentication', :required => false)
                xml.tag!('apigee:example', :url => "/#{name.pluralize.underscore}/#{config[:example_id]}.#{config[:show_formats].keys.first}")
              end
            end
          end
        end
      end
    end
  end    

  self
end

#to_textObject



13
14
15
16
# File 'lib/wadl/generator.rb', line 13

def to_text
  generate
  @wadl
end