angus-sdoc 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/angus/base.rb ADDED
@@ -0,0 +1,11 @@
1
+ module Angus
2
+ module SDoc
3
+ end
4
+ end
5
+
6
+ require_relative 'definitions/base'
7
+ require_relative 'exceptions/base'
8
+ require_relative 'definitions_reader'
9
+ require_relative 'definitions_utils'
10
+ require_relative 'sdoc/json_formatter'
11
+ require_relative 'sdoc/html_formatter'
@@ -0,0 +1,18 @@
1
+ module Angus
2
+ module SDoc
3
+ module Definitions
4
+ end
5
+ end
6
+ end
7
+
8
+ require_relative 'service'
9
+ require_relative 'proxy_operation'
10
+ require_relative 'representation'
11
+ require_relative 'representation_field'
12
+ require_relative 'request_element'
13
+ require_relative 'response_element'
14
+ require_relative 'uri_element'
15
+ require_relative 'operation'
16
+ require_relative 'message'
17
+ require_relative 'glossary'
18
+ require_relative 'glossary_term'
@@ -0,0 +1,52 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::Glossary
4
+
5
+ # @!attribute [rw] terms
6
+ # @return [GlossaryTerm] The terms of the glossary.
7
+ attr_accessor :terms
8
+
9
+ # Initializes a new Glossary and its terms.
10
+ #
11
+ # @param [Hash<String, Hash>] terms_hash The hash contains GlossaryTerm#short_name as the key
12
+ # and a hash containing GlossaryTerm#long_name and GlossaryTerm#description as a value.
13
+ #
14
+ # @example
15
+ #
16
+ # {'Glossary Term Name' =>
17
+ # {
18
+ # 'long_name' => 'Long name of the glossary term',
19
+ # 'description' => 'Description of the glossary term'
20
+ # }
21
+ # }
22
+ def initialize(terms_hash = {})
23
+ @terms = Angus::SDoc::Definitions::GlossaryTerm.build_from_hash(terms_hash)
24
+ end
25
+
26
+ # Returns a hash with short name as key and the term as the value.
27
+ #
28
+ # @return [Hash<String, GlossaryTerm>]
29
+ def terms_hash
30
+ hash = {}
31
+
32
+ @terms.each do |term|
33
+ hash[term.short_name] = term
34
+ end
35
+
36
+ hash
37
+ end
38
+
39
+ # Returns a hash with long name as key and the term as the value.
40
+ #
41
+ # @return [Hash<String, GlossaryTerm>]
42
+ def terms_hash_with_long_names
43
+ hash = {}
44
+ @terms.each do |term|
45
+ hash[term.long_name] = term
46
+ end
47
+ hash
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ module Angus
2
+ module SDoc
3
+ # @attr [String] short_name The short name of the glossary term.
4
+ # @attr [String] long_name The long name of the glossary term.
5
+ # @attr [String] description The description of the glossary term.
6
+ class Definitions::GlossaryTerm < Struct.new(:short_name, :long_name, :description)
7
+
8
+ class << self
9
+
10
+ # Initialize multiple GlossaryTerm using the information in the hash parameter.
11
+ #
12
+ # @param [Hash] hash The Hash contains GlossaryTerm#short_name
13
+ # as key and other Hash with the other attributes of the GlossaryTerm as a value.
14
+ #
15
+ # @return [Array<GlossaryTerm>] The list of glossary terms.
16
+ def build_from_hash(hash = {})
17
+ hash.map do |sort_name, gt|
18
+ self.new(sort_name, gt['long_name'], gt['description'])
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ # Check if an object is equals to the current instance.
25
+ #
26
+ # @param [GlossaryTerm] other The object to be compared.
27
+ #
28
+ # @return [Boolean] true if all the attributes are equal and false otherwise.
29
+ def == (other)
30
+ other.kind_of?(Definitions::GlossaryTerm) &&
31
+ self.short_name == other.short_name && self.long_name == other.long_name &&
32
+ self.description == other.description
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::Message
4
+
5
+ ERROR_LEVEL = 'error'
6
+ INFO_LEVEL = 'info'
7
+ WARNING_LEVEL = 'warning'
8
+
9
+ # @!attribute [rw] level
10
+ # @return [String] the level of the message. Possible values are the *_LEVEL constants.
11
+ attr_accessor :level
12
+
13
+ # @!attribute [rw] key
14
+ # @return [String] the key of the message. It is a human readable unique identifier.
15
+ # It has to start with Mayus and it has to be camelcase.
16
+ attr_accessor :key
17
+
18
+ # @!attribute [rw] description
19
+ # @return [String] the description of the message.
20
+ attr_accessor :description
21
+
22
+ # @!attribute [rw] status_code
23
+ # @return [String] the HTTP status code of the message.
24
+ attr_accessor :status_code
25
+
26
+ # @!attribute [rw] text
27
+ # @return [String] when set this text overrides all others and is set as the message description.
28
+ attr_accessor :text
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,62 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::Operation
4
+
5
+ # @!attribute [rw] name
6
+ # @return [String] the name of the operation.
7
+ attr_accessor :name
8
+
9
+ # @!attribute [rw] code_name
10
+ # @return [String] the code name of the message. It is a human readable unique identifier.
11
+ # It has to start with Mayus and it has to be camelcase.
12
+ attr_accessor :code_name
13
+
14
+ # @!attribute [rw] description
15
+ # @return [String] the description of the operation.
16
+ attr_accessor :description
17
+
18
+ # @!attribute [rw] path
19
+ # @return [String] the path of the operation.
20
+ attr_accessor :path
21
+
22
+ # @!attribute [rw] method
23
+ # @return [String] the HTTP method of the operation.
24
+ attr_accessor :method
25
+
26
+ # @!attribute [rw] messages
27
+ # @return [Hash<String, Message>] the messages associated with the operation.
28
+ attr_accessor :messages
29
+
30
+ # @!attribute [rw] uri_elements
31
+ # @return [Set<UriElement>] the uri elements of the operation.
32
+ attr_accessor :uri_elements
33
+
34
+ # @!attribute [rw] request_elements
35
+ # @return [Set<RequestElement>] the request elements of the operation.
36
+ attr_accessor :request_elements
37
+
38
+ # @!attribute [rw] response_elements
39
+ # @return [Set<ResponseElement>] the response elements of the operation.
40
+ attr_accessor :response_elements
41
+
42
+ def code_name
43
+ @code_name.split('.')[-1]
44
+ end
45
+
46
+ # Returns the first message that matches the given key and level.
47
+ #
48
+ # @param [String] key The key of the message.
49
+ # @param [String] level The level of the message.
50
+ # Possible values are the *_LEVEL constants from {Message}.
51
+ #
52
+ # @return [Message] the message or nil if no one matches.
53
+ def message(key, level)
54
+ return nil if self.messages.nil?
55
+
56
+ level &&= level.downcase
57
+ self.messages.find { |message| message.key == key && message.level.downcase == level }
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,24 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::ProxyOperation
4
+
5
+ # @!attribute [rw] code_name
6
+ # @return [String] the code name of the message. It is a human readable unique identifier.
7
+ # It has to start with Mayus and it has to be camelcase.
8
+ attr_accessor :code_name
9
+
10
+ # @!attribute [rw] path
11
+ # @return [String] the path of the operation.
12
+ attr_accessor :path
13
+
14
+ # @!attribute [rw] method
15
+ # @return [String] the HTTP method of the operation.
16
+ attr_accessor :method
17
+
18
+ # @!attribute [rw] service_name
19
+ # @return [String] the service name of the operation.
20
+ attr_accessor :service_name
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::Representation
4
+
5
+ # @!attribute [rw] name
6
+ # @return [String] the name of the representation.
7
+ attr_accessor :name
8
+
9
+ # @!attribute [rw] fields
10
+ # @return [Array<RepresentationField>] the fields of the representation.
11
+ attr_accessor :fields
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::RepresentationField
4
+
5
+ # @!attribute [rw] name
6
+ # @return [String] the name of the representation field.
7
+ attr_accessor :name
8
+
9
+ # @!attribute [rw] description
10
+ # @return [String] the description of the representation field.
11
+ attr_accessor :description
12
+
13
+ # @!attribute [rw] required
14
+ # @return [Boolean] indicates if the representation field is required or not.
15
+ attr_accessor :required
16
+
17
+ # @!attribute [rw] type
18
+ # @return [String] the type of the representation field.
19
+ # This attribute is used when the representation is a single object.
20
+ attr_accessor :type
21
+
22
+ # @!attribute [rw] elements_type
23
+ # @return [String] the elements type of the representation field.
24
+ # This attribute is used when the representation is a list of objects.
25
+ attr_accessor :elements_type
26
+
27
+ # @note if the elements_type attribute is different from nil, it means that the
28
+ # representation field is a list of objects. In that case, the type parameter
29
+ # is not considered.
30
+ def initialize(name = nil, description = nil, required = nil, type = nil, elements_type = nil)
31
+ self.name= name
32
+ self.description= description
33
+ self.required= required
34
+ self.type= type
35
+ self.elements_type= elements_type
36
+ end
37
+
38
+ # Check if an object is equals to the current instance.
39
+ #
40
+ # @param [RepresentationField] other The object to be compared.
41
+ #
42
+ # @return [Boolean] true if all the attributes are equal and false otherwise.
43
+ def == (other)
44
+ other.kind_of?(Definitions::RepresentationField) &&
45
+ self.name == other.name && self.description == other.description &&
46
+ self.required == other.required && self.type == other.type && self.elements_type == other.elements_type
47
+ end
48
+
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,61 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::RequestElement
4
+
5
+ # @!attribute [rw] name
6
+ # @return [String] the name of the request element.
7
+ attr_accessor :name
8
+
9
+ # @!attribute [rw] description
10
+ # @return [String] the description of the request element.
11
+ attr_accessor :description
12
+
13
+ # @!attribute [rw] required
14
+ # @return [Boolean] indicates if the request element is required or not.
15
+ attr_accessor :required
16
+
17
+ # @!attribute [rw] type
18
+ # @return [String] the type of the request element.
19
+ attr_accessor :type
20
+
21
+ # @!attribute [rw] constraints
22
+ # @return [Array<String>] the constraints of the request element.
23
+ attr_accessor :constraints
24
+
25
+ # @!attribute [rw] valid_values
26
+ # @return [Array<String>] the valid values of the request element.
27
+ attr_accessor :valid_values
28
+
29
+ # @!attribute [rw] elements_type
30
+ # @return [String] the elements type of the request element.
31
+ attr_accessor :elements_type
32
+
33
+
34
+ def initialize(name = nil, description = nil, required = nil, type = nil,
35
+ constraints = nil, valid_values = nil, elements_type = nil)
36
+ self.name= name if name
37
+ self.description= description if description
38
+ self.required= required if required
39
+ self.type= type if type
40
+ self.constraints= constraints if constraints
41
+ self.valid_values= valid_values if valid_values
42
+ self.elements_type= elements_type if elements_type
43
+ end
44
+
45
+ # Check if an object is equals to the current instance.
46
+ #
47
+ # @param [RequestElement] other The object to be compared.
48
+ #
49
+ # @return [Boolean] true if all the attributes are equal and false otherwise.
50
+ def == (other)
51
+ other.instance_of?(Definitions::RequestElement) &&
52
+ self.name == other.name && self.description == other.description &&
53
+ self.required == other.required && self.type == other.type &&
54
+ self.constraints == other.constraints &&
55
+ self.valid_values == other.valid_values &&
56
+ self.elements_type == other.elements_type
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,41 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::ResponseElement
4
+
5
+ # @!attribute [rw] name
6
+ # @return [String] the name of the response element.
7
+ attr_accessor :name
8
+
9
+ # @!attribute [rw] description
10
+ # @return [String] the description of the response element.
11
+ attr_accessor :description
12
+
13
+ # @!attribute [rw] required
14
+ # @return [Boolean] indicates if the response element is required or not.
15
+ attr_accessor :required
16
+
17
+ # @!attribute [rw] type
18
+ # @return [String] the type of the response element.
19
+ attr_accessor :type
20
+
21
+ # @!attribute [rw] default
22
+ # @return [String] the default value of the response element.
23
+ attr_accessor :default
24
+
25
+ # @!attribute [rw] elements_type
26
+ # @return [String] the elements type of the response element.
27
+ attr_accessor :elements_type
28
+
29
+ def initialize(name = nil, description = nil, required = nil, type = nil, default = nil,
30
+ elements_type = nil)
31
+ @name = name
32
+ @description = description
33
+ @required = required
34
+ @type = type
35
+ @default = default
36
+ @elements_type = elements_type
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,118 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::Service
4
+
5
+ # @!attribute [rw] name
6
+ # @return [String] the name of the sevice.
7
+ attr_accessor :name
8
+
9
+ # @!attribute [rw] code_name
10
+ # @return [String] the service code name is a unique identifier.
11
+ # It has to be human-readable and a valid literal identifier.
12
+ attr_accessor :code_name
13
+
14
+ # @!attribute [rw] version
15
+ # @return [String] the version of the sevice.
16
+ attr_accessor :version
17
+
18
+ # @!attribute [rw] messages
19
+ # @return [Hash<String, Message>] the messages associated with the service.
20
+ attr_accessor :messages
21
+
22
+ # @!attribute [rw] operations
23
+ # @return [Set<Operation>] the operations of the service.
24
+ attr_accessor :operations
25
+
26
+ # @!attribute [rw] proxy_operations
27
+ # @return [Set<ProxyOperation>] the proxy operations of the service.
28
+ attr_accessor :proxy_operations
29
+
30
+ # @!attribute [rw] representations
31
+ # @return [Set<Representation>] the representations of the service.
32
+ attr_accessor :representations
33
+
34
+ # @!attribute [rw] glossary
35
+ # @return [Glossary] the glossary of the service.
36
+ attr_accessor :glossary
37
+
38
+ def initialize
39
+ @messages = {}
40
+ @operations = Set.new
41
+ @representations = Set.new
42
+ @glossary = Angus::SDoc::Definitions::Glossary.new
43
+ end
44
+
45
+ # Returns the message that matches the given key and level.
46
+ #
47
+ # This method searches for messages in all the operations and returns
48
+ # the first message that matches.
49
+ #
50
+ # @param [#to_s] key The key of the message.
51
+ # @param [String] level The level of the message
52
+ # Possible values are the *_LEVEL constants from {Message}
53
+ #
54
+ # @return [Message] the message or nil if no one matches.
55
+ def message(key, level)
56
+ msg = nil
57
+
58
+ @operations.find do |operation|
59
+ msg = operation.message(key.to_s, level)
60
+ end
61
+
62
+ msg
63
+ end
64
+
65
+ # Merge the following definitions:
66
+ # - Operations.
67
+ # - Representations.
68
+ # - Messages.
69
+ #
70
+ # @note This method does not merge glossary terms.
71
+ #
72
+ # @param [Service] other The service to be merged.
73
+ def merge(other)
74
+ self.operations.merge!(other.operations)
75
+ self.representations += other.representations
76
+
77
+ self.messages.merge!(other.messages)
78
+ end
79
+
80
+ # Returns the operation definition that matches with the given operation name.
81
+ #
82
+ # @param operation_code_name The operation code name.
83
+ #
84
+ # @return [Operation] The operation.
85
+ def operation_definition(namespace, operation_code_name)
86
+ @operations[namespace].find { |operation| operation.code_name == operation_code_name }
87
+ end
88
+
89
+ # Returns the proxy operations for a given service.
90
+ # @todo Does it receives the service or the service name?
91
+ # @todo Verify if this method is being called by remote-client and if it receives
92
+ # the service name.
93
+ # @param service The service name.
94
+ # @return [Array<ProxyOperation>] The proxy operations.
95
+ def proxy_operations_for(service)
96
+ self.proxy_operations.select do |op|
97
+ op.service_name == service
98
+ end
99
+ end
100
+
101
+ # Returns a hash with the representations.
102
+ # @example
103
+ # {
104
+ # key => rollback_payment_multi,
105
+ # value => {representation}
106
+ # }
107
+ # @return [Hash<String, Representation>] The representation hash.
108
+ def representations_hash
109
+ hash = {}
110
+ @representations.each do |representation|
111
+ hash[representation.name] = representation
112
+ end
113
+ hash
114
+ end
115
+
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,20 @@
1
+ module Angus
2
+ module SDoc
3
+ class Definitions::UriElement
4
+
5
+ # @!attribute [rw] name
6
+ # @return [String] the name of the uri element.
7
+ attr_accessor :name
8
+
9
+ # @!attribute [rw] description
10
+ # @return [String] the description of the uri element.
11
+ attr_accessor :description
12
+
13
+ def initialize(name = nil, description = nil)
14
+ @name = name
15
+ @description = description
16
+ end
17
+
18
+ end
19
+ end
20
+ end