Module: Circuitdata

Defined in:
lib/circuitdata.rb,
lib/circuitdata/profile.rb,
lib/circuitdata/version.rb,
lib/circuitdata/dereferencer.rb

Defined Under Namespace

Classes: CompatibilityChecker, Dereferencer, FileComparer, Profile, Tools

Constant Summary collapse

SCHEMA_PATH =
'circuitdata/schema_files/v1/ottp_circuitdata_schema.json'
SCHEMA_FULL_PATH =
File.join(__dir__, SCHEMA_PATH)
VERSION =
'0.6.4'

Class Method Summary collapse

Class Method Details

.compare_files(file_hash, validate_origins = false) ⇒ Object



103
104
105
106
# File 'lib/circuitdata.rb', line 103

def self.compare_files(file_hash, validate_origins=false)
  comparer = FileComparer.new(file_hash, validate_origins)
  comparer.compare
end

.compatibility_checker(product_file, check_file = nil, validate_origins = false) ⇒ Object



108
109
110
111
# File 'lib/circuitdata.rb', line 108

def self.compatibility_checker(product_file, check_file=nil, validate_origins=false)
  checker = CompatibilityChecker.new(product_file, check_file, validate_origins)
  checker.start_check
end

.create_documentationObject



113
114
115
116
117
# File 'lib/circuitdata.rb', line 113

def self.create_documentation()
  docu = Tools.new()
  ra = docu.create_structure
  docu.create_documentation(ra)
end

.dereferenced_schemaObject



96
97
98
99
100
101
# File 'lib/circuitdata.rb', line 96

def self.dereferenced_schema
  Dereferencer.dereference(
    schema,
    File.dirname(Circuitdata::SCHEMA_FULL_PATH)
  )
end

.get_data_summary(data) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/circuitdata.rb', line 14

def self.get_data_summary(data)
  types = []
  wrapper = data&.dig(:open_trade_transfer_package)
  types << 'profile_restricted' unless wrapper&.dig(:profiles, :restricted).nil?
  types << 'profile_enforced' unless wrapper&.dig(:profiles, :enforced).nil?
  types << 'profile_defaults' unless wrapper&.dig(:profiles, :defaults).nil?
  types << 'capabilities' unless wrapper&.dig(:capabilities).nil?

  products = wrapper&.dig(:products)
  product_names = products.nil? ? [] : products.keys # this will return all the product names
  types << 'product' if product_names.any?
  # loop through the products
  products.each do |k, v|
    if v&.dig(:stackup, :specification_level) == 'specified' && !v&.dig(:stackup, :specification_level, :specified).nil?
      types << 'stackup'
    end
  end unless products.nil?

  return product_names, types
end

.read_json(file) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/circuitdata.rb', line 35

def self.read_json(file)
  error, message, data = false, nil, nil

  if file.is_a? Hash
    begin
      data = file
      data.deep_symbolize_keys!
    rescue
      error = true
      message = "Could not convert the Hash into JSON"
    end
  else
    begin
      open(file) do |f|
        data = JSON.parse(f.read, symbolize_names: true)
      end
    rescue
      error = true
      message = "Could not read the file"
    end
  end
  return error, message, data
end

.schemaObject



89
90
91
92
93
94
# File 'lib/circuitdata.rb', line 89

def self.schema
  JSON.parse(
    File.read(SCHEMA_FULL_PATH),
    symbolize_names: true
  )
end

.testObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/circuitdata.rb', line 119

def self.test
  product1 = File.join(File.dirname(__FILE__), '../test/test_data/test_product1.json')
  product2 = File.join(File.dirname(__FILE__), '../test/test_data/test_product2.json')
  profile_restricted = File.join(File.dirname(__FILE__), '../test/test_data/testfile-profile-restricted.json')
  profile_enforced = File.join(File.dirname(__FILE__), '../test/test_data/testfile-profile-enforced.json')
  profile_default = File.join(File.dirname(__FILE__), '../test/test_data/testfile-profile-default.json')
  capabilities = File.join(File.dirname(__FILE__), '../test/test_data/testfile-capability.json')

  # THEN TEST THE COMPARE FILES:
  puts "Testing file comparison"
  file_hash = {product1: product1, product2: product2, restricted: profile_restricted, enforced: profile_enforced, default: profile_default, capability: capabilities}
  Circuitdata.compare_files(file_hash, true)
end

.validate(content) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/circuitdata.rb', line 59

def self.validate(content)
  error, message, validations_errors = false, nil, {}

  begin
    validated = JSON::Validator.fully_validate(SCHEMA_FULL_PATH, content, :errors_as_objects => true)
  rescue JSON::Schema::ReadFailed
    error = true
    message = "Could not read the validating schema"
  rescue JSON::Schema::SchemaError
    error = true
    message = "There is something was wrong with the validating schema"
  end
  unless error
    if validated.count > 0
      error = true
      message = "Could not validate the file against the CircuitData json schema"
      validated.each do |val_error|
        validations_errors[val_error[:fragment]] = [] unless validations_errors.has_key? val_error[:fragment]
        begin
          keep = val_error[:message].match("^(The\\sproperty\\s\\'[\\s\\S]*\\'\\s)([\\s\\S]*)(\\sin\\sschema\\sfile[\\s\\S]*)$").captures[1]
        rescue
          keep = val_error[:message]
        end
        validations_errors[val_error[:fragment]] << keep
      end
    end
  end
  return error, message, validations_errors
end