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