epb_view_models 1.0.8 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 59d79d9f281a39f02dbee9c2951c691175b3dfc89f0bf3e3f907aa3210e07e8a
4
- data.tar.gz: 648c87f4d9ad0b4f23eaf859969665ec413b4a62fd7fc621d764cd2211231eb2
3
+ metadata.gz: 1aa9014f126ebe6df35a45f9be7898fe71ebf4253739d537c62ab133f976480a
4
+ data.tar.gz: 9de075f8be11ba4626d771128e75f72917edfb3797eeee2f4bac4c6252df32d7
5
5
  SHA512:
6
- metadata.gz: 40c679fd072c9fea3087edcfc7e04d289f1bf4c653c60cb2f1c25fb114aa3cbf9d4ef1d4a3ac9c17220bd6b3f876d133b29e8d58bda62acf642e6fa16eef9977
7
- data.tar.gz: a754bf7fe7e9a8fb144ae351fa1e7af1591ee572370d384a23fb857f254133c5087a313951dd77757dea1b38baaffb45c973de651a971a0f2bb941f1027ac4c3
6
+ metadata.gz: 3ea8139966fc1c55555be100e3ecb63a19364eb6e027c631aa1865a753af2577abee1c00ab53edb78436754fdd89db0aa331a42518cdf1bea5b03b242be48023
7
+ data.tar.gz: cefc2119116d4aab5c1299dd71f609df481e4ca2327a17a8bca256d83a92eeefed2e17127650e3d24d86fdd39445306d8456439947c55db20119168414ce50c7
data/Gemfile CHANGED
@@ -10,5 +10,6 @@ source "https://rubygems.org" do
10
10
  end
11
11
 
12
12
  gem "nokogiri", "~> 1.11"
13
+ gem "rexml", "~> 3.2", ">= 3.2.5"
13
14
  gem "zeitwerk", "~> 2.4.2"
14
15
  end
data/Gemfile.lock CHANGED
@@ -83,6 +83,7 @@ PLATFORMS
83
83
  DEPENDENCIES
84
84
  nokogiri (~> 1.11)!
85
85
  rake (~> 13.0.6)!
86
+ rexml (~> 3.2, >= 3.2.5)!
86
87
  rspec (~> 3.0)!
87
88
  rubocop-govuk (~> 4.0.0)!
88
89
  zeitwerk (~> 2.4.2)!
@@ -5,7 +5,7 @@ loader = Zeitwerk::Loader.for_gem
5
5
  loader.setup
6
6
 
7
7
  module EpbViewModels
8
- VERSION = "1.0.8"
8
+ VERSION = "1.0.12"
9
9
  end
10
10
 
11
11
  # Monkey patching to avoid using ActiveRecord::Type::Boolean.new.cast
@@ -0,0 +1,67 @@
1
+ module Presenter
2
+ class Xsd
3
+ def get_enums_by_type(domain_arguments)
4
+ xsd_files_gateway = ViewModelGateway::XsdFilesGateway.new(domain_arguments)
5
+
6
+ begin
7
+ xsd_files = xsd_files_gateway.xsd_files
8
+ rescue ViewModelBoundary::XsdFilesNotFound => e
9
+ raise ViewModelBoundary::XsdFilesNotFound, e.message.to_s
10
+ end
11
+
12
+ hash = {}
13
+
14
+ xsd_files.each do |file|
15
+ enums_hash = File.extname(file).downcase == ".xsd" ? read_xsd(file, domain_arguments.simple_type) : read_xml(file, domain_arguments.simple_type, domain_arguments.node_hash)
16
+
17
+ next if enums_hash.empty?
18
+
19
+ hash[xsd_files_gateway.schema_version(file)] = enums_hash
20
+ end
21
+
22
+ raise ViewModelBoundary::NodeNotFound, "Node #{domain_arguments.simple_type} was not found in any of the files in #{domain_arguments.xsd_dir_path} directory" if hash.empty?
23
+
24
+ hash
25
+ end
26
+
27
+ def unique_enums(domain_arguments)
28
+ uniq_enums = []
29
+ enums = get_enums_by_type(domain_arguments).values
30
+
31
+ enums.each_with_index do |_hash, i|
32
+ if i.positive? && (enums[i].to_a != enums[i + 1].to_a)
33
+ uniq_enums << enums[i]
34
+ end
35
+ end
36
+ uniq_enums
37
+ end
38
+
39
+ def variation_between_schema_versions?(enums_hash)
40
+ enums_hash.values.flatten.uniq.count != 1
41
+ end
42
+
43
+ private
44
+
45
+ def read_xsd(file_name, simple_type)
46
+ xpath = "//xs:simpleType[@name='#{simple_type}']//xs:enumeration"
47
+ doc = REXML::Document.new(File.read(file_name))
48
+ enums_hash = {}
49
+ REXML::XPath.each(doc, "#{xpath}/@value") do |node|
50
+ desc_path = "#{xpath}[@value='#{node.value}']//xs:annotation//xs:documentation"
51
+ enums_hash.merge!(node.value => REXML::XPath.first(doc, desc_path).children.first)
52
+ end
53
+ enums_hash
54
+ end
55
+
56
+ def read_xml(file_name, node_name, node_hash)
57
+ doc = Nokogiri.XML(File.read(file_name))
58
+ enums_hash = {}
59
+
60
+ doc.xpath(node_name).each do |node|
61
+ enums_hash.merge!(node.xpath(node_hash.keys[0].to_s).children.text => node.xpath(node_hash.values[0]).children.text)
62
+ end
63
+
64
+ enums_hash
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module ViewModelBoundary
2
+ class NodeNotFound < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module ViewModelBoundary
2
+ class XsdFilesNotFound < StandardError; end
3
+ end
@@ -0,0 +1,13 @@
1
+ module ViewModelDomain
2
+ class XsdArguments
3
+ attr_reader :simple_type, :assessment_type, :xsd_dir_path, :gem_path, :node_hash
4
+
5
+ def initialize(simple_type:, assessment_type:, xsd_dir_path: "", gem_path: "", node_hash: nil)
6
+ @simple_type = simple_type
7
+ @assessment_type = assessment_type
8
+ @xsd_dir_path = xsd_dir_path
9
+ @gem_path = gem_path
10
+ @node_hash = node_hash
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,61 @@
1
+ module ViewModelGateway
2
+ class XsdFilesGateway
3
+ attr_reader :simple_type, :assessment_type, :xsd_dir_path, :glob_path
4
+
5
+ API_PATH = "api/schemas/xml/".freeze
6
+ XSD_DEFAULT_PATH = "/api/schemas/xml/**/".freeze
7
+
8
+ def initialize(domain_arguments)
9
+ @simple_type = domain_arguments.simple_type
10
+ @assessment_type = domain_arguments.assessment_type
11
+ @xsd_dir_path = domain_arguments.xsd_dir_path
12
+ @dir_path = Dir.pwd if domain_arguments.gem_path.nil? || domain_arguments.gem_path.empty?
13
+ end
14
+
15
+ def schema_version(file)
16
+ api_path_start = file.index(API_PATH) + API_PATH.length
17
+ schema_version = file[api_path_start..].split("/").first
18
+ sap_defnied_in_rdsap_dir?(file) ? "#{schema_version}/SAP" : schema_version
19
+ end
20
+
21
+ def xsd_files
22
+ files = if @xsd_dir_path.nil? || @xsd_dir_path.empty?
23
+ case @assessment_type.downcase
24
+ when "sap"
25
+ sap_xsd_files
26
+ when "rdsap"
27
+ rdsap_xsd_files
28
+ when "cepc"
29
+ cepc_xsd_files
30
+ end
31
+ else
32
+ Dir.glob("#{@dir_path}#{@xsd_dir_path}")
33
+ end
34
+
35
+ raise ViewModelBoundary::XsdFilesNotFound, "No xsd files were found in #{@glob_path} directory" if files.nil? || files.empty?
36
+
37
+ files
38
+ end
39
+
40
+ private
41
+
42
+ def sap_defnied_in_rdsap_dir?(file)
43
+ assessment_type == "SAP" && file.end_with?("SAP-Domains.xsd")
44
+ end
45
+
46
+ def sap_xsd_files
47
+ @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}*-Domains.xsd"
48
+ Dir.glob(@glob_path)
49
+ end
50
+
51
+ def rdsap_xsd_files
52
+ @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}*-Domains.xsd"
53
+ Dir.glob(@glob_path)
54
+ end
55
+
56
+ def cepc_xsd_files
57
+ @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}Reported-Data.xsd"
58
+ Dir.glob(@glob_path)
59
+ end
60
+ end
61
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epb_view_models
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - MHCLG Energy Performance of Buildings
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-27 00:00:00.000000000 Z
11
+ date: 2021-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -1661,6 +1661,7 @@ files:
1661
1661
  - lib/presenter/sap/recommendation_report.rb
1662
1662
  - lib/presenter/sap/report.rb
1663
1663
  - lib/presenter/sap/summary.rb
1664
+ - lib/presenter/xsd.rb
1664
1665
  - lib/view_model/ac_cert_wrapper.rb
1665
1666
  - lib/view_model/ac_report_wrapper.rb
1666
1667
  - lib/view_model/base_view_model.rb
@@ -1820,6 +1821,10 @@ files:
1820
1821
  - lib/view_model/sap_schema_ni_174/common_schema.rb
1821
1822
  - lib/view_model/sap_schema_ni_1800/common_schema.rb
1822
1823
  - lib/view_model/sap_wrapper.rb
1824
+ - lib/view_model_boundary/node_not_found.rb
1825
+ - lib/view_model_boundary/xsd_files_not_found.rb
1826
+ - lib/view_model_domain/xsd_arguments.rb
1827
+ - lib/view_model_gateway/xsd_files_gateway.rb
1823
1828
  homepage: https://github.com/communitiesuk/epb-view-models
1824
1829
  licenses:
1825
1830
  - MIT