about summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2020-06-30 12:32:26 +0530
committerAndinus <andinus@nand.sh>2020-06-30 12:32:26 +0530
commit8dd9f42bfae6bb62b93b91f9c47ff410b12fa58b (patch)
treec4aa5f98164b15e10cc33a6ce5d4846bd2e0fbb5 /lib
parent420a084a2d6d28b4ece2f369c881965fe5ea89b8 (diff)
downloadcrux-8dd9f42bfae6bb62b93b91f9c47ff410b12fa58b.tar.gz
Fix logical error in $url generation
This issue started with f3d1bc5f9d86300c42d6a57a9f60bd508bdf9bb4:

  Use URI to encode the url for random_search

  I'll use this for everything eventually. Before this search terms
  like "rocky mountains" would break, also segments, query_keywords
  makes it easier.

Now the problem was that query_keywords forms urls like
`?nature+water' which totally makes sense & this is why it's logical
error. I want to form it in `?nature,water' format so first we use
`join' to form `nature,water' & then use URI::Encode to encode it
correctly & then use query_keywords to form $url.

We have to use URI::Encode because otherwise we would end up with same
problem that I tried fixing in
f3d1bc5f9d86300c42d6a57a9f60bd508bdf9bb4. "rocky mountains" would've
been seperated by space.

Now it's all good, every space will be encoded & keywords will be
seperated by `,' which is what Unsplash Source wants.
Diffstat (limited to 'lib')
-rw-r--r--lib/UnsplashSource.pm5
1 files changed, 3 insertions, 2 deletions
diff --git a/lib/UnsplashSource.pm b/lib/UnsplashSource.pm
index e4c26aa..c41960e 100644
--- a/lib/UnsplashSource.pm
+++ b/lib/UnsplashSource.pm
@@ -8,6 +8,7 @@ use warnings;
 use URI;
 use HTTP::Tiny;
 use Carp qw( croak carp );
+use URI::Encode qw( uri_encode );
 
 my $api = "https://source.unsplash.com";
 my $http = HTTP::Tiny->new(
@@ -51,7 +52,7 @@ sub random_search {
     push @segments, $options{resolution};
     $url->path_segments( @segments );
 
-    $url->query_keywords( \@{$options{search}} );
+    $url->query_keywords( uri_encode(join(',', @{$options{search}})) );
 
     return $http->head($url);
 }
@@ -92,7 +93,7 @@ sub fixed {
     push @segments, "weekly" if $options{weekly};
     $url->path_segments( @segments );
 
-    $url->query_keywords( \@{$options{search}} );
+    $url->query_keywords( uri_encode(join(',', @{$options{search}})) );
 
     return $http->head($url);
 }