Skip to content

Commit

Permalink
Merge pull request #17 from debtcollective/od/algolia-fix
Browse files Browse the repository at this point in the history
fix: make sure we handle empty values correctly
  • Loading branch information
orlando authored Feb 22, 2021
2 parents 8a2c97c + c6d4238 commit 957a31f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
121 changes: 75 additions & 46 deletions lib/algolia_places_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,84 @@

module Debtcollective
class AlgoliaPlacesClient
# Returns JSON response or nil
def self.query(query, options = { type: "address", "restrictSearchableAttributes": "postcode", hitsPerPage: 1 })
payload = { query: query }.merge(options)
api_url = 'https://places-dsn.algolia.net/1/places/query'

response = Net::HTTP.post(
URI(api_url),
payload.to_json,
self.headers
)

case response
when Net::HTTPSuccess then
success_response(response)
else
Raven.capture_message("Error while making Algolia Places request", extra: { status: response.status, body: response.body }) if defined?(Raven)

nil
class << self
QUERY_DEFAULT_OPTIONS = {type: "address", "restrictSearchableAttributes": "postcode", hitsPerPage: 1}

# Returns JSON response or nil
def query(query, options = QUERY_DEFAULT_OPTIONS)
options = QUERY_DEFAULT_OPTIONS.merge(options)
payload = {query: query}.merge(options)
api_url = "https://places-dsn.algolia.net/1/places/query"

response = Net::HTTP.post(
URI(api_url),
payload.to_json,
headers
)

case response
when Net::HTTPSuccess
success_response(response)
else
Raven.capture_message("Error while making Algolia Places request", extra: {status: response.status, body: response.body}) if defined?(Raven)

nil
end
end
end

private

def self.success_response(response)
body = response.body
json = JSON.parse(body)

# We return the first result
result = json['hits'].first

{
city: result['city']['default'].first,
country: result['country']['default'],
country_code: result['country_code'],
county: result['county']['default'].first,
objectID: result['objectID'],
geoloc: result['_geoloc'],
postcodes: result['postcode'],
state: result['administrative'].first
}.with_indifferent_access
end
private

def success_response(response)
body = response.body
json = JSON.parse(body)

# Algolia limits some queries due server capacity
# This probably is related to the upcoming deprecation of Places
# https://www.algolia.com/blog/product/sunseting-our-places-feature/
degraded_query = json["degradedQuery"]
result = json["hits"].first

def self.headers
{
accept: "application/json",
"Content-Type": "application/json",
"X-Algolia-Application-Id": SiteSetting.debtcollective_algolia_app_id,
"X-Algolia-API-Key": SiteSetting.debtcollective_algolia_api_key
}
if degraded_query && result.blank?
return {degraded_query: true}.with_indifferent_access
end

# no results, return gracefully
return if result.blank?

# Algolia return postcode results as an array of string or as an array of objects
# we handle the two cases here.
#
# Also, the value can return unsanitized HTML
postcodes = result["postcode"]

postcodes = postcodes.map do |postcode|
if postcode.is_a?(Hash)
postcode = ActionView::Base.full_sanitizer.sanitize(postcode["value"])
else
postcode
end
end

{
city: result["city"]&.[]("default")&.first,
country: result["country"]&.[]("default"),
country_code: result["country_code"],
county: result["county"]&.[]("default")&.first,
objectID: result["objectID"],
geoloc: result["_geoloc"],
postcodes: postcodes,
state: result["administrative"]&.first
}.with_indifferent_access
end

