Class: OdataDuty::OAS2

Inherits:
Object
  • Object
show all
Defined in:
lib/odata_duty/oas2.rb,
lib/odata_duty/oas2/collection_get_path.rb,
lib/odata_duty/oas2/individual_get_path.rb,
lib/odata_duty/oas2/collection_post_path.rb,
lib/odata_duty/oas2/individual_patch_path.rb,
lib/odata_duty/oas2/individual_delete_path.rb

Defined Under Namespace

Classes: CollectionGetPath, CollectionPostPath, IndividualDeletePath, IndividualGetPath, IndividualPatchPath

Constant Summary collapse

ERROR_PROPERTIES =
{
  'code' => { 'type' => 'string', 'description' => 'A service-defined error code.' },
  'message' => { 'type' => 'string', 'description' => 'A human-readable message.' },
  'target' => { 'type' => 'string', 'description' => 'The target of the error.',
                'x-nullable' => true }
}.freeze
DEFAULT_ERROR_RESPONSE =
{
  'description' => 'Unexpected error',
  'schema' => {
    '$ref' => '#/definitions/Error'
  }
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, context:) ⇒ OAS2

Returns a new instance of OAS2.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/odata_duty/oas2.rb', line 19

def initialize(schema, context:)
  @schema = schema
  @context = context
  @info = {}
  @paths = {}
  @definitions = {}
  @hash = { 'swagger' => '2.0', 'info' => info, 'host' => schema.host,
            'schemes' => [schema.scheme], 'basePath' => schema.base_path,
            'paths' => paths, 'definitions' => definitions }
  info['version'] = schema.version if schema.version
  info['title'] = schema.title if schema.title
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



17
18
19
# File 'lib/odata_duty/oas2.rb', line 17

def context
  @context
end

#definitionsObject (readonly)

Returns the value of attribute definitions.



17
18
19
# File 'lib/odata_duty/oas2.rb', line 17

def definitions
  @definitions
end

#hashObject (readonly)

Returns the value of attribute hash.



17
18
19
# File 'lib/odata_duty/oas2.rb', line 17

def hash
  @hash
end

#infoObject (readonly)

Returns the value of attribute info.



17
18
19
# File 'lib/odata_duty/oas2.rb', line 17

def info
  @info
end

#pathsObject (readonly)

Returns the value of attribute paths.



17
18
19
# File 'lib/odata_duty/oas2.rb', line 17

def paths
  @paths
end

#schemaObject (readonly)

Returns the value of attribute schema.



17
18
19
# File 'lib/odata_duty/oas2.rb', line 17

def schema
  @schema
end

Class Method Details

.build_json(schema, context: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/odata_duty/oas2.rb', line 6

def self.build_json(schema, context: nil)
  builder = new(schema, context: context)
  builder.add_error_definition
  builder.add_enum_definitions
  builder.add_complex_definitions
  builder.add_request_body_definitions
  builder.add_collection_paths
  builder.add_individual_paths
  builder.hash
end

Instance Method Details

#add_collection_pathsObject



71
72
73
74
75
76
77
# File 'lib/odata_duty/oas2.rb', line 71

def add_collection_paths
  schema.collection_entity_sets.each do |entity_set|
    path = { 'get' => CollectionGetPath.new(entity_set, wrap_context(entity_set)).to_oas2 }
    path['post'] = CollectionPostPath.to_oas2(entity_set) if entity_set.supports_create?
    paths["/#{entity_set.url}"] = path
  end
end

#add_complex_definitionsObject



56
57
58
59
60
# File 'lib/odata_duty/oas2.rb', line 56

def add_complex_definitions
  (schema.complex_types + schema.entity_types).each do |complex_type|
    definitions[complex_type.name] = complex_type.to_oas2
  end
end

#add_enum_definitionsObject



50
51
52
53
54
# File 'lib/odata_duty/oas2.rb', line 50

def add_enum_definitions
  schema.enum_types.each do |enum_type|
    definitions[enum_type.name] = enum_type.to_oas2
  end
end

#add_error_definitionObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/odata_duty/oas2.rb', line 38

def add_error_definition
  definitions['Error'] = {
    'type' => 'object',
    'properties' => {
      'error' => {
        'type' => 'object',
        'properties' => ERROR_PROPERTIES
      }
    }
  }
end

#add_individual_pathsObject



79
80
81
82
83
84
85
86
# File 'lib/odata_duty/oas2.rb', line 79

def add_individual_paths
  schema.individual_entity_sets.each do |entity_set|
    path = { 'get' => IndividualGetPath.new(entity_set).to_oas2 }
    path['patch'] = IndividualPatchPath.to_oas2(entity_set) if entity_set.supports_update?
    path['delete'] = IndividualDeletePath.to_oas2(entity_set) if entity_set.supports_delete?
    paths["/#{entity_set.url}({id})"] = path
  end
end

#add_request_body_definitionsObject



62
63
64
65
66
67
68
69
# File 'lib/odata_duty/oas2.rb', line 62

def add_request_body_definitions
  schema.collection_entity_sets.select(&:supports_create?).each do |entity_set|
    register_definition(CollectionPostPath.request_body_definition(entity_set))
  end
  schema.individual_entity_sets.select(&:supports_update?).each do |entity_set|
    register_definition(IndividualPatchPath.request_body_definition(entity_set))
  end
end