File Coverage

blib/lib/WebService/Prismatic/InterestGraph.pm
Criterion Covered Total %
statement 15 32 46.8
branch 0 6 0.0
condition n/a
subroutine 5 8 62.5
pod 2 2 100.0
total 22 48 45.8


line stmt bran cond sub pod time code
1             package WebService::Prismatic::InterestGraph;
2             $WebService::Prismatic::InterestGraph::VERSION = '0.02';
3 1     1   600 use 5.006;
  1         3  
  1         33  
4 1     1   2149 use Moo;
  1         19472  
  1         5  
5 1     1   1684 use JSON qw(decode_json);
  1         1  
  1         7  
6 1     1   137 use Carp qw/ croak /;
  1         1  
  1         52  
7 1     1   440 use WebService::Prismatic::InterestGraph::Tag;
  1         3  
  1         360  
8              
9             has api_token => (
10             is => 'ro',
11             required => 1,
12             );
13              
14             # Really not sure it's worth having this as an attribute
15             # because the usage is pretty tied to HTTP::Tiny.
16             # But if I don't do this, Olaf will just bug me about it.
17             has ua => (
18             is => 'ro',
19             default => sub {
20             require HTTP::Tiny;
21             require IO::Socket::SSL;
22             return HTTP::Tiny->new;
23             },
24             );
25              
26             has base_url => (
27             is => 'ro',
28             default => sub { 'https://interest-graph.getprismatic.com' },
29             );
30              
31             sub tag_url
32             {
33 0     0 1   my ($self, $url) = @_;
34 0           return $self->_post_tag_request('url/topic', { url => $url });
35             }
36              
37             sub tag_text
38             {
39 0     0 1   my ($self, $text, $title) = @_;
40 0           my $params = { body => $text };
41              
42 0 0         $params->{title} = defined($title) ? $title :'';
43 0           return $self->_post_tag_request('text/topic', $params);
44             }
45              
46             sub _post_tag_request
47             {
48 0     0     my ($self, $path, $params) = @_;
49 0           my $full_url = $self->base_url.'/'.$path;
50 0           my $headers = { 'X-API-TOKEN' => $self->api_token,
51             Accept => 'application/json',
52             };
53 0           my $response = $self->ua->post_form($full_url, $params,
54             { headers => $headers });
55              
56 0 0         if (!$response->{success}) {
57 0           croak "failed to make request: $response->{status} $response->{reason}";
58             }
59 0           my $ref = decode_json($response->{content});
60 0 0         return unless exists($ref->{topics});
61              
62 0           return map { WebService::Prismatic::InterestGraph::Tag->new($_) }
  0            
63 0           @{ $ref->{topics} };
64             }
65              
66             1;
67              
68             =head1 NAME
69              
70             WebService::Prismatic::InterestGraph - identify topics in web page or text
71              
72             =head1 SYNOPSIS
73              
74             use WebService::Prismatic::InterestGraph;
75             my $ig = WebService::Prismatic::InterestGraph->new( api_key => $key );
76             my @tags = $ig->tag_url('http://perl.org');
77              
78             foreach my $tag (@tags) {
79             printf " %s [score: %f]\n", $tag->topic, $tag->score;
80             }
81              
82             =head1 DESCRIPTION
83              
84             This module provides a simple interface to the Prismatic Interface Graph API,
85             which is an alpha service provided by L.
86             It takes a piece of text and returns a number of tags, each of which
87             identifies a topic and a score for how likely the text includes that topic.
88             The text can either be specified via a URL, or passed as a scalar.
89              
90             Before you can use the API, you must register with Prismatic to
91             get an I. One you've got that, you're ready to go.
92              
93             Please note: because the service is in alpha,
94             you're currently restricted to 20 calls per minute.
95              
96             Prismatic is a service which suggests things on the web that
97             you might be interested in reading.
98              
99             =head1 METHODS
100              
101             =head2 new
102              
103             The constructor takes an C:
104              
105             use WebService::Prismatic::InterestGraph;
106              
107             my $ig = WebService::Prismatic::InterestGraph->new( api_key => $key );
108              
109             You can also pass an HTTP user agent with the C parameter,
110             but it pretty much has to be an instance of L.
111              
112             =head2 tag_url( $URL )
113              
114             Takes a URL and analyses the text of the referenced page.
115             Returns a list of zero or more tags:
116              
117             @tags = $ig->tag_url('http://perl.org');
118              
119             The tags are instances of L,
120             data objects with the following methods:
121              
122             =over 4
123              
124             =item * topic: short text giving the label for a topic, such as "open source"
125              
126             =item * score: a number between 0 and 1 which says how likely it is that the text is actually about that topic.
127              
128             =item * id: a unique integer identifier for the topic.
129              
130             =back
131              
132             =head2 tag_text( $TEXT [,$TITLE] )
133              
134             Takes some text and an optional title string and returns a list of tags,
135             as for C above:
136              
137             @tags = $ig->tag_url($body, $title);
138              
139             =head1 SEE ALSO
140              
141             L - the Prismatic blog post where they announced the API.
142              
143             L - the github
144             repo which has details of the API.
145              
146             L - the Prismatic home page.
147              
148             =head1 REPOSITORY
149              
150             L
151              
152             =head1 AUTHOR
153              
154             Neil Bowers Eneilb@cpan.orgE
155              
156             =head1 COPYRIGHT AND LICENSE
157              
158             This software is copyright (c) 2015 by Neil Bowers .
159              
160             This is free software; you can redistribute it and/or modify it under
161             the same terms as the Perl 5 programming language system itself.
162              
163             =cut