def headers
{
accept: "application/json",
"Content-Type": "application/json",
"X-Algolia-Application-Id": SiteSetting.debtcollective_algolia_app_id,
"X-Algolia-API-Key": SiteSetting.debtcollective_algolia_api_key
}
end
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/algolia_places_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
it "returns search using zip code" do
# Response from the Algolia places API
query_response = <<-END
{"hits":[{"country":{"de":"Vereinigte Staaten von Amerika","ru":"Соединённые Штаты Америки","pt":"Estados Unidos da América","it":"Stati Uniti d'America","fr":"États-Unis d'Amérique","hu":"Amerikai Egyesült Államok","es":"Estados Unidos de América","zh":"美国","ar":"الولايات المتّحدة الأمريكيّة","default":"United States of America","ja":"アメリカ合衆国","pl":"Stany Zjednoczone Ameryki","ro":"Statele Unite ale Americii","nl":"Verenigde Staten van Amerika"},"is_country":false,"city":{"ar":["سان فرانسيسكو"],"default":["San Francisco","SF"],"ru":["Сан-Франциско"],"pt":["São Francisco"],"ja":["サンフランシスコ"],"zh":["旧金山"]},"is_highway":true,"importance":26,"_tags":["highway","highway/residential","country/us","address","source/osm"],"postcode":["94118","94115"],"county":{"default":["San Francisco City and County","San Francisco","SF"],"ru":["Сан-Франциско"],"zh":["旧金山市县"]},"population":864816,"country_code":"us","is_city":false,"is_popular":false,"administrative":["California"],"admin_level":15,"is_suburb":false,"locale_names":{"default":["Anzavista Avenue"]},"_geoloc":{"lat":37.7795,"lng":-122.444},"objectID":"170678228_399595260","_highlightResult":{"country":{"de":{"value":"Vereinigte Staaten von Amerika","matchLevel":"none","matchedWords":[]},"ru":{"value":"Соединённые Штаты Америки","matchLevel":"none","matchedWords":[]},"pt":{"value":"Estados Unidos da América","matchLevel":"none","matchedWords":[]},"it":{"value":"Stati Uniti d'America","matchLevel":"none","matchedWords":[]},"fr":{"value":"États-Unis d'Amérique","matchLevel":"none","matchedWords":[]},"hu":{"value":"Amerikai Egyesült Államok","matchLevel":"none","matchedWords":[]},"es":{"value":"Estados Unidos de América","matchLevel":"none","matchedWords":[]},"zh":{"value":"美国","matchLevel":"none","matchedWords":[]},"ar":{"value":"الولايات المتّحدة الأمريكيّة","matchLevel":"none","matchedWords":[]},"default":{"value":"United States of America","matchLevel":"none","matchedWords":[]},"ja":{"value":"アメリカ合衆国","matchLevel":"none","matchedWords":[]},"pl":{"value":"Stany Zjednoczone Ameryki","matchLevel":"none","matchedWords":[]},"ro":{"value":"Statele Unite ale Americii","matchLevel":"none","matchedWords":[]},"nl":{"value":"Verenigde Staten van Amerika","matchLevel":"none","matchedWords":[]}},"city":{"ar":[{"value":"سان فرانسيسكو","matchLevel":"none","matchedWords":[]}],"default":[{"value":"San Francisco","matchLevel":"none","matchedWords":[]},{"value":"SF","matchLevel":"none","matchedWords":[]}],"ru":[{"value":"Сан-Франциско","matchLevel":"none","matchedWords":[]}],"pt":[{"value":"São Francisco","matchLevel":"none","matchedWords":[]}],"ja":[{"value":"サンフランシスコ","matchLevel":"none","matchedWords":[]}],"zh":[{"value":"旧金山","matchLevel":"none","matchedWords":[]}]},"postcode":[{"value":"94118","matchLevel":"none","matchedWords":[]},{"value":"<em>94115</em>","matchLevel":"full","fullyHighlighted":true,"matchedWords":["94115"]}],"county":{"default":[{"value":"San Francisco City and County","matchLevel":"none","matchedWords":[]},{"value":"San Francisco","matchLevel":"none","matchedWords":[]},{"value":"SF","matchLevel":"none","matchedWords":[]}],"ru":[{"value":"Сан-Франциско","matchLevel":"none","matchedWords":[]}],"zh":[{"value":"旧金山市县","matchLevel":"none","matchedWords":[]}]},"administrative":[{"value":"California","matchLevel":"none","matchedWords":[]}],"locale_names":{"default":[{"value":"Anzavista Avenue","matchLevel":"none","matchedWords":[]}]}}}],"nbHits":1,"processingTimeMS":27,"query":"94115","params":"query=94115&type=address&restrictSearchableAttributes=postcode&hitsPerPage=1","degradedQuery":false}
{"hits":[{"country":{"de":"Vereinigte Staaten von Amerika","ru":"Соединённые Штаты Америки","pt":"Estados Unidos da América","it":"Stati Uniti d'America","fr":"États-Unis d'Amérique","hu":"Amerikai Egyesült Államok","es":"Estados Unidos de América","zh":"美国","ar":"الولايات المتّحدة الأمريكيّة","default":"United States of America","ja":"アメリカ合衆国","pl":"Stany Zjednoczone Ameryki","ro":"Statele Unite ale Americii","nl":"Verenigde Staten van Amerika"},"is_country":false,"city":{"ar":["سان فرانسيسكو"],"default":["San Francisco","SF"],"ru":["Сан-Франциско"],"pt":["São Francisco"],"ja":["サンフランシスコ"],"zh":["旧金山"]},"is_highway":true,"importance":26,"_tags":["highway","highway/residential","country/us","address","source/osm"],"postcode":["94118","94115", {"value": "94115"}],"county":{"default":["San Francisco City and County","San Francisco","SF"],"ru":["Сан-Франциско"],"zh":["旧金山市县"]},"population":864816,"country_code":"us","is_city":false,"is_popular":false,"administrative":["California"],"admin_level":15,"is_suburb":false,"locale_names":{"default":["Anzavista Avenue"]},"_geoloc":{"lat":37.7795,"lng":-122.444},"objectID":"170678228_399595260","_highlightResult":{"country":{"de":{"value":"Vereinigte Staaten von Amerika","matchLevel":"none","matchedWords":[]},"ru":{"value":"Соединённые Штаты Америки","matchLevel":"none","matchedWords":[]},"pt":{"value":"Estados Unidos da América","matchLevel":"none","matchedWords":[]},"it":{"value":"Stati Uniti d'America","matchLevel":"none","matchedWords":[]},"fr":{"value":"États-Unis d'Amérique","matchLevel":"none","matchedWords":[]},"hu":{"value":"Amerikai Egyesült Államok","matchLevel":"none","matchedWords":[]},"es":{"value":"Estados Unidos de América","matchLevel":"none","matchedWords":[]},"zh":{"value":"美国","matchLevel":"none","matchedWords":[]},"ar":{"value":"الولايات المتّحدة الأمريكيّة","matchLevel":"none","matchedWords":[]},"default":{"value":"United States of America","matchLevel":"none","matchedWords":[]},"ja":{"value":"アメリカ合衆国","matchLevel":"none","matchedWords":[]},"pl":{"value":"Stany Zjednoczone Ameryki","matchLevel":"none","matchedWords":[]},"ro":{"value":"Statele Unite ale Americii","matchLevel":"none","matchedWords":[]},"nl":{"value":"Verenigde Staten van Amerika","matchLevel":"none","matchedWords":[]}},"city":{"ar":[{"value":"سان فرانسيسكو","matchLevel":"none","matchedWords":[]}],"default":[{"value":"San Francisco","matchLevel":"none","matchedWords":[]},{"value":"SF","matchLevel":"none","matchedWords":[]}],"ru":[{"value":"Сан-Франциско","matchLevel":"none","matchedWords":[]}],"pt":[{"value":"São Francisco","matchLevel":"none","matchedWords":[]}],"ja":[{"value":"サンフランシスコ","matchLevel":"none","matchedWords":[]}],"zh":[{"value":"旧金山","matchLevel":"none","matchedWords":[]}]},"postcode":[{"value":"94118","matchLevel":"none","matchedWords":[]},{"value":"<em>94115</em>","matchLevel":"full","fullyHighlighted":true,"matchedWords":["94115"]}],"county":{"default":[{"value":"San Francisco City and County","matchLevel":"none","matchedWords":[]},{"value":"San Francisco","matchLevel":"none","matchedWords":[]},{"value":"SF","matchLevel":"none","matchedWords":[]}],"ru":[{"value":"Сан-Франциско","matchLevel":"none","matchedWords":[]}],"zh":[{"value":"旧金山市县","matchLevel":"none","matchedWords":[]}]},"administrative":[{"value":"California","matchLevel":"none","matchedWords":[]}],"locale_names":{"default":[{"value":"Anzavista Avenue","matchLevel":"none","matchedWords":[]}]}}}],"nbHits":1,"processingTimeMS":27,"query":"94115","params":"query=94115&type=address&restrictSearchableAttributes=postcode&hitsPerPage=1","degradedQuery":false}
END

stub_request(:post, "https://places-dsn.algolia.net/1/places/query").
Expand Down

0 comments on commit 957a31f

Please sign in to comment.