skeleton 0.3.3 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/README.md +140 -65
  4. data/Rakefile +10 -3
  5. data/lib/skeleton.rb +6 -10
  6. data/lib/skeleton/contact.rb +1 -13
  7. data/lib/skeleton/error.rb +4 -0
  8. data/lib/skeleton/graph.rb +56 -0
  9. data/lib/skeleton/header.rb +2 -63
  10. data/lib/skeleton/items.rb +34 -0
  11. data/lib/skeleton/license.rb +1 -12
  12. data/lib/skeleton/model.rb +13 -17
  13. data/lib/skeleton/operation.rb +50 -121
  14. data/lib/skeleton/parameter.rb +31 -88
  15. data/lib/skeleton/parameters.rb +40 -0
  16. data/lib/skeleton/path.rb +42 -80
  17. data/lib/skeleton/presenter.rb +19 -0
  18. data/lib/skeleton/property.rb +6 -0
  19. data/lib/skeleton/response.rb +24 -45
  20. data/lib/skeleton/schema.rb +80 -63
  21. data/lib/skeleton/scope.rb +18 -0
  22. data/lib/skeleton/security_scheme.rb +19 -37
  23. data/lib/skeleton/serializers/options.rb +215 -0
  24. data/lib/skeleton/serializers/swagger.rb +197 -0
  25. data/lib/skeleton/structure.rb +92 -138
  26. data/lib/skeleton/swagger.rb +9 -0
  27. data/lib/skeleton/tag.rb +11 -16
  28. data/lib/skeleton/version.rb +1 -1
  29. data/skeleton.gemspec +2 -0
  30. data/test/fixtures/json-schema-draft-04.json +150 -0
  31. data/test/fixtures/schema.json +1482 -0
  32. data/test/integrations/validate_complex_schema_spec.rb +42 -0
  33. data/test/skeleton/graph_test.rb +22 -0
  34. data/test/skeleton/mapper_test.rb +84 -0
  35. data/test/skeleton/operation_test.rb +11 -0
  36. data/test/skeleton/parameter_test.rb +34 -0
  37. data/test/skeleton/parameters_test.rb +9 -0
  38. data/test/skeleton/path_test.rb +46 -0
  39. data/test/skeleton/property_test.rb +8 -0
  40. data/test/skeleton/serializers/options_test.rb +68 -0
  41. data/test/skeleton/serializers/swagger_test.rb +30 -0
  42. data/test/support/factories/structure_factory.rb +86 -0
  43. data/test/support/fixtures.rb +6 -0
  44. data/test/support/kissmetrics/core_api.rb +542 -0
  45. data/{spec/spec_helper.rb → test/test_helper.rb} +7 -1
  46. metadata +73 -25
  47. data/lib/skeleton/config.rb +0 -37
  48. data/lib/skeleton/documentation.rb +0 -17
  49. data/lib/skeleton/example.rb +0 -31
  50. data/lib/skeleton/headers.rb +0 -50
  51. data/lib/skeleton/helpers/controller_helpers.rb +0 -25
  52. data/lib/skeleton/info.rb +0 -40
  53. data/lib/skeleton/item.rb +0 -99
  54. data/lib/skeleton/responses.rb +0 -59
  55. data/lib/skeleton/scopes.rb +0 -24
  56. data/lib/skeleton/security_definitions.rb +0 -46
  57. data/lib/skeleton/security_requirement.rb +0 -29
  58. data/spec/integrations/use_case_spec.rb +0 -131
  59. data/spec/skeleton/operation_spec.rb +0 -113
  60. data/spec/skeleton/serializers/contact_spec.rb +0 -30
  61. data/spec/skeleton/serializers/documentation_spec.rb +0 -23
  62. data/spec/skeleton/serializers/header_spec.rb +0 -57
@@ -0,0 +1,34 @@
1
+ require 'skeleton/schema'
2
+
3
+ module Skeleton
4
+ class Items < Schema
5
+ attr_reader :items, :collection_format
6
+
7
+ def items=(value)
8
+ case value
9
+ when Hash
10
+ @items = Skeleton::Items.new(value)
11
+ else
12
+ @items = value
13
+ end
14
+ end
15
+
16
+ def collection_format
17
+ return nil unless array?
18
+
19
+ if @collection_format.nil?
20
+ 'csv'
21
+ else
22
+ @collection_format
23
+ end
24
+ end
25
+
26
+ def collection_format=(value)
27
+ @collection_format = value.to_s
28
+ end
29
+
30
+ def items?
31
+ !!@items
32
+ end
33
+ end
34
+ end
@@ -1,16 +1,5 @@
1
- require 'skeleton/model'
2
-
3
1
  module Skeleton
