| 1 | package Google::ISBNNumbers; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Cpanel::JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = "1.02"; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | 0 | my ($class, $google_api_key) = @_; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # the API key is required | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | croak "Google API Key is required to use Google::ISBNNumbers.\n". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 16 | "Please set up a key at https://console.cloud.google.com/apis/credentials" unless $google_api_key; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # become self, with an HTTP::Tiny and Cpanel::JSON objects | ||||||||||||||||||||||||||||
| 19 | 0 | my $self = bless { | |||||||||||||||||||||||||||
| 20 | 'api_key' => $google_api_key, | ||||||||||||||||||||||||||||
| 21 | 'json_coder' => Cpanel::JSON::XS->new->utf8->allow_nonref->allow_blessed, | ||||||||||||||||||||||||||||
| 22 | }, $class; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | return $self; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # method to query the open library for an ISBN number | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub lookup_isbn { | ||||||||||||||||||||||||||
|
Subroutine
lookup_isbn: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 0 | my ($self, $isbn_number) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # can't do much with a valid number | ||||||||||||||||||||||||||||
| 32 | 0 | $isbn_number =~ s/\-|\s//g; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | croak "Valid ISBN number required for lookup_book()" unless $isbn_number =~ /^97/ && $isbn_number !~ /\D/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$isbn_number =~ /^97/ and not $isbn_number =~ /\D/
MC/DC0%0/2
$isbn_number =~ /^97/ and not $isbn_number =~ /\D/
$isbn_number =~ /^97/
not $isbn_number =~ /\D/
Truth table0%0/3
A and not(B)
A= $isbn_number =~ /^97/B= not $isbn_number =~ /\D/
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # do the lookup! | ||||||||||||||||||||||||||||
| 36 | my $response = HTTP::Tiny->new->get( | ||||||||||||||||||||||||||||
| 37 | 'https://www.googleapis.com/books/v1/volumes?q=isbn:' | ||||||||||||||||||||||||||||
| 38 | . $isbn_number . '&key=' . $self->{api_key} | ||||||||||||||||||||||||||||
| 39 | 0 | ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # alert if failure | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | croak "Lookup failed: " . $response->{reason} unless $response->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # translate JSON to data struct | ||||||||||||||||||||||||||||
| 45 | 0 | my $results = $self->{json_coder}->decode( $response->{content} ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # must be an array | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | croak "Invalid results returned. Check your API key." unless ref($$results{items}) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # we have a book: simplify and return | ||||||||||||||||||||||||||||
| 51 | 0 | my $book_info = $results->{items}->[0]->{volumeInfo}; | |||||||||||||||||||||||||||
| 52 | return { | ||||||||||||||||||||||||||||
| 53 | 'title' => $book_info->{title}, | ||||||||||||||||||||||||||||
| 54 | 'author_name' => $book_info->{authors}->[0], | ||||||||||||||||||||||||||||
| 55 | 'publication_date' => $book_info->{publishedDate}, | ||||||||||||||||||||||||||||
| 56 | 'description' => $book_info->{description}, | ||||||||||||||||||||||||||||
| 57 | 'cover_link' => $book_info->{imageLinks}->{smallThumbnail}, | ||||||||||||||||||||||||||||
| 58 | 0 | }; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | 1; | |||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||