| 1 | package P5U::Lib::Whois; | ||
| 2 | |||
| 3 | BEGIN { | ||
| 4 | $P5U::Lib::Whois::AUTHORITY = 'cpan:TOBYINK'; | ||
| 5 | $P5U::Lib::Whois::VERSION = '0.100'; | ||
| 6 | }; | ||
| 7 | |||
| 8 | use Moo; no warnings; | ||
| 9 | use Types::Standard qw< ArrayRef HashRef Maybe Num Str >; | ||
| 10 | use JSON qw(from_json); | ||
| 11 | use LWP::Simple qw(get); | ||
| 12 | use Object::AUTHORITY; | ||
| 13 | |||
| 14 | use constant { | ||
| 15 | template_website => 'https://metacpan.org/author/%s', | ||
| 16 | template_email => '%s@cpan.org', | ||
| 17 | template_metacpan_data => 'http://api.metacpan.org/v0/author/%s', | ||
| 18 | template_metacpan_releases => 'http://api.metacpan.org/v0/release/_search?q=author:%s+AND+status:latest&fields=name,date,abstract,status&size=5000', | ||
| 19 | template_metacpan_popular => 'http://api.metacpan.org/v0/favorite/_search?q=author:%s&fields=distribution&size=5000', | ||
| 20 | }; | ||
| 21 | |||
| 22 | has cpanid => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => Str, | ||
| 25 | ); | ||
| 26 | |||
| 27 | has metacpan_data => ( | ||
| 28 | is => 'lazy', | ||
| 29 | isa => HashRef, | ||
| 30 | ); | ||
| 31 | |||
| 32 | sub _build_metacpan_data | ||
| 33 | { | ||
| 34 | from_json get sprintf __PACKAGE__->template_metacpan_data, uc shift->cpanid | ||
| 35 | } | ||
| 36 | |||
| 37 | has [qw(metacpan_releases metacpan_popular)] => ( | ||
| 38 | is => 'lazy', | ||
| 39 | isa => ArrayRef, | ||
| 40 | ); | ||
| 41 | |||
| 42 | sub _build_metacpan_releases | ||
| 43 | { | ||
| 44 | [ | ||
| 45 | map $_->{fields}, @{ | ||
| 46 | (from_json get sprintf __PACKAGE__->template_metacpan_releases, uc shift->cpanid) | ||
| 47 | ->{hits}{hits} | ||
| 48 | } | ||
| 49 | ] | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _build_metacpan_popular | ||
| 53 | { | ||
| 54 | my @plusplus = map $_->{fields}{distribution}, @{ | ||
| 55 | (from_json get sprintf __PACKAGE__->template_metacpan_popular, uc shift->cpanid) | ||
| 56 | ->{hits}{hits} | ||
| 57 | }; | ||
| 58 | my %dist; $dist{$_}++ for @plusplus; | ||
| 59 | [ sort { $b->[1] <=> $a->[1] or $a->[0] cmp $b->[0] } map [ $_ => $dist{$_} ], keys %dist ]; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | has $_ => ( | ||
| 64 | is => 'lazy', | ||
| 65 | isa => Maybe[Str], | ||
| 66 | ) for qw(name city region country); | ||
| 67 | |||
| 68 | sub _build_name { $_[0]->metacpan_data->{name} } | ||
| 69 | sub _build_city { $_[0]->metacpan_data->{city} } | ||
| 70 | sub _build_region { $_[0]->metacpan_data->{region} } | ||
| 71 | sub _build_country { $_[0]->metacpan_data->{country} } | ||
| 72 | |||
| 73 | has $_ => ( | ||
| 74 | is => 'lazy', | ||
| 75 | isa => Maybe[Num], | ||
| 76 | ) for qw(latitude longitude); | ||
| 77 | |||
| 78 | sub _build_longitude { $_[0]->metacpan_data->{location}[0] } | ||
| 79 | sub _build_latitude { $_[0]->metacpan_data->{location}[1] } | ||
| 80 | |||
| 81 | has $_ => ( | ||
| 82 | is => 'lazy', | ||
| 83 | isa => ArrayRef, | ||
| 84 | ) for qw(website email); | ||
| 85 | |||
| 86 | sub _build_website | ||
| 87 | { | ||
| 88 | my @r = @{ $_[0]->metacpan_data->{website} || [] }; | ||
| 89 | @r = sprintf __PACKAGE__->template_website, uc shift->cpanid unless @r; | ||
| 90 | \@r | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _build_email | ||
| 94 | { | ||
| 95 | my @r = @{ $_[0]->metacpan_data->{email} || [] }; | ||
| 96 | @r = sprintf __PACKAGE__->template_email, lc shift->cpanid unless @r; | ||
| 97 | \@r | ||
| 98 | } | ||
| 99 | |||
| 100 | sub location | ||
| 101 | { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | my $addr = join q[, ], grep defined, map { $self->$_ } qw(city region country); | ||
| 105 | |||
| 106 | if (defined $self->longitude and defined $self->latitude) | ||
| 107 | { | ||
| 108 | $addr .= sprintf( | ||
| 109 | " (%s, %s)", | ||
| 110 | $self->latitude, | ||
| 111 | $self->longitude, | ||
| 112 | ) | ||
| 113 | } | ||
| 114 | |||
| 115 | return $addr; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub releases | ||
| 119 | { | ||
| 120 | my $self = shift; | ||
| 121 | my @r = | ||
| 122 | sort | ||
| 123 | map { $_->{name} } | ||
| 124 | @{ $self->metacpan_releases || [] }; | ||
| 125 | wantarray ? @r : \@r | ||
| 126 | } | ||
| 127 | |||
| 128 | sub namespaces | ||
| 129 | { | ||
| 130 | my $self = shift; | ||
| 131 | my %counts; | ||
| 132 | for ($self->releases) | ||
| 133 | { | ||
| 134 | next unless /^(.+?)-/; | ||
| 135 | $counts{$1}++; | ||
| 136 | } | ||
| 137 | my @r = | ||
| 138 | sort { $counts{$b} <=> $counts{$a} or $a cmp $b } | ||
| 139 | keys %counts; | ||
| 140 | wantarray ? @r : \@r | ||
| 141 | } | ||
| 142 | |||
| 143 | sub report | ||
| 144 | { | ||
| 145 | my ($self, $detailed) = @_; | ||
| 146 | my $report = sprintf("%s (%s)\n", $self->name, uc $self->cpanid); | ||
| 147 | |||
| 148 | my $location = $self->location; | ||
| 149 | $report .= "$location\n" if $location =~ /\w/; | ||
| 150 | |||
| 151 | my $web = join q( ), map { "<$_>" } @{ $self->website }; | ||
| 152 | $report .= "$web\n" if $web =~ /\w/; | ||
| 153 | |||
| 154 | my $email = join q( ), map { "<mailto:$_>" } @{ $self->email }; | ||
| 155 | $report .= "$email\n" if $email =~ /\w/; | ||
| 156 | |||
| 157 | if ($detailed) | ||
| 158 | { | ||
| 159 | my @namespaces = $self->namespaces; | ||
| 160 | $report .= sprintf "\nNamespaces: %s\n" => join q(, ), @namespaces[0..9] | ||
| 161 | if @namespaces; | ||
| 162 | |||
| 163 | my @recent = | ||
| 164 | map { | ||
| 165 | sprintf | ||
| 166 | '%s: %s - %s', | ||
| 167 | substr($_->{date}, 0, 10), | ||
| 168 | $_->{name}, | ||
| 169 | $_->{abstract}, | ||
| 170 | } | ||
| 171 | sort { $b->{date} cmp $a->{date} } | ||
| 172 | @{ $self->metacpan_releases || [] }; | ||
| 173 | $report .= join "\n", q(), q(Recent:), @recent[0..9], q() | ||
| 174 | if @recent; | ||
| 175 | |||
| 176 | my @popular = map sprintf('%s - %d votes', @$_), @{ $self->metacpan_popular || [] }; | ||
| 177 | $report .= join "\n", q(), q(Popular:), @popular[0..9], q() | ||
| 178 | if @recent; | ||
| 179 | |||
| 180 | if (@{ $self->metacpan_data->{profile} || [] }) | ||
| 181 | { | ||
| 182 | $report .= "\n"; | ||
| 183 | for (sort { $a->{name} cmp $b->{name} } @{ $self->metacpan_data->{profile} }) | ||
| 184 | { | ||
| 185 | $report .= sprintf(qq{%-16s%s\n}, @{$_}{qw{name id}}) | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | return $report; | ||
| 191 | } | ||
| 192 | |||
| 193 | 1; | ||
| 194 | |||
| 195 | |||
| 196 | |||
| 197 | __END__ |