4
- class License < Model
2
+ class License
5
3
  attr_accessor :name, :url
6
- attr_presence :name, :url
7
-
8
- def to_h
9
- hash = {}
10
- hash[:name] = name if name?
11
- hash[:url] = url if url?
12
- hash
13
- end
14
- alias_method :to_swagger_hash, :to_h
15
4
  end
16
5
  end
@@ -1,31 +1,27 @@
1
- require 'multi_json'
2
- require 'skeleton/attributes'
1
+ require 'skeleton/schema'
3
2
 
4
3
  module Skeleton
5
- class Model
6
- extend Skeleton::Attributes
4
+ class Model < Schema
5
+ attr_reader :description, :descriptor, :name
7
6
 
8
- def initialize(args={})
9
- args.each do |k, v|
10
- setter = "#{k}="
11
- self.public_send(setter, v) if self.respond_to?(setter)
12
- end
7
+ def describe(value)
8
+ @description = value
13
9
  end
14
10
 
15
- def to_swagger_hash
16
- raise(NotImplementedError)
11
+ def extends(value)
12
+ @descriptor = value
17
13
  end
18
14
 
19
- def to_h
20
- raise(NotImplementedError)
15
+ def required(field, options={})
16
+ property(field, { required: true }.merge(options))
21
17
  end
22
18
 
23
- def to_json
24
- MultiJson.dump(to_h)
19
+ def optional(field, options={})
20
+ property(field, { required: false }.merge(options))
25
21
  end
26
22
 
27
- def to_swagger_json
28
- MultiJson.dump(to_swagger_hash)
23
+ def property(field, options={})
24
+ properties[field] = Skeleton::Schema.new(options)
29
25
  end
30
26
  end
31
27
  end
@@ -1,157 +1,86 @@
1
- require 'skeleton/model'
2
- require 'skeleton/parameter'
3
- require 'skeleton/responses'
1
+ require 'skeleton/response'
2
+ require 'skeleton/parameters'
4
3
 
5
4
  module Skeleton
6
- class Operation < Model
7
- attr_accessor :summary, :description, :external_docs, :operation_id, :deprecated
8
- attr_presence :summary, :description, :external_docs, :operation_id, :deprecated
9
- attr_not_empty :tags, :consumes, :produces, :responses, :schemes, :security, :parameters
5
+ class Operation
6
+ attr_accessor :description, :summary, :id
7
+ attr_reader :responses
10
8
 
11
- attr_writer :responses
12
-
13
- def consumes
14
- @consumes ||= []
9
+ def initialize
10
+ @responses = {}
11
+ @tags = {}
12
+ @consumes = {}
13
+ @produces = {}
14
+ @parameters = Skeleton::Parameters.new
15
15
  end
16
16
 
17
- def consumes=(value)
18
- @consumes = Array(value)
19
- end
20
-
21
- def produces
22
- @produces ||= []
23
- end
17
+ alias_method :summarize, :summary=
18
+ alias_method :describe, :description=
19
+ alias_method :identify, :id=
24
20
 
25
- def produces=(value)
26
- @produces = Array(value)
21
+ def deprecate
22
+ @deprecated = true
27
23
  end
28
24
 
29
- def parameters
30
- @parameters ||= []
25
+ def deprecated?
26
+ !!@deprecated
31
27
  end
32
28
 
33
- def parameters=(value)
34
- @parameters = Array(value)
29
+ def id?
30
+ !!@id
35
31
  end
36
32
 
37
- def schemes
38
- @schemes ||= []
33
+ def tag(*values)
34
+ values.each { |v| @tags[v] = true }
39
35
  end
40
36
 
41
- def schemes=(value)
42
- @schemes = Array(value)
37
+ def tags
38
+ @tags.map { |t,_| t.to_s }
43
39
  end
44
40
 
45
- def security
46
- @security ||= []
41
+ def consume(*values)
42
+ values.each { |t| @consumes[t.to_s] = true }
47
43
  end
48
44
 
49
- def security=(value)
50
- @security = Array(value)
45
+ def consumes
46
+ @consumes.map { |s, _| s }
51
47
  end
52
48
 
53
- def responses
54
- @responses ||= Skeleton::Responses.new
49
+ def consumes?
50
+ !@consumes.empty?
55
51
  end
56
52
 
57
- def tags=(value)
58
- @tags = Array(value)
53
+ def produce(*values)
54
+ values.each { |t| @produces[t.to_s] = true }
59
55
  end
