1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
package Open311::Endpoint::Schema;
use Moo;
use Data::Rx;
use Open311::Endpoint::Schema::Comma;
use Open311::Endpoint::Schema::Regex;
use Carp 'confess';
has endpoint => (
is => 'ro',
handles => [qw/
get_jurisdiction_id_required_clause
get_jurisdiction_id_optional_clause
get_identifier_type
learn_additional_types
/],
);
sub enum {
my ($self, $type, @values) = @_;
return {
type => '//any',
of => [ map {
{
type => $type,
value => $_,
}
} @values ],
};
}
sub format_boolean {
my ($self, $value) = @_;
return $value ? 'true' : 'false';
}
has schema => (
is => 'lazy',
default => sub {
my $self = shift;
my $schema = Data::Rx->new({
sort_keys => 1,
prefix => {
open311 => 'tag:wiki.open311.org,GeoReport_v2:rx/',
},
type_plugins => [qw(
Open311::Endpoint::Schema::Comma
Open311::Endpoint::Schema::Regex
)],
});
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/bool',
$self->enum( '//str', qw[ true false ] ));
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/datetime',
{
type => '/open311/regex',
pattern => qr{
^
\d{4} - \d{2} - \d{2} # yyyy-mm-dd
T
\d{2} : \d{2} : \d{2} # hh:mm:ss
(?:
Z # "Zulu" time, e.g. UTC
| [+-] \d{2} : \d{2} # +/- hh:mm offset
)
$
}ax, # use ascii semantics so /d means [0-9], and allow formatting
message => "found value isn't a datetime",
});
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/example/identifier',
{
type => '/open311/regex',
pattern => qr{^ \w+ $}ax,
message => "found value isn't a valid identifier",
});
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/status',
$self->enum( '//str', qw[ open closed ] ));
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/post_type',
$self->enum( '//str', qw[ realtime batch blackbox ] ));
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/service',
{
type => '//rec',
required => {
service_name => '//str',
type => '/open311/post_type',
metadata => '/open311/bool',
description => '//str',
service_code => '//str',
},
optional => {
keywords => '//str',
group => '//str',
}
}
);
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/value',
{
type => '//rec',
required => {
key => '//str',
name => '//str',
}
}
);
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/attribute',
{
type => '//rec',
required => {
code => '//str',
datatype => $self->enum( '//str', qw[ string number datetime text singlevaluelist multivaluelist ] ),
datatype_description => '//str',
description => '//str',
order => '//int',
required => '/open311/bool',
variable => '/open311/bool',
},
optional => {
values => {
type => '//arr',
contents => '/open311/value',
},
},
}
);
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/service_definition',
{
type => '//rec',
required => {
service_code => '//str',
attributes => {
type => '//arr',
contents => '/open311/attribute',
}
},
}
);
$schema->learn_type( 'tag:wiki.open311.org,GeoReport_v2:rx/service_request',
{
type => '//rec',
required => {
service_request_id => $self->get_identifier_type('service_request_id'),
status => '/open311/status',
service_name => '//str',
service_code => $self->get_identifier_type('service_code'),
requested_datetime => '/open311/datetime',
updated_datetime => '/open311/datetime',
address => '//str',
address_id => '//str',
zipcode => '//str',
lat => '//num',
long => '//num',
media_url => '//str',
},
optional => {
request => '//str',
description => '//str',
agency_responsible => '//str',
service_notice => '//str',
},
}
);
$self->learn_additional_types($schema);
return $schema;
},
);
1;
|