Skip to content

Commit

Permalink
Merge pull request #218 from companieshouse/feature/get-content-type-…
Browse files Browse the repository at this point in the history
…sub-return-fix

Fix temporary implementation of get_content_tye sub
  • Loading branch information
amoriartyCH authored Mar 22, 2021
2 parents bd305f1 + dffcdcc commit 4c27cc5
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lib/ChGovUk/Controllers/Company/FilingHistory.pm
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ sub view {

my $delay_end = $delay->begin(0);
trace "Calling document API to retrieve content_type for possible zip filing [%s]", $doc->{links}->{document_metadata};
$self->_get_content_type( $doc->{links}->{document_metadata}, $doc, $delay_end);
$self->_get_content_type_application_zip( $doc->{links}->{document_metadata}, $doc, $delay_end);
}

if ( $formatted_transaction_date >= $formatted_xhtml_available_date) {
Expand Down Expand Up @@ -202,12 +202,10 @@ sub view {

#-------------------------------------------------------------------------------

# _get_content_type calls the document API and retrieves a content_type for the provided filing.
# Please note - this function grabs the first variable returned from the resources array and only uses it
# in the template if it is application/zip (zip filing). Changes would be required if you wanted to use
# other resource types returned in the template. If you want to return multiple changes would also be required to cater for this.
# This future work has been documented in a ticket on JIRA: https://companieshouse.atlassian.net/browse/UK-155.
sub _get_content_type {
# _get_content_type_application_zip calls the document API and retrieves a resources array of content_types for the provided filing.
# If a resources array is found, we will search the resources array for an 'application/zip' content_type and if found,
# set it on the '$doc' element for use in the frontend template.
sub _get_content_type_application_zip {
my ( $self, $document_metadata_uri, $doc, $callback ) = @_;

$self->ch_api->document($document_metadata_uri)->metadata->get->on(
Expand All @@ -234,8 +232,13 @@ sub _get_content_type {
my ($api, $tx) = @_;
my $resources = $tx->res->json->{resources};
if ($resources) {
$doc->{content_type} = (keys $resources)[0];
trace "Successfully retrieved content_type %s for %s", $doc->{content_type}, $document_metadata_uri;
foreach my $content_type (keys $resources) {
if ($content_type eq 'application/zip') {
trace "ZIP filing found. Setting content_type on doc to application/zip [%s]", $document_metadata_uri;
$doc->{content_type} = $content_type;
last;
}
}
} else {
trace "No content_type found for %s. Setting content_type to unknown", $document_metadata_uri;
$doc->{content_type} = 'unknown';
Expand Down

0 comments on commit 4c27cc5

Please sign in to comment.