60
56
 
61
- def tags
62
- @tags ||= []
63
- end
64
-
65
- def response(name, args={})
66
- responses.set(name, args)
57
+ def produces
58
+ @produces.map { |s, _| s }
67
59
  end
68
60
 
69
- def tag(value)
70
- tags << value
61
+ def produces?
62
+ !@produces.empty?
71
63
  end
72
64
 
73
- def parameter(location, name, &block)
74
- param = Parameter.new(location: location, name: name)
75
- yield(param) if block
76
- parameters << param
65
+ def response(value, options={}, &block)
66
+ @responses[value] = Skeleton::Response.new
67
+ @responses[value].instance_eval(&block) if block
68
+ @responses[:default] = @responses[value] if options[:default]
69
+ @responses[value]
77
70
  end
78
71
 
79
- def to_h
80
- hash = {}
81
- hash[:tags] = self.tags if self.tags?
82
- hash[:summary] = self.summary if self.summary?
83
- hash[:description] = self.description if self.description?
84
- hash[:external_docs] = self.external_docs if self.external_docs?
85
- hash[:operation_id] = self.operation_id if self.operation_id?
86
- hash[:consumes] = self.consumes if self.consumes?
87
- hash[:produces] = self.produces if self.produces?
88
-
89
- if self.parameters?
90
- hash[:parameters] = []
91
- self.parameters.each do |parameter|
92
- if parameter.respond_to?(:to_h)
93
- hash[:parameters] << parameter.to_h
94
- else
95
- hash[:parameters] = parameter
96
- end
97
- end
72
+ def parameters(location=:query, &block)
73
+ params = Skeleton::Parameters.new
74
+ params.instance_eval(&block) if block
75
+ params.each do |p|
76
+ p.location = location
77
+ @parameters.add(p)
98
78
  end
99
-
100
- hash[:responses] = self.responses.to_h if self.responses?
101
- hash[:schemes] = self.schemes if self.schemes?
102
- hash[:deprecated] = self.deprecated if self.deprecated?
103
- if self.security?
104
- hash[:security] = []
105
- self.security.each do |sec|
106
- if sec.respond_to?(:to_h)
107
- hash[:secuirty] << sec.to_h
108
- else
109
- hash[:security] << sec
110
- end
111
- end
112
- end
113
- hash[:security] = self.deprecated if self.deprecated?
114
- hash
79
+ @parameters
115
80
  end
116
81
 
117
- def to_swagger_hash
118
- hash = {}
119
- hash[:tags] = self.tags if self.tags?
120
- hash[:summary] = self.summary if self.summary?
121
- hash[:description] = self.description if self.description?
122
- hash[:externalDocs] = self.external_docs if self.external_docs?
123
- hash[:operationId] = self.operation_id if self.operation_id?
124
- hash[:consumes] = self.consumes if self.consumes?
125
- hash[:produces] = self.produces if self.produces?
126
-
127
- if self.parameters?
128
- hash[:parameters] = []
129
- self.parameters.each do |parameter|
130
- if parameter.respond_to?(:to_swagger_hash)
131
- hash[:parameters] << parameter.to_swagger_hash
132
- else
133
- hash[:parameters] = parameter
134
- end
135
- end
136
- end
137
-
138
- hash[:responses] = self.responses.to_swagger_hash if self.responses?
139
-
140
- hash[:schemes] = self.schemes if self.schemes?
141
- hash[:deprecated] = self.deprecated if self.deprecated?
142
-
143
- if self.security?
144
- hash[:security] = []
145
- self.security.each do |sec|
146
- if sec.respond_to?(:to_swagger_hash)
147
- hash[:secuirty] << sec.to_swagger_hash
148
- else
149
- hash[:security] << sec
150
- end
151
- end
152
- end
153
- hash[:security] = self.deprecated if self.deprecated?
154
- hash
82
+ def parameters?
83
+ !@parameters.empty?
155
84
  end
156
85
  end
157
86
  end
@@ -1,112 +1,55 @@
1
- require 'skeleton/model'
1
+ require 'skeleton/schema'
2
+ require 'skeleton/items'
2
3
 
3
4
  module Skeleton
4
- class Parameter < Model
5
- attr_accessor :name, :location, :description, :required, :schema, :type,
6
- :format, :items, :collection_format, :default, :maximum,
7
- :exclusive_maximum, :minimum, :exclusive_minimum, :max_length,
8
- :min_length, :unique_items, :multiple_of, :min_items,
9
- :max_items, :pattern
5
+ class Parameter < Schema
6
+ attr_reader :name, :location, :items, :schema
10
7
 
11
- attr_writer :enum
12
- attr_presence :pattern, :multiple_of, :unique_items, :max_items, :min_items,
13
- :min_length, :max_length, :minimum, :maximum, :default,
14
- :collection_format, :items, :format, :type, :schema, :required,
15
- :description, :location, :name, :exclusive_maximum,
16
- :exclusive_minimum, :unique_items
17
-
18
- def enum
19
- @enum ||= []
8
+ def items?
9
+ !!@items
20
10
  end
21
11
 
22
- def enum?
23
- !enum.empty?
12
+ def items=(hash)
13
+ self.type = 'array'
14
+ @items = Skeleton::Items.new(hash)
24
15
  end
16
+ alias_method :array=, :items=
25
17
 
26
- def array?
27
- @type == 'array'
18
+ def schema?
19
+ !!@schema
28
20
  end
29
21
 
30
- def string?
31
- @type == 'string'
22
+ def schema=(value)
23
+ case value
24
+ when Hash
25
+ @schema = Skeleton::Schema.new(value)
26
+ else
27
+ @schema = value
28
+ end
32
29
  end
33
30
 
34
- def number?
35
- @type == 'number'
31
+ def name=(value)
32
+ @name = value.to_s
36
33
  end
37
34
 
38
- def integer?
39
- @type == 'integer'
35
+ def location=(value)
36
+ @location = value.to_s
40
37
  end
41
38
 
42
- def boolean?
43
- @type == 'boolean'
39
+ def body?
40
+ @location == 'body'
44
41
  end
45
42
 
46
- def file?
47
- @type == 'file'
43
+ def query?
44
+ @location == 'query'
48
45
  end
49
46
 
50
- def to_h
51
- hash = {}
52
- hash[:name] = name if name?
53
- hash[:location] = location if location?
54
- hash[:description] = description if description?
55
- hash[:required] = required if required?
56
-
57
- hash[:schema] = schema.to_h if schema?
58
-
59
- hash[:type] = type if type?
60
- hash[:format] = format if format?
61
-
62
- hash[:items] = items.to_h if items?
63
-
64
- hash[:collection_format] = collection_format if collection_format?
65
- hash[:default] = default if default?
66
- hash[:maximum] = maximum if maximum?
67
- hash[:exclusive_maximum] = exclusive_maximum if exclusive_maximum?
68
- hash[:minimum] = minimum if minimum?
69
- hash[:exclusive_minimum] = exclusive_minimum if exclusive_minimum?
70
- hash[:max_length] = max_length if max_length?
71
- hash[:min_length] = min_length if min_length?
72
- hash[:unique_items] = unique_items if unique_items?
73
- hash[:multiple_of] = multiple_of if multiple_of?
74
- hash[:min_items] = min_items if min_items?
75
- hash[:max_items] = max_items if max_items?
76
- hash[:pattern] = pattern if pattern?
77
- hash[:enum] = enum if enum?
78
- hash
47
+ def path?
48
+ @location == 'path'
79
49
  end
80
50
 
81
- def to_swagger_hash
82
- hash = {}
83
- hash[:name] = name if name?
84
- hash[:in] = location if location?
85
- hash[:description] = description if description?
86
- hash[:required] = required if required?
87
-
88
- hash[:schema] = schema.to_swagger_json if schema?
89
-
90
- hash[:type] = type if type?
91
- hash[:format] = format if format?
92
-
93
- hash[:items] = items.to_swagger_json if items?
94
-
95
- hash[:collectionFormat] = collection_format if collection_format?
96
- hash[:default] = default if default?
97
- hash[:maximum] = maximum if maximum?
98
- hash[:exclusiveMaximum] = exclusive_maximum if exclusive_maximum?
99
- hash[:minimum] = minimum if minimum?
100
- hash[:exclusiveMinimum] = exclusive_minimum if exclusive_minimum?
101
- hash[:maxLength] = max_length if max_length?
102
- hash[:minLength] = min_length if min_length?
103
- hash[:uniqueItems] = unique_items if unique_items?
104
- hash[:multipleOf] = multiple_of if multiple_of?
105
- hash[:minItems] = min_items if min_items?
106
- hash[:maxItems] = max_items if max_items?
107
- hash[:pattern] = pattern if pattern?
108
- hash[:enum] = enum if enum?
109
- hash
51
+ def header?
52
+ @location == 'header'
110
53
  end
111
54
  end
112
55
  end