| 1 | package MojoMojo::Formatter::Amazon; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 27 | ▶ | eval "use Net::Amazon"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 27 | my $eval_res=$@; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head2 module_loaded | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Return true if the module is loaded. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =cut | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 125 | ▶ | sub module_loaded { $eval_res ? 0 : 1 } | ||||||||||||||||||||||||||
|
Subroutine
module_loaded: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 27 | our $VERSION='0.01'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | MojoMojo::Formatter::Amazon - Include Amazon objects on your page. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | This is an url formatter. it takes urls containing amazon and | ||||||||||||||||||||||||||||
| 24 | /-/ or /ASIN/ and make a pretty formatted link to that object | ||||||||||||||||||||||||||||
| 25 | in the amazon web store. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | It automatically handles books/movies/dvds and formats them as | ||||||||||||||||||||||||||||
| 28 | apropriate. You can also pass 'small' as a parameter after the | ||||||||||||||||||||||||||||
| 29 | url, and it will make a thumb link instead of a blurb. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 METHODS | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head2 format_content_order | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | Format order can be 1-99. The Amazon formatter runs on 5. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 744 | ▶ | sub format_content_order { 5 } | ||||||||||||||||||||||||||
|
Subroutine
format_content_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 format_content | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | calls the formatter. Takes a ref to the content as well as the | ||||||||||||||||||||||||||||
| 44 | context object. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 124 | ▶ | sub format_content { | ||||||||||||||||||||||||||
|
Subroutine
format_content: called CC=7 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 124 | my ($class,$content,$c)=@_; | |||||||||||||||||||||||||||
| 50 | 124 | ▶ | return unless $class->module_loaded; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 124 | my @lines=split /\n/,$$content; | |||||||||||||||||||||||||||
| 52 | 124 | my $pod;$$content=""; | |||||||||||||||||||||||||||
| 53 | 124 | foreach my $line (@lines) { | |||||||||||||||||||||||||||
| 54 | 646 | ▶ | if ($line =~ m/(\{\{?:http:\/\/(?:www\.){0,1}amazon\.com(?:\/.*){0,1}(?:\/dp\/|\/gp\/product\/))(.*?)(?:\/.*|$)\}\}/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | my $item=$class->get($1,$c->config->{amazon_id}); | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | unless (ref($item)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | $$content.=$line."\n"; | |||||||||||||||||||||||||||
| 58 | 0 | next; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | next unless $class->can($2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | $$content.=$class->$2($item); | |||||||||||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||||||||||
| 64 | 0 | $$content.=$class->blurb($item); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||
| 67 | 646 | $$content .=$line."\n"; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 get <asin> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Connects to amazon and retrieves a L<Net::Amazon> object | ||||||||||||||||||||||||||||
| 76 | based on the supplied ASIN number. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ($class,$id,$amazon_id,$secret_key)=@_; | |||||||||||||||||||||||||||
| 82 | #FIXME: devel token should be set in formatter config. | ||||||||||||||||||||||||||||
| 83 | 0 | my $amazon=Net::Amazon->new(token=>$amazon_id,secret_key=>$secret_key); | |||||||||||||||||||||||||||
| 84 | 0 | my $response=$amazon->search(asin=>$id); | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | return "Unable to connect to amazon." unless $response->is_success; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | ($property)=$response->properties; | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | return "No property object" unless $property; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | return $property; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 small <property> | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Renders a small version of the formatter. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub small { | ||||||||||||||||||||||||||
|
Subroutine
small: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 98 | 0 | my ($class,$property)=@_; | |||||||||||||||||||||||||||
| 99 | 0 | return "!".$property->ImageUrlMedium. | |||||||||||||||||||||||||||
| 100 | '!:http://www.amazon.com/exec/obidos/ASIN/'.$property->Asin."/feed-20\n"; | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head2 blurb <property> | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | renders a full width blurb of the product, suitable for reviews and | ||||||||||||||||||||||||||||
| 106 | such. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =cut | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub blurb { | ||||||||||||||||||||||||||
|
Subroutine
blurb: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | my ($class,$property)=@_; | |||||||||||||||||||||||||||
| 112 | 0 | my $method=ref $property; | |||||||||||||||||||||||||||
| 113 | 0 | $method =~ s/.*:://; | |||||||||||||||||||||||||||
| 114 | 0 | ▶ | return "<div class=\"amazon\">!<".$property->ImageUrlSmall. | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$method && ($class->can($method) ? $class->$method($property) : "<br/>\n\n")
MC/DC0%0/2
$method && ($class->can($method) ? $class->$method($property) : "<br/>\n\n")
$method
($class->can($method) ? $class->$method($property) : "<br/>\n\n")
Truth table0%0/3
A && B
A= $methodB= ($class->can($method) ? $class->$method($property) : "<br/>\n\n")
| |||||||||||||||||||||||||||||
| 115 | '!:http://www.amazon.com/exec/obidos/ASIN/'.$property->Asin."/feed-20\n\n". | ||||||||||||||||||||||||||||
| 116 | "h1. ".$property->ProductName."\n\n". | ||||||||||||||||||||||||||||
| 117 | '"buy at amazon for '.$property->OurPrice.'":'. | ||||||||||||||||||||||||||||
| 118 | 'http://www.amazon.com/exec/obidos/ASIN/'.$property->Asin."/feed-20\n\n". | ||||||||||||||||||||||||||||
| 119 | ($method && ($class->can($method) ? $class->$method($property) :"<br/>\n\n")). | ||||||||||||||||||||||||||||
| 120 | "</div>"; | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head2 DVD <property> | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | Product information suitable for DVD movies. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub DVD { | ||||||||||||||||||||||||||
|
Subroutine
DVD: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 0 | my ($class,$property) = @_; | |||||||||||||||||||||||||||
| 131 | 0 | return " -- ??".join(',',$property->directors).'?? ('.$property->year .")\n\n"; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 Book <property> | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Product information suitable for books. | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =cut | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub Book { | ||||||||||||||||||||||||||
|
Subroutine
Book: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 0 | my ($class,$property) = @_; | |||||||||||||||||||||||||||
| 142 | 0 | return " -- ??".join(',',$property->authors).'?? ('.$property->year .")\n\n"; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head2 Music <property> | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Product information suitable for music CDs. | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | sub Music { | ||||||||||||||||||||||||||
|
Subroutine
Music: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 152 | 0 | my ($class,$property) = @_; | |||||||||||||||||||||||||||
| 153 | 0 | return " -- ??".join(',',$property->artists).'?? ('.$property->year .")\n\n"; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | L<MojoMojo>, L<Module::Pluggable::Ordered>, L<Net::Amazon>. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Marcus Ramberg <mramberg@cpan.org | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | This library is free software. You can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 167 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 27 | 1; | |||||||||||||||||||||||||||