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,40 @@
1
+ require 'skeleton/parameter'
2
+
3
+ module Skeleton
4
+ class Parameters
5
+ include Enumerable
6
+
7
+ def initialize
8
+ @list = []
9
+ end
10
+
11
+ def empty?
12
+ @list.empty?
13
+ end
14
+
15
+ def each(&block)
16
+ @list.each(&block)
17
+ end
18
+
19
+ def get(name)
20
+ find { |p| p.name == name }
21
+ end
22
+ alias_method :[], :get
23
+
24
+ def add(parameter)
25
+ @list << parameter
26
+ parameter
27
+ end
28
+
29
+ def required(name, args={})
30
+ opts = args.merge(name: name, required: true)
31
+ add(Skeleton::Parameter.new(opts))
32
+ end
33
+
34
+ def optional(name, args={})
35
+ opts = args.merge(name: name, required: false)
36
+ add(Skeleton::Parameter.new(opts))
37
+ end
38
+ alias_method :param, :optional
39
+ end
40
+ end
@@ -1,111 +1,73 @@
1
1
  require 'skeleton/operation'
2
- require 'skeleton/parameter'
3
- require 'skeleton/model'
4
2
 
5
3
  module Skeleton
6
- class Path < Model
7
- attr_accessor :ref
8
- attr_writer :operations
9
- attr_not_empty :parameters, :operations
4
+ class Path
5
+ attr_reader :operations
10
6
 
11
- def operations
12
- @operations ||= {}
7
+ def initialize
8
+ @operations = Hash.new
13
9
  end
14
10
 
15
- def parameters=(value)
16
- @parameters = Array(value)
11
+ def head(&block)
12
+ operation(:head, &block)
17
13
  end
18
14
 
19
- def parameters
20
- @parameters ||= []
15
+ def head?
16
+ @operations.key?(:head)
21
17
  end
22
18
 
23
- def parameter(location, name, &block)
24
- param = Skeleton::Parameter.new({name: name})
25
- yield(param) if block
26
- parameters << param
19
+ def get(&block)
20
+ operation(:get, &block)
27
21
  end
28
22
 
29
- def get(&block)
30
- define_operation(:get, &block)
23
+ def get?
24
+ @operations.key?(:get)
31
25
  end
32
26
 
33
27
  def put(&block)
34
- define_operation(:put, &block)
28
+ operation(:put, &block)
29
+ end
30
+
31
+ def put?
32
+ @operations.key?(:put)
35
33
  end
36
34
 
37
35
  def post(&block)
38
- define_operation(:post, &block)
36
+ operation(:post, &block)
37
+ end
38
+
39
+ def post?
40
+ @operations.key?(:post)
41
+ end
42
+
43
+ def patch(&block)
44
+ operation(:patch, &block)
45
+ end
46
+
47
+ def patch?
48
+ @operations.key?(:patch)
39
49
  end
40
50
 
41
51
  def delete(&block)
42
- define_operation(:delete, &block)
52
+ operation(:delete, &block)
53
+ end
54
+
55
+ def delete?
56
+ @operations.key?(:delete)
43
57
  end
44
58
 
45
59
  def options(&block)
46
- define_operation(:options, &block)
60
+ operation(:options, &block)
47
61
  end
48
62
 
49
- def head(&block)
50
- define_operation(:head, &block)
63
+ def options?
64
+ @operations.key?(:options)
51
65
  end
52
66
 
53
- def patch(&block)
54
- define_operation(:patch, &block)
55
- end
56
-
57
- def to_h
58
- hash = {}
59
- if operations?
60
- operations.each do |method, operation|
61
- if operation.respond_to?(:to_h)
62
- hash[method] = operation.to_h
63
- else
64
- hash[method] = operation
65
- end
66
- end
67
- end
68
- if parameters?
69
- hash[:parameters] = parameters.map do |parameter|
70
- if parameter.respond_to?(:to_h)
71
- parameter.to_h
72
- else
73
- parameter
74
- end
75
- end
76
- end
77
- hash
78
- end
79
-
80
- def to_swagger_hash
81
- hash = {}
82
- if operations?
83
- operations.each do |method, operation|
84
- if operation.respond_to?(:to_swagger_hash)
85
- hash[method] = operation.to_swagger_hash
86
- else
87
- hash[method] = operation
88
- end
89
- end
90
- end
91
- if parameters?
92
- hash[:parameters] = parameters.map do |parameter|
93
- if parameter.respond_to?(:to_swagger_hash)
94
- parameter.to_swagger_hash
95
- else
96
- parameter
97
- end
98
- end
99
- end
100
- hash
101
- end
102
-
103
- private
104
-
105
- def define_operation(method, &block)
106
- operation = Skeleton::Operation.new
107
- yield(operation) if block
108
- operations[method] = operation
67
+ def operation(type, &block)
68
+ @operations[type] = Skeleton::Operation.new
69
+ @operations[type].instance_eval(&block)
70
+ @operations[type]
109
71
  end
110
72
  end
111
73
  end
@@ -0,0 +1,19 @@
1
+ module Skeleton
2
+ class Presenter
3
+ def initialize(object)
4
+ @object = object
5
+ end
6
+
7
+ def method_missing(m, *args, &blk)
8
+ if @object.respond_to?(m)
9
+ @object.__send__ m, *args, &blk
10
+ else
11
+ super
12
+ end
13
+ end
14
+
15
+ def respond_to_missing?(m, include_private = false)
16
+ @object.respond_to?(m, include_private)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'skeleton/schema'
2
+
3
+ module Skeleton
4
+ class Property < Schema
5
+ end
6
+ end
@@ -1,59 +1,38 @@
1
- require 'skeleton/model'
2
- require 'skeleton/headers'
3
1
  require 'skeleton/schema'
2
+ require 'skeleton/header'
4
3
 
5
4
  module Skeleton
6
- class Response < Model
7
- attr_accessor :description
8
- attr_reader :headers, :schema
9
- attr_presence :descriptions, :examples
10
- attr_not_empty :headers, :schema
11
-
12
- def headers=(value)
13
- case value
14
- when Hash
15
- @headers = Skeleton::Headers.new(value)
16
- else
17
- @headers = value
18
- end
5
+ class Response < Schema
6
+ attr_accessor :summary, :description
7
+
8
+ alias_method :summarize, :summary=
9
+ alias_method :describe, :description=
10
+
11
+ def headers
12
+ @headers ||= {}
19
13
  end
20
14
 
21
- def schema=(value)
22
- case value
23
- when Hash
24
- @schema = Skeleton::Schema.new(value)
25
- else
26
- @schema = value
27
- end
15
+ def no_body
16
+ @schema = nil
28
17
  end
29
18
 
30
- def examples=(value)
31
- case value
32
- when Hash
33
- @examples = Skeleton::Example.new(value)
34
- else
35
- @examples = value
36
- end
19
+ def schema?
20
+ !!@schema
37
21
  end
38
22
 
39
- def to_h
40
- hash = {}
41
- hash[:description] = description if description?
42
- hash[:examples] = examples.to_h if examples?
43
- hash[:headers] = headers.to_h if headers?
44
- hash[:schema] = schema.to_h if schema?
45
- hash[:examples] = examples.to_h if examples?
46
- hash
23
+ def schema(value=nil, &block)
24
+ if block
25
+ @schema = Skeleton::Model.new
26
+ @schema.instance_eval(&block)
27
+ elsif value.is_a?(Hash)
28
+ @schema = Skeleton::Schema.new(value)
29
+ end
30
+
31
+ @schema
47
32
  end
48
33
 
49
- def to_swagger_hash
50
- hash = {}
51
- hash[:description] = description if description?
52
- hash[:examples] = examples.to_swagger_hash if examples?
53
- hash[:headers] = headers.to_swagger_hash if headers?
54
- hash[:schema] = schema.to_swagger_hash if schema?
55
- hash[:examples] = examples.to_swagger_hash if examples?
56
- hash
34
+ def header(field, options={})
35
+ headers[field] = Skeleton::Header.new(options)
57
36
  end
58
37
  end
59
38
  end
@@ -1,70 +1,87 @@
1
- require 'skeleton/model'
1
+ require 'skeleton/attributes'
2
2
 
3
3
  module Skeleton
4
- class Schema < Model
5
- attr_accessor :ref, :type, :format, :title, :description, :default,
6
- :multiple_of, :maximum, :exclusive_maximum, :minimum,
7
- :exclusive_minimum, :max_length, :min_length, :pattern,
8
- :max_items, :min_items, :unique_items, :max_properties,
9
- :min_properties, :required, :enum, :discriminator, :read_only,
10
- :xml, :external_docs, :example
11
-
12
- attr_presence :required, :exclusive_maximum, :exclusive_minimum,
13
- :unique_items, :discriminator, :read_only, :xml,
14
- :external_docs, :example
15
-
16
- def to_h
17
- hash = {}
18
- hash[:type] = type if type?
19
- hash[:format] = format if format?
20
-
21
- hash[:items] = items.to_hash if items?
22
-
23
- hash[:title] = title if title?
24
- hash[:description] = description if description?
25
- hash[:default] = default if default?
26
- hash[:multiple_of] = multiple_of if multiple_of?
27
- hash[:maximum] = maximum if maximum?
28
- hash[:exclusive_maximum] = exclusive_maximum if exclusive_maximum?
29
- hash[:minimum] = minimum if minimum?
30
- hash[:exclusive_minimum] = exclusive_minimum if exclusive_minimum?
31
- hash[:max_length] = max_length if max_length?
32
- hash[:min_length] = min_length if min_length?
33
- hash[:pattern] = pattern if pattern?
34
- hash[:max_items] = max_items if max_items?
35
- hash[:min_items] = min_items if min_items?
36
- hash[:unique_items] = unique_items if unique_items?
37
- hash[:max_properties] = max_properties if max_properties?
38
- hash[:min_properties] = min_properties if min_properties?
39
- hash[:enum] = enum if enum?
40
- hash
4
+ # @abstract This is simply an abstract class
5
+ class Schema
6
+ extend Skeleton::Attributes
7
+
8
+ PRIMITIVES = %w(string number integer boolean array file)
9
+
10
+ attr_accessor :ref, :title, :description, :default, :maximum,
11
+ :exclusive_maximum, :minimum, :exclusive_minimum, :max_length,
12
+ :min_length, :pattern, :max_items, :min_items, :unique_items,
13
+ :max_properties, :min_properties, :required, :multiple_of
14
+
15
+ attr_reader :type, :format, :items
16
+
17
+ attr_presence :ref, :title, :description, :default, :maximum,
18
+ :exclusive_maximum, :minimum, :exclusive_minimum, :max_length,
19
+ :min_length, :pattern, :max_items, :min_items, :unique_items,
20
+ :max_properties, :min_properties, :required, :format, :multiple_of,
21
+ :items
22
+
23
+ def initialize(args={})
24
+ args.each do |k, v|
25
+ setter = "#{k}="
26
+ self.send(setter, v) if self.respond_to?(setter)
27
+ end
28
+ end
29
+
30
+ def items=(value)
31
+ case value
32
+ when Hash
33
+ @items = Skeleton::Items.new(value)
34
+ else
35
+ @items = value
36
+ end
37
+ end
38
+
39
+ def properties
40
+ @properties ||= {}
41
+ end
42
+
43
+ def properties?
44
+ !properties.empty?
45
+ end
46
+
47
+ def enum
48
+ @enum ||= []
49
+ end
50
+
51
+ def format=(value)
52
+ @format = value.to_s
53
+ end
54
+
55
+ def type=(value)
56
+ @type = value.to_s
57
+ end
58
+
59
+ def primitive?
60
+ PRIMITIVES.include?(@type)
61
+ end
62
+
63
+ def array?
64
+ @type == 'array'
65
+ end
66
+
67
+ def string?
68
+ @type == 'string'
69
+ end
70
+
71
+ def number?
72
+ @type == 'number'
73
+ end
74
+
75
+ def integer?
76
+ @type == 'integer'
77
+ end
78
+
79
+ def boolean?
80
+ @type == 'boolean'
41
81
  end
42
82
 
43
- def to_swagger_hash
44
- hash = {}
45
- hash[:type] = type if type?
46
- hash[:format] = format if format?
47
-
48
- hash[:items] = items.to_swagger_hash if items?
49
-
50
- hash[:title] = title if title?
51
- hash[:description] = description if description?
52
- hash[:default] = default if default?
53
- hash[:multipleOf] = multiple_of if multiple_of?
54
- hash[:maximum] = maximum if maximum?
55
- hash[:exclusiveMaximum] = exclusive_maximum if exclusive_maximum?
56
- hash[:minimum] = minimum if minimum?
57
- hash[:exclusiveMinimum] = exclusive_minimum if exclusive_minimum?
58
- hash[:maxLength] = max_length if max_length?
59
- hash[:minLength] = min_length if min_length?
60
- hash[:pattern] = pattern if pattern?
61
- hash[:maxItems] = max_items if max_items?
62
- hash[:minItems] = min_items if min_items?
63
- hash[:uniqueItems] = unique_items if unique_items?
64
- hash[:maxProperties] = max_properties if max_properties?
65
- hash[:minProperties] = min_properties if min_properties?
66
- hash[:enum] = enum if enum?
67
- hash
83
+ def file?
84
+ @type == 'file'
68
85
  end
69
86
  end
70
87
  end