| 1 | package WebService::Technorati::CosmosApiQuery; | ||
| 2 | use strict; | ||
| 3 | use utf8; | ||
| 4 | |||
| 5 | use WebService::Technorati::ApiQuery; | ||
| 6 | use WebService::Technorati::LinkQuerySubject; | ||
| 7 | use WebService::Technorati::BlogLink; | ||
| 8 | use WebService::Technorati::Exception; | ||
| 9 | use base 'WebService::Technorati::ApiQuery'; | ||
| 10 | |||
| 11 | use constant API_URI => '/cosmos'; | ||
| 12 | |||
| 13 | BEGIN { | ||
| 14 | use vars qw ($VERSION $DEBUG); | ||
| 15 | $VERSION = 0.04; | ||
| 16 | $DEBUG = 0; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my ($class, %params) = @_; | ||
| 21 | if (! exists $params{'key'}) { | ||
| 22 | WebService::Technorati::InstantiationException->throw( | ||
| 23 | "WebService::Technorati::CosmosApiQuery must be " . | ||
| 24 | "instantiated with at least 'key => theverylongkeystring'"); | ||
| 25 | } | ||
| 26 | my $data = {}; | ||
| 27 | if (! exists $params{'url'}) { | ||
| 28 | $data->{'needs_url'}++; | ||
| 29 | } | ||
| 30 | for my $k (keys %params) { | ||
| 31 | $data->{'args'}{$k} = $params{$k}; | ||
| 32 | } | ||
| 33 | my $self = bless ($data, ref ($class) || $class); | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub url { | ||
| 38 | my $self = shift; | ||
| 39 | my $url = shift; | ||
| 40 | if ($url) { | ||
| 41 | $self->{'url'} = $url; | ||
| 42 | delete($self->{'needs_url'}); | ||
| 43 | } | ||
| 44 | return $self->{'url'}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub execute { | ||
| 48 | my $self = shift; | ||
| 49 | my $apiUrl = $self->apiHostUrl() . API_URI; | ||
| 50 | if (exists $self->{'needs_url'}) { | ||
| 51 | WebService::Technorati::StateValidationException->throw( | ||
| 52 | "WebService::Technorati::AuthorinfoApiQuery must have a " . | ||
| 53 | "'url' attribute set prior to query execution"); | ||
| 54 | } | ||
| 55 | $self->SUPER::execute($apiUrl,$self->{'args'}); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub readResults { | ||
| 59 | my $self = shift; | ||
| 60 | my $result_xp = shift; | ||
| 61 | my $error = $result_xp->find('/tapi/document/result/error'); | ||
| 62 | if ($error) { | ||
| 63 | WebService::Technorati::DataException->throw($error); | ||
| 64 | } | ||
| 65 | my $nodeset = $result_xp->find("/tapi/document/result"); | ||
| 66 | my $node = $nodeset->shift; | ||
| 67 | my $blogSubject = WebService::Technorati::LinkQuerySubject->new_from_node($node); | ||
| 68 | $nodeset = $result_xp->find('/tapi/document/item'); | ||
| 69 | my @links = (); | ||
| 70 | for my $node ($nodeset->get_nodelist) { | ||
| 71 | my $blogLink = WebService::Technorati::BlogLink->new_from_node($node); | ||
| 72 | push(@links, $blogLink); | ||
| 73 | } | ||
| 74 | $self->{'subject'} = $blogSubject; | ||
| 75 | $self->{'links'} = \@links; | ||
| 76 | |||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | =head2 getLinkQuerySubject | ||
| 81 | |||
| 82 | Usage : getLinkQuerySubject(); | ||
| 83 | Purpose : | ||
| 84 | Returns : a scalar WebService::Technorati::LinkQuerySubject instance | ||
| 85 | Argument : none | ||
| 86 | Throws : none | ||
| 87 | Comments : the URL subject (blog or not) is returned with what | ||
| 88 | Technorati knows about it | ||
| 89 | See Also : WebService::Technorati | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub getLinkQuerySubject { | ||
| 94 | my $self = shift; | ||
| 95 | return $self->{'subject'}; | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 getInboundLinks | ||
| 99 | |||
| 100 | Usage : getInboundLinks(); | ||
| 101 | Purpose : | ||
| 102 | Returns : an array of WebService::Technorati::BlogLink instances | ||
| 103 | Argument : none | ||
| 104 | Throws : none | ||
| 105 | Comments : the list of inbound links are returned with what Technorati knows | ||
| 106 | about them | ||
| 107 | See Also : WebService::Technorati | ||
| 108 | |||
| 109 | =cut | ||
| 110 | |||
| 111 | sub getInboundLinks { | ||
| 112 | my $self = shift; | ||
| 113 | return (wantarray) ? @{$self->{'links'}} : $self->{'links'}; | ||
| 114 | } | ||
| 115 | |||
| 116 | 1; |