Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode/decode social volume words to/from www form #4438

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/sanbase/social_data/social_volume.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ defmodule Sanbase.SocialData.SocialVolume do

transformed_words =
case Keyword.get(opts, :treat_word_as_lucene_query, false) do
false -> transformed_words |> Enum.map(&String.downcase/1)
true -> transformed_words
false ->
transformed_words
|> Enum.map(fn word ->
word
|> String.downcase()
|> URI.encode_www_form()
end)

true ->
Enum.map(transformed_words, fn word ->
word
|> URI.encode_www_form()
end)
end

selector = %{selector | words: transformed_words}
Expand Down Expand Up @@ -98,7 +109,7 @@ defmodule Sanbase.SocialData.SocialVolume do
|> maybe_format_response(words)
|> Enum.map(fn {word, %{} = timeseries} ->
%{
word: word,
word: URI.decode_www_form(word),
timeseries_data: social_volume_result(timeseries)
}
end)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
body =
%{
"data" => %{
"btc" => %{
URI.encode_www_form("btc") => %{
"2024-09-28T00:00:00Z" => 373,
"2024-09-29T00:00:00Z" => 487,
"2024-09-30T00:00:00Z" => 323
},
"eth or nft" => %{
URI.encode_www_form("eth or nft") => %{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We return from the API the keys as www-form-urlencoded strings ? Does this mean that the frontend should change their logic also ?

"2024-09-28T00:00:00Z" => 1681,
"2024-09-29T00:00:00Z" => 3246,
"2024-09-30T00:00:00Z" => 1577
Expand All @@ -38,8 +38,9 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
Map.new(options[:params])
|> Map.get("search_texts")

# Assert that the words are lowercased before they are sent
assert search_texts == "eth or nft,btc"
# Assert that the words are lowercased and www form encoded
assert search_texts ==
URI.encode_www_form("eth or nft") <> "," <> URI.encode_www_form("btc")

{:ok, resp}
end)
Expand Down Expand Up @@ -97,12 +98,12 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
body =
%{
"data" => %{
"BTC" => %{
URI.encode_www_form("BTC") => %{
"2024-09-28T00:00:00Z" => 373,
"2024-09-29T00:00:00Z" => 487,
"2024-09-30T00:00:00Z" => 323
},
"eth OR nft" => %{
URI.encode_www_form("eth OR nft") => %{
"2024-09-28T00:00:00Z" => 1681,
"2024-09-29T00:00:00Z" => 3246,
"2024-09-30T00:00:00Z" => 1577
Expand All @@ -119,7 +120,8 @@ defmodule SanbaseWeb.Graphql.WordsSocialVolumeApiTest do
|> Map.get("search_texts")

# Assert that the words are **not** lowercased before they are sent
assert search_texts == "eth OR nft,BTC"
assert search_texts ==
URI.encode_www_form("eth OR nft") <> "," <> URI.encode_www_form("BTC")

{:ok, resp}
end)
Expand Down