File Coverage

blib/lib/SignalWire/Agents/SWML/Schema.pm
Criterion Covered Total %
statement 54 54 100.0
branch 3 6 50.0
condition 8 15 53.3
subroutine 12 12 100.0
pod 0 6 0.0
total 77 93 82.8


line stmt bran cond sub pod time code
1             package SignalWire::Agents::SWML::Schema;
2 2     2   313152 use strict;
  2         4  
  2         76  
3 2     2   9 use warnings;
  2         3  
  2         165  
4 2     2   641 use Moo;
  2         10014  
  2         13  
5 2     2   3113 use JSON ();
  2         13611  
  2         50  
6 2     2   14 use File::Basename ();
  2         5  
  2         1308  
7              
8             # Singleton instance
9             my $instance;
10              
11             has 'verbs' => (
12             is => 'ro',
13             default => sub { {} },
14             );
15              
16             has 'schema_data' => (
17             is => 'ro',
18             default => sub { {} },
19             );
20              
21             sub BUILD {
22 2     2 0 15 my ($self) = @_;
23 2         10 $self->_load_schema();
24             }
25              
26             sub _load_schema {
27 2     2   7 my ($self) = @_;
28 2         219 my $dir = File::Basename::dirname(__FILE__);
29 2         16 my $schema_file = "$dir/schema.json";
30              
31 2 50       163 open my $fh, '<', $schema_file
32             or die "Cannot open schema.json at $schema_file: $!";
33 2         14 local $/;
34 2         4020 my $json_text = <$fh>;
35 2         36 close $fh;
36              
37 2         10501 my $data = JSON::decode_json($json_text);
38 2         22 $self->{schema_data} = $data;
39              
40 2   50     29 my $defs = $data->{'$defs'} || {};
41 2   50     23 my $swml_method = $defs->{SWMLMethod} || {};
42 2   50     7 my $any_of = $swml_method->{anyOf} || [];
43              
44 2         5 my %verbs;
45 2         11 for my $entry (@$any_of) {
46 76   50     177 my $ref = $entry->{'$ref'} || next;
47 76         285 (my $def_name) = $ref =~ m{/([^/]+)$};
48 76 50       129 next unless $def_name;
49              
50 76   50     171 my $def = $defs->{$def_name} || next;
51 76   50     158 my $props = $def->{properties} || next;
52              
53 76         181 my @keys = keys %$props;
54 76 50       136 next unless @keys;
55              
56 76         118 my $verb_name = $keys[0];
57             $verbs{$verb_name} = {
58             schema_name => $def_name,
59             verb_name => $verb_name,
60 76         329 properties => $props->{$verb_name},
61             };
62             }
63              
64 2         94 $self->{verbs} = \%verbs;
65             }
66              
67             sub instance {
68 9     9 0 23871 my ($class) = @_;
69 9   66     56 $instance //= $class->new();
70 9         34 return $instance;
71             }
72              
73             sub get_verb_names {
74 2     2 0 12 my ($self) = @_;
75 2         6 return sort keys %{ $self->verbs };
  2         96  
76             }
77              
78             sub has_verb {
79 30     30 0 10866 my ($self, $name) = @_;
80 30         209 return exists $self->verbs->{$name};
81             }
82              
83             sub get_verb {
84 3     3 0 538 my ($self, $name) = @_;
85 3         18 return $self->verbs->{$name};
86             }
87              
88             sub verb_count {
89 3     3 0 970 my ($self) = @_;
90 3         6 return scalar keys %{ $self->verbs };
  3         26  
91             }
92              
93             1;