1package WebService::Technorati::CosmosApiQuery;
2use strict;
3use utf8;
4
5use WebService::Technorati::ApiQuery;
6use WebService::Technorati::LinkQuerySubject;
7use WebService::Technorati::BlogLink;
8use WebService::Technorati::Exception;
9use base 'WebService::Technorati::ApiQuery';
10
11use constant API_URI => '/cosmos';
12
13BEGIN {
14    use vars qw ($VERSION $DEBUG);
15    $VERSION    = 0.04;
16    $DEBUG       = 0;
17}
18
19sub 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
37sub 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
47sub 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
58sub 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
89See Also   : WebService::Technorati
90
91=cut
92
93sub 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
107See Also   : WebService::Technorati
108
109=cut
110
111sub getInboundLinks {
112    my $self = shift;
113    return (wantarray) ? @{$self->{'links'}} : $self->{'links'};
114}
115
1161;
117