Skip to content

Instantly share code, notes, and snippets.

@kitwalker12
Created March 25, 2015 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitwalker12/d5f4f896f56a9e2951a3 to your computer and use it in GitHub Desktop.
Save kitwalker12/d5f4f896f56a9e2951a3 to your computer and use it in GitHub Desktop.
Algolia Rest API
class AlgoliaApi
include HTTParty
def initialize(type = 'analytics')
self.class.base_uri self.class.send("#{type}_base")
@auth = {
'X-Algolia-API-KEY' => ENV['ALGOLIASEARCH_API_KEY'],
'X-Algolia-Application-Id' => ENV['ALGOLIASEARCH_APPLICATION_ID']
}
end
def self.analytics_base
'https://analytics.algolia.com/1'
end
def self.indexes_base
"https://#{ENV['ALGOLIASEARCH_APPLICATION_ID']}.algolia.io/1/indexes"
end
def self.keys_base
"https://#{ENV['ALGOLIASEARCH_APPLICATION_ID']}.algolia.io/1/keys"
end
def self.logs_base
"https://#{ENV['ALGOLIASEARCH_APPLICATION_ID']}.algolia.io/1/logs"
end
def popular(index, options = { size: 5 })
result = self.class.get("/searches/#{index}/popular", headers: @auth, query: options)
if result.success?
return result.fetch("topSearches", []).map{ |q| q['query'] }
else
return []
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment