line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::WOVN::Store; |
2
|
1
|
|
|
1
|
|
398
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
3
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
17
|
|
4
|
1
|
|
|
1
|
|
428
|
use utf8; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
4
|
|
5
|
1
|
|
|
1
|
|
379
|
use parent 'Class::Accessor::Fast'; |
|
1
|
|
|
|
|
198
|
|
|
1
|
|
|
|
|
3
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
2277
|
use JSON; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
598
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
28152
|
|
|
1
|
|
|
|
|
26
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
351
|
use Plack::Middleware::WOVN::Lang; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
504
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/ config_loaded /); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $default_settings = { |
15
|
|
|
|
|
|
|
user_token => '', |
16
|
|
|
|
|
|
|
secret_key => '', |
17
|
|
|
|
|
|
|
url_pattern => 'path', |
18
|
|
|
|
|
|
|
url_pattern_reg => "/(?[^/.?]+)", |
19
|
|
|
|
|
|
|
query => [], |
20
|
|
|
|
|
|
|
api_url => 'https://api.wovn.io/v0/values', |
21
|
|
|
|
|
|
|
default_lang => 'en', |
22
|
|
|
|
|
|
|
supported_langs => ['en'], |
23
|
|
|
|
|
|
|
test_mode => 0, |
24
|
|
|
|
|
|
|
test_url => '', |
25
|
|
|
|
|
|
|
}; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new { |
28
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
29
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(); |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
0
|
|
|
|
my $args = shift || {}; |
32
|
0
|
|
0
|
|
|
|
my $settings = $args->{settings} || {}; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
$self->{settings} = +{ %$default_settings, %$settings }; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
$self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub is_valid_settings { |
40
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
41
|
0
|
|
|
|
|
|
my $settings = $self->{settings}; |
42
|
0
|
|
0
|
|
|
|
my $user_token = $settings->{user_token} || ''; |
43
|
0
|
|
|
|
|
|
my $secret_key = $settings->{secret_key}; |
44
|
0
|
|
|
|
|
|
my $url_pattern = $settings->{url_pattern}; |
45
|
0
|
|
|
|
|
|
my $query = $settings->{query}; |
46
|
0
|
|
|
|
|
|
my $api_url = $settings->{api_url}; |
47
|
0
|
|
|
|
|
|
my $default_lang = $settings->{default_lang}; |
48
|
0
|
|
|
|
|
|
my $supported_langs = $settings->{supported_langs}; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $valid = 1; |
51
|
0
|
|
|
|
|
|
my @errors; |
52
|
|
|
|
|
|
|
|
53
|
0
|
0
|
0
|
|
|
|
if ( length $user_token < 5 || length $user_token > 6 ) { |
54
|
0
|
|
|
|
|
|
$valid = 0; |
55
|
0
|
|
|
|
|
|
push @errors, "User token $user_token is not valid."; |
56
|
|
|
|
|
|
|
} |
57
|
0
|
0
|
0
|
|
|
|
if ( !defined $secret_key || $secret_key eq '' ) { |
58
|
0
|
|
|
|
|
|
$valid = 0; |
59
|
0
|
0
|
|
|
|
|
$secret_key = '' unless defined $secret_key; |
60
|
0
|
|
|
|
|
|
push @errors, "Secret key $secret_key is not valid."; |
61
|
|
|
|
|
|
|
} |
62
|
0
|
0
|
0
|
|
|
|
if ( !defined $url_pattern || $url_pattern eq '' ) { |
63
|
0
|
|
|
|
|
|
$valid = 0; |
64
|
0
|
0
|
|
|
|
|
$url_pattern = '' unless defined $url_pattern; |
65
|
0
|
|
|
|
|
|
push @errors, "Url pattern $url_pattern is not valid."; |
66
|
|
|
|
|
|
|
} |
67
|
0
|
0
|
0
|
|
|
|
if ( !$query || ref $query ne 'ARRAY' ) { |
68
|
0
|
|
|
|
|
|
$valid = 0; |
69
|
0
|
0
|
|
|
|
|
$query = '' unless defined $query; |
70
|
0
|
|
|
|
|
|
push @errors, "query config $query is not valid."; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
0
|
0
|
|
|
|
if ( !defined $api_url || $api_url eq '' ) { |
73
|
0
|
|
|
|
|
|
$valid = 0; |
74
|
0
|
|
|
|
|
|
push @errors, 'API url is not configured.'; |
75
|
|
|
|
|
|
|
} |
76
|
0
|
0
|
0
|
|
|
|
if ( !defined $default_lang || $default_lang eq '' ) { |
77
|
0
|
|
|
|
|
|
$valid = 0; |
78
|
0
|
0
|
|
|
|
|
$default_lang = '' unless defined $default_lang; |
79
|
0
|
|
|
|
|
|
push @errors, "Default lang $default_lang is not valid."; |
80
|
|
|
|
|
|
|
} |
81
|
0
|
0
|
0
|
|
|
|
if ( !$supported_langs |
|
|
|
0
|
|
|
|
|
82
|
|
|
|
|
|
|
|| ref $supported_langs ne 'ARRAY' |
83
|
|
|
|
|
|
|
|| @$supported_langs < 1 ) |
84
|
|
|
|
|
|
|
{ |
85
|
0
|
|
|
|
|
|
$valid = 0; |
86
|
0
|
|
|
|
|
|
push @errors, 'Supported langs configuration is not valid.'; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
if (@errors) { |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# output error logs? |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
$valid; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub settings { |
98
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
99
|
0
|
|
|
|
|
|
my $settings = $self->{settings}; |
100
|
|
|
|
|
|
|
|
101
|
0
|
0
|
|
|
|
|
if ( !$self->config_loaded ) { |
102
|
|
|
|
|
|
|
$settings->{default_lang} = Plack::Middleware::WOVN::Lang->get_code( |
103
|
0
|
|
|
|
|
|
$settings->{default_lang} ); |
104
|
0
|
0
|
|
|
|
|
if ( !exists $settings->{supported_langs} ) { |
105
|
0
|
|
|
|
|
|
$settings->{supported_langs} = [ $settings->{default_lang} ]; |
106
|
|
|
|
|
|
|
} |
107
|
0
|
0
|
|
|
|
|
if ( $settings->{url_pattern} eq 'path' ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
$settings->{url_pattern_reg} = "/(?[^/.?]+)"; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
elsif ( $settings->{url_pattern} eq 'query' ) { |
111
|
|
|
|
|
|
|
$settings->{url_pattern_reg} |
112
|
0
|
|
|
|
|
|
= "((\\?.*&)|\\?)wovn=(?[^&]+)(&|\$)"; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
elsif ( $settings->{url_pattern} eq 'subdomain' ) { |
115
|
0
|
|
|
|
|
|
$settings->{url_pattern_reg} = "^(?[^.]+)\."; |
116
|
|
|
|
|
|
|
} |
117
|
0
|
0
|
0
|
|
|
|
if ( !$settings->{test_mode} || $settings->{test_mode} ne 'on' ) { |
118
|
0
|
|
|
|
|
|
$settings->{test_mode} = 0; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
0
|
|
|
|
|
|
$settings->{test_mode} = 1; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
$self->config_loaded(1); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
|
$settings; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub get_values { |
131
|
0
|
|
|
0
|
0
|
|
my ( $self, $url ) = @_; |
132
|
0
|
|
|
|
|
|
$url =~ s/\/+$//; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my $api_url = $self->{settings}{api_url}; |
135
|
0
|
|
|
|
|
|
my $user_token = $self->{settings}{user_token}; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
138
|
0
|
|
|
|
|
|
my $res = $ua->get("$api_url?token=$user_token&url=$url"); |
139
|
|
|
|
|
|
|
|
140
|
0
|
|
|
|
|
|
my $values = {}; |
141
|
0
|
0
|
|
|
|
|
if ( $res->is_success ) { |
142
|
0
|
|
0
|
|
|
|
$values = eval { JSON::decode_json( $res->content ) } || {}; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
$values; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
__END__ |