| 1 | # ABSTRACT: Perl wrapper for the SEOmoz API | ||||||||||||
| 2 | package WWW::SEOmoz; | ||||||||||||
| 3 | |||||||||||||
| 4 | |||||||||||||
| 5 | 3 | ▶ | use Moose; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | ▶ | use namespace::autoclean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 3 | ▶ | use LWP::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 3 | ▶ | use URI::Escape; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 3 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 3 | ▶ | use Carp qw( croak ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 3 | ▶ | use Digest::SHA qw( hmac_sha1_base64 ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 3 | ▶ | use WWW::SEOmoz::URLMetrics; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 3 | ▶ | use WWW::SEOmoz::Links; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 3 | our $VERSION = '0.03'; # VERSION | |||||||||||
| 19 | |||||||||||||
| 20 | 3 | has access_id => ( | |||||||||||
| 21 | is => 'ro', | ||||||||||||
| 22 | isa => 'Str', | ||||||||||||
| 23 | required => 1, | ||||||||||||
| 24 | ); | ||||||||||||
| 25 | |||||||||||||
| 26 | 3 | has secret_key => ( | |||||||||||
| 27 | is => 'ro', | ||||||||||||
| 28 | isa => 'Str', | ||||||||||||
| 29 | required => 1, | ||||||||||||
| 30 | ); | ||||||||||||
| 31 | |||||||||||||
| 32 | 3 | has api_url => ( | |||||||||||
| 33 | is => 'ro', | ||||||||||||
| 34 | isa => 'Str', | ||||||||||||
| 35 | lazy_build => 1, | ||||||||||||
| 36 | ); | ||||||||||||
| 37 | |||||||||||||
| 38 | 3 | has ua => ( | |||||||||||
| 39 | is => 'ro', | ||||||||||||
| 40 | isa => 'LWP::UserAgent', | ||||||||||||
| 41 | lazy_build => 1, | ||||||||||||
| 42 | ); | ||||||||||||
| 43 | |||||||||||||
| 44 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||
| 45 | |||||||||||||
| 46 | 3 | my $API_BASE = 'http://lsapi.seomoz.com/'; | |||||||||||
| 47 | |||||||||||||
| 48 | 0 | ▶ | sub _build_ua { | ||||||||||
|
Subroutine
_build_ua: not called CC=1 SCAR=6.9
| |||||||||||||
| 49 | 0 | my $self = shift; | |||||||||||
| 50 | |||||||||||||
| 51 | 0 | my $ua = LWP::UserAgent->new; | |||||||||||
| 52 | 0 | $ua->env_proxy; | |||||||||||
| 53 | 0 | return $ua; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | 0 | ▶ | sub _build_api_url { | ||||||||||
|
Subroutine
_build_api_url: not called CC=1 SCAR=6.9
| |||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||
| 58 | |||||||||||||
| 59 | 0 | return $API_BASE . 'linkscape/'; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | sub _generate_authentication { | ||||||||||
|
Subroutine
_generate_authentication: not called CC=2 SCAR=17.9
| |||||||||||||
| 63 | 0 | my $self = shift; | |||||||||||
| 64 | |||||||||||||
| 65 | 0 | my $epoch = DateTime->now->add( seconds => 60 )->epoch; # A bit in the future | |||||||||||
| 66 | 0 | my $sig = hmac_sha1_base64( | |||||||||||
| 67 | $self->access_id . "\n" . $epoch, $self->secret_key | ||||||||||||
| 68 | ); | ||||||||||||
| 69 | |||||||||||||
| 70 | # Pad the base_64 encoding, if required | ||||||||||||
| 71 | 0 | ▶ | while (length($sig) % 4) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 0 | $sig .= '='; | |||||||||||
| 73 | } | ||||||||||||
| 74 | |||||||||||||
| 75 | 0 | $sig = uri_escape( $sig ); | |||||||||||
| 76 | |||||||||||||
| 77 | 0 | return '?AccessID='.$self->access_id.'&Expires='.$epoch.'&Signature='.$sig; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | sub _make_api_call { | ||||||||||
|
Subroutine
_make_api_call: not called CC=3 SCAR=24.8
| |||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||
| 82 | 0 | ▶ | my $url = shift || croak 'API URL required'; | ||||||||||
|
Condition0%0/2
shift() || croak('API URL required')
MC/DC0%0/1
shift() || croak('API URL required')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 83 | |||||||||||||
| 84 | 0 | my $res = $self->ua->get( $url ); | |||||||||||
| 85 | |||||||||||||
| 86 | 0 | ▶ | if ( $res->is_success ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | 0 | return from_json $res->content; | |||||||||||
| 88 | } | ||||||||||||
| 89 | |||||||||||||
| 90 | 0 | croak $res->content; | |||||||||||
| 91 | |||||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | |||||||||||||
| 95 | # XXX should allow people to request the metrics they want | ||||||||||||
| 96 | 0 | ▶ | sub url_metrics { | ||||||||||
|
Subroutine
url_metrics: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 97 | 0 | my $self = shift; | |||||||||||
| 98 | 0 | ▶ | my $url = shift || croak 'URL required'; | ||||||||||
|
Condition0%0/2
shift() || croak('URL required')
MC/DC0%0/1
shift() || croak('URL required')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 99 | |||||||||||||
| 100 | 0 | my $api_url = $self->api_url | |||||||||||
| 101 | . 'url-metrics/' | ||||||||||||
| 102 | . uri_escape($url) | ||||||||||||
| 103 | . $self->_generate_authentication | ||||||||||||
| 104 | . "&Cols=133712314365"; # MAGIC - see the API docs (http://apiwiki.seomoz.org/url-metrics) | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | my $url_metrics = WWW::SEOmoz::URLMetrics->new_from_data( | |||||||||||
| 107 | $self->_make_api_call( $api_url ) | ||||||||||||
| 108 | ); | ||||||||||||
| 109 | |||||||||||||
| 110 | 0 | return $url_metrics; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | |||||||||||||
| 114 | 0 | ▶ | sub links { | ||||||||||
|
Subroutine
links: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 115 | 0 | my $self = shift; | |||||||||||
| 116 | 0 | ▶ | my $url = shift || croak 'URL required'; | ||||||||||
|
Condition0%0/2
shift() || croak('URL required')
MC/DC0%0/1
shift() || croak('URL required')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 117 | 0 | ▶ | my $limit = shift || 30; | ||||||||||
|
Condition0%0/2
shift() || 30
MC/DC0%0/1
shift() || 30
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 118 | |||||||||||||
| 119 | 0 | my $api_url = $self->api_url | |||||||||||
| 120 | . 'links/' | ||||||||||||
| 121 | . uri_escape($url) | ||||||||||||
| 122 | . $self->_generate_authentication | ||||||||||||
| 123 | . '&SourceCols=4' | ||||||||||||
| 124 | . '&TargetCols=4' | ||||||||||||
| 125 | . '&Scope=page_to_page' | ||||||||||||
| 126 | . '&Sort=page_authority' | ||||||||||||
| 127 | . '&Limit=20'; | ||||||||||||
| 128 | |||||||||||||
| 129 | 0 | return WWW::SEOmoz::Links->new_from_data( | |||||||||||
| 130 | $self->_make_api_call( $api_url ) | ||||||||||||
| 131 | ); | ||||||||||||
| 132 | |||||||||||||
| 133 | } | ||||||||||||
| 134 | |||||||||||||
| 135 | |||||||||||||
| 136 | 3 | 1; | |||||||||||
| 137 | |||||||||||||
| 138 | __END__ | ||||||||||||