File Coverage

blib/lib/Web/oEmbed/Response.pm
Criterion Covered Total %
statement 12 49 24.4
branch 0 24 0.0
condition n/a
subroutine 4 8 50.0
pod 0 4 0.0
total 16 85 18.8


line stmt bran cond sub pod time code
1             package Web::oEmbed::Response;
2 3     3   18 use strict;
  3         8  
  3         115  
3 3     3   16 use Carp;
  3         6  
  3         391  
4 3     3   18 use Any::Moose;
  3         7  
  3         79  
5              
6             has 'http_response', is => 'ro', isa => 'HTTP::Response';
7              
8             has 'matched_uri', is => 'ro';
9             has 'type', is => 'rw';
10             has 'version', is => 'rw';
11             has 'title', is => 'rw';
12             has 'author_name', is => 'rw';
13             has 'author_url', is => 'rw';
14             has 'provider_name', is => 'rw';
15             has 'provider_url', is => 'rw';
16             has 'cache_age', is => 'rw';
17             has 'thumbnail_url', is => 'rw';
18             has 'thumbnail_width', is => 'rw';
19             has 'thumbnail_height', is => 'rw';
20              
21             has 'url', is => 'rw';
22             has 'width', is => 'rw';
23             has 'height', is => 'rw';
24              
25             has 'html', is => 'rw';
26              
27 3     3   7007 use HTML::Element;
  3         102643  
  3         32  
28              
29             sub new_from_response {
30 0     0 0   my($class, $http_res, $uri) = @_;
31              
32 0 0         return if $http_res->is_error;
33              
34 0           my $res = $class->new( http_response => $http_res, matched_uri => $uri );
35              
36 0           my $data;
37 0 0         if ($http_res->content_type =~ /json/) {
    0          
38 0           $data = $res->parse_json($http_res->content);
39             } elsif ($http_res->content_type =~ /xml/) {
40 0           $data = $res->parse_xml($http_res->content);
41             } else {
42 0           croak "Content-Type is not either JSON or XML: " . $http_res->content_type;
43             }
44              
45 0           for my $key (keys %$data) {
46 0 0         if ($res->can($key)) {
47 0           $res->$key( $data->{$key} );
48             } else {
49 0           $res->{$key} = $data->{$key};
50             }
51             }
52              
53 0           $res;
54             }
55              
56             sub parse_json {
57 0     0 0   my($self, $json) = @_;
58 0           require JSON::XS;
59 0           JSON::XS->new->decode($json);
60             }
61              
62             sub parse_xml {
63 0     0 0   my($self, $xml) = @_;
64 0           require XML::LibXML::Simple;
65 0           XML::LibXML::Simple->new->XMLin($xml);
66             }
67              
68             sub render {
69 0     0 0   my $self = shift;
70              
71 0 0         if ($self->type eq 'photo') {
72 0 0         if ($self->thumbnail_url) {
73 0           my $element = HTML::Element->new('a', href => $self->url);
74 0 0         $element->attr(title => $self->title) if defined $self->title;
75 0           my $img = HTML::Element->new(
76             'img',
77             src => $self->thumbnail_url,
78             width => $self->thumbnail_width,
79             height => $self->thumbnail_height,
80             );
81 0 0         $img->attr(alt => $self->title) if defined $self->title;
82              
83 0           $element->push_content($img);
84 0           return $element->as_HTML;
85             } else {
86 0           my $img = HTML::Element->new(
87             'img',
88             src => $self->url,
89             width => $self->width,
90             height => $self->height,
91             );
92 0 0         $img->attr(alt => $self->title) if defined $self->title;
93 0           return $img->as_HTML;
94             }
95             }
96              
97 0 0         if ($self->type eq 'link') {
98 0           my $element = HTML::Element->new('a', href => $self->url);
99 0 0         $element->push_content(defined $self->title ? $self->title : $self->url);
100 0           return $element->as_HTML;
101             }
102              
103 0 0         if ($self->html) {
104 0           return $self->html;
105             }
106             }
107              
108             1;