Class: ThreeScaleToolbox::OpenAPI::Swagger

Inherits:
Object
  • Object
show all
Defined in:
lib/3scale_toolbox/openapi/swagger.rb

Overview

Swagger object

  • Swagger.title -> string
  • Swagger.description -> string
  • Swagger.version -> string
  • Swagger.basePath -> string
  • Swagger.host -> string
  • Swagger.scheme -> string
  • Swagger.operation -> array of operation hash
    • operation hash properties
      • :verb
      • :path
      • :description
      • :operation_id
  • Swagger.security -> security hash
    • security hash properties
      • :id -> string
      • :type -> string
      • :name -> string
      • :in_f -> string
      • :flows -> hash
        • :implicit_flow_enabled -> bool
        • :direct_access_grants_enabled -> bool
        • :service_accounts_enabled -> bool
        • :standard_flow_enabled -> bool
      • :scopes -> array of string
  • Swagger.service_backend_version -> string ('1','2','oidc')
  • Swagger.set_server_url -> def(spec, url)
  • Swagger.set_oauth2_urls-> def(spec, scheme_id, authorization_url, token_url)

Constant Summary collapse

META_SCHEMA_PATH =
File.expand_path('../../../resources/swagger_meta_schema.json', __dir__)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#raw ⇒ Object (readonly)

Returns the value of attribute raw.



47
48
49
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 47

def raw
  @raw
end

Class Method Details

.build(raw, validate: true) ⇒ Object



41
42
43
44
45
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 41

def self.build(raw, validate: true)
  self.validate(raw) if validate

  new(raw)
end

.validate(raw) ⇒ Object



36
37
38
39
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 36

def self.validate(raw)
  meta_schema = JSON.parse(File.read(META_SCHEMA_PATH))
  JSON::Validator.validate!(meta_schema, raw)
end

Instance Method Details

#base_path ⇒ Object



61
62
63
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 61

def base_path
  raw['basePath']
end

#description ⇒ Object



53
54
55
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 53

def description
  raw.dig('info', 'description')
end

#host ⇒ Object



65
66
67
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 65

def host
  raw['host']
end

#operations ⇒ Object



73
74
75
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 73

def operations
  @operations ||= parse_operations
end

#scheme ⇒ Object



69
70
71
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 69

def scheme
  Array(raw['schemes']).first
end

#security ⇒ Object



77
78
79
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 77

def security
  @security ||= parse_security
end

#service_backend_version ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 81

def service_backend_version
  # default authentication mode if no security requirement
  return '1' if security.nil?

  case security[:type]
  when 'oauth2'
    'oidc'
  when 'apiKey'
    '1'
  else
    raise ThreeScaleToolbox::Error, "Unexpected security scheme type #{security[:type]}"
  end
end

#set_oauth2_urls(spec, sec_scheme_id, authorization_url, token_url) ⇒ Object

Update given spec with urls It is expected identified security scheme to be oauth2 type



106
107
108
109
110
111
112
113
114
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 106

def set_oauth2_urls(spec, sec_scheme_id, authorization_url, token_url)
  sec_scheme_obj = spec.dig('securityDefinitions', sec_scheme_id)
  if sec_scheme_obj.nil? || sec_scheme_obj['type'] != 'oauth2'
    raise ThreeScaleToolbox::Error, "Expected security scheme {#{sec_scheme_id}} not found or not oauth2"
  end

  sec_scheme_obj['authorizationUrl'] = authorization_url if %w[implicit accessCode].include?(sec_scheme_obj['flow'])
  sec_scheme_obj['tokenUrl'] = token_url if %w[password application accessCode].include?(sec_scheme_obj['flow'])
end

#set_server_url(spec, url) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 95

def set_server_url(spec, url)
  URI(url).tap do |uri|
    spec['host'] = "#{uri.host}:#{uri.port}"
    spec['schemes'] = [uri.scheme]
    spec['basePath'] = uri.path
  end
end

#title ⇒ Object



49
50
51
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 49

def title
  raw.dig('info', 'title')
end

#version ⇒ Object



57
58
59
# File 'lib/3scale_toolbox/openapi/swagger.rb', line 57

def version
  raw.dig('info', 'version')
end