Module: Committee::Drivers

Defined in:
lib/committee/drivers.rb,
lib/committee/drivers/open_api_2.rb,
lib/committee/drivers/open_api_3.rb,
lib/committee/drivers/hyper_schema.rb

Defined Under Namespace

Classes: Driver, HyperSchema, OpenAPI2, OpenAPI3, Schema

Class Method Summary collapse

Class Method Details

.driver_from_name(name) ⇒ Object

Gets a driver instance from the specified name. Raises ArgumentError for an unknown driver name.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/committee/drivers.rb', line 5

def self.driver_from_name(name)
  case name
  when :hyper_schema
    Committee::Drivers::HyperSchema.new
  when :open_api_2
    Committee::Drivers::OpenAPI2.new
  when :open_api_3
    Committee::Drivers::OpenAPI3.new
  else
    raise ArgumentError, %{Committee: unknown driver "#{name}".}
  end
end

.load_from_data(hash) ⇒ Committee::Driver

load and build drive from Hash object

Parameters:

  • hash (Hash)

Returns:

  • (Committee::Driver)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/committee/drivers.rb', line 49

def self.load_from_data(hash)
  if hash['openapi']&.start_with?('3.0.')
    parser = OpenAPIParser.parse(hash)
    return Committee::Drivers::OpenAPI3.new.parse(parser)
  end

  driver = if hash['swagger'] == '2.0'
             Committee::Drivers::OpenAPI2.new
           else
             Committee::Drivers::HyperSchema.new
           end

  driver.parse(hash)
end

.load_from_file(schema_path) ⇒ Committee::Driver

load and build drive from file

Parameters:

  • schema_path (String)

Returns:

  • (Committee::Driver)


35
36
37
38
39
40
41
42
43
44
# File 'lib/committee/drivers.rb', line 35

def self.load_from_file(schema_path)
  case File.extname(schema_path)
  when '.json'
    load_from_json(schema_path)
  when '.yaml', '.yml'
    load_from_yaml(schema_path)
  else
    raise "Committee only supports the following file extensions: '.json', '.yaml', '.yml'"
  end
end

.load_from_json(schema_path) ⇒ Committee::Driver

load and build drive from JSON file

Parameters:

  • schema_path (String)

Returns:

  • (Committee::Driver)


21
22
23
# File 'lib/committee/drivers.rb', line 21

def self.load_from_json(schema_path)
  load_from_data(JSON.parse(File.read(schema_path)))
end

.load_from_yaml(schema_path) ⇒ Committee::Driver

load and build drive from YAML file

Parameters:

  • schema_path (String)

Returns:

  • (Committee::Driver)


28
29
30
# File 'lib/committee/drivers.rb', line 28

def self.load_from_yaml(schema_path)
  load_from_data(YAML.load_file(schema_path))
end