| 1 | package Acme::ICan::tSpell; | ||
| 2 | |||
| 3 | 2 | our $VERSION = '0.03'; | |
| 4 | |||
| 5 | 2 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use MooX::LazierAttributes qw/rw lzy/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use MooX::ValidateSubs; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Types::Standard qw/Object Str HashRef/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 2 | ▶ | use HTTP::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use URI::Escape; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use Carp qw/croak/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | attributes ( | ||
| 14 | 0 | ▶ | tiny => [Object, {lzy, default => sub {HTTP::Tiny->new}}], |
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||
| 15 | base_url => [Str, {lzy, default => 'http://www.google.com/search?gws_rd=ssl&hl=en&q='}], | ||
| 16 | ); | ||
| 17 | |||
| 18 | 2 | validate_subs ( | |
| 19 | get => { params => [ [Str] ], returns => [[HashRef]] }, | ||
| 20 | spell_check => { | ||
| 21 | params => { check => [Str], base_url => [Str, 'base_url'] }, | ||
| 22 | returns => [[Str]], | ||
| 23 | }, | ||
| 24 | spell => { params => [[Str]], returns => [[Str]] }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | sub get { | ||
| 28 | my $response = $_[0]->tiny->get($_[1]); | ||
| 29 | $response->{success} and return $response; | ||
| 30 | croak sprintf "something went terribly wrong status - %s - reason - %s", | ||
| 31 | $response->{status}, $response->{reason}; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub spell_check { | ||
| 35 | my $moon = $_[0]->get(sprintf('%s%s', $_[1]->{base_url}, uri_escape($_[1]->{check})))->{content}; | ||
| 36 | if ($moon =~ m{(?:Showing results for|Did you mean|Including results for)[^\0]*?<a.*?>(.*?)</a>}){ | ||
| 37 | (my $str = $1) =~ s/<.*?>//g; | ||
| 38 | return $_[0]->spell_check({ check => $str }); # work around googles struggles | ||
| 39 | } | ||
| 40 | return $_[1]->{check}; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub spell { | ||
| 44 | return $_[0]->spell_check({ check => $_[1] }); | ||
| 45 | } | ||
| 46 | |||
| 47 | 2 | 1; | |
| 48 | |||
| 49 | __END__ | ||