PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/DDG/Spice/Snow.pm

https://github.com/eladab/zeroclickinfo-spice
Perl | 76 lines | 60 code | 12 blank | 4 comment | 8 complexity | 2d5a3ba71476c596f526893387a8328a MD5 | raw file
 1package DDG::Spice::Snow;
 2
 3use DDG::Spice;
 4
 5primary_example_queries "is it snowing?";
 6secondary_example_queries "is it snowing in New York City?";
 7description "Check weather conditions at your location";
 8name "Snow";
 9icon_url "/icon16.png";
10source "Is it snowing yet?";
11code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Snow.pm";
12topics "everyday";
13category "facts";
14attribution web => [ 'https://www.duckduckgo.com', 'DuckDuckGo' ],
15            github => [ 'https://github.com/duckduckgo', 'duckduckgo'],
16            twitter => ['http://twitter.com/duckduckgo', 'duckduckgo'];
17
18spice to => 'http://isitsnowingyet.org/api/check/$1/key/{{ENV{DDG_SPICE_SNOW_APIKEY}}}';
19
20triggers any => "snow", "snowing";
21
22spice proxy_cache_valid => '418 1d';
23
24my %snow = map { $_ => undef } (
25    'is it going to snow',
26    'going to snow',
27    'going to snow today',
28    'is it snowing',
29    'is it snowing here',
30    'is it snowing now',
31    'is it going to snow here',
32    'is it snowing today',
33    'is it going to snow today',
34    'going to snow today',
35    'is it snowing yet',
36    'make it snow',
37    'let it snow'
38);
39
40handle query_lc => sub {
41    my $query = $_;
42
43    # It's possible that $loc or some of its attributes will be null. If we
44    # don't know where the user is, we can't determine the weather there, so we
45    # return. This prevents a needless request to isitsnowingyet; we already
46    # know that we would get an empty response.
47    return unless $loc && ($loc->city || $loc->region_name) && $loc->country_name;
48
49    my @location = ();
50    if ($loc->city) {
51      push(@location, $loc->city);
52    }
53    if ($loc->region_name) {
54      push(@location, $loc->region_name);
55    }
56    if ($loc->country_name) {
57      push(@location, $loc->country_name);
58    }
59
60    my $location_str = join(',', @location);
61
62    if (exists $snow{$query}) {
63        return $location_str, {is_cached => 0};
64    } elsif ($query =~ /^(?:is[ ]it[ ])?
65                        (?:going[ ]to[ ])?
66                        snow(?:ing)?[ ]?
67                        (?:(?:here|now|yet)[ ]?)?
68                        (?:in[ ](.*?))?
69                        (?:[ ]today)?\??$/ix) {
70        return $1 if $1;
71        return $location_str, {is_cached => 0};
72    }
73    return;
74};
75
761;