Skip to content

Instantly share code, notes, and snippets.

@seaneshbaugh
Created August 26, 2020 20:24
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 seaneshbaugh/4cc8e035a00dcdabf9292114490076ac to your computer and use it in GitHub Desktop.
Save seaneshbaugh/4cc8e035a00dcdabf9292114490076ac to your computer and use it in GitHub Desktop.
Applebees per capita
# frozen_string_literal: true
require 'fileutils'
require 'json'
require 'pp'
# https://simple.wikipedia.org/wiki/List_of_U.S._states_by_population
# See July 1, 2019 estimate
STATES = {
'AL' => 4_887_871,
'AK' => 737_438,
'AZ' => 7_278_717,
'AR' => 3_013_825,
'CA' => 39_512_223,
'CO' => 5_695_564,
'CT' => 3_572_665,
'DE' => 967_171,
'FL' => 21_477_737,
'GA' => 10_617_423,
'HI' => 1_420_491,
'ID' => 1_754_208,
'IL' => 12_671_821,
'IN' => 6_691_878,
'IA' => 3_156_145,
'KS' => 2_911_505,
'KY' => 4_468_402,
'LA' => 4_659_978,
'ME' => 1_338_404,
'MD' => 6_042_718,
'MA' => 6_902_149,
'MI' => 9_986_857,
'MN' => 5_611_179,
'MS' => 2_986_530,
'MO' => 6_126_452,
'MT' => 1_062_305,
'NE' => 1_929_268,
'NV' => 3_034_392,
'NH' => 1_356_458,
'NJ' => 8_882_190,
'NM' => 2_095_428,
'NY' => 19_453_561,
'NC' => 10_488_084,
'ND' => 760_077,
'OH' => 11_689_442,
'OK' => 3_943_079,
'OR' => 4_190_713,
'PA' => 12_801_989,
'RI' => 1_057_315,
'SC' => 5_084_127,
'SD' => 882_235,
'TN' => 6_770_010,
'TX' => 29_206_997,
'UT' => 3_205_958,
'VT' => 626_299,
'VA' => 8_535_519,
'WA' => 7_614_893,
'WV' => 1_805_832,
'WI' => 5_813_568,
'WY' => 577_737,
}
curl_command = "curl 'https://www.applebees.com/api/sitecore/Locations/LocationSearchAsync' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:71.0) Gecko/20100101 Firefox/71.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Origin: https://www.applebees.com' -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Referer: https://www.applebees.com/en/restaurants?searchQuery=TX' -H 'Cookie: __cfduid=dfc21c1cde46638f7d724e6d3da32988e1580839968; AWSALB=9oAuVBR1yW3a7L/06+e8bD/13HSIX8RKIKMomeGNDxmTZ6GH92Ag8tFapnyel2WcHVx7L0clf4LnIlFR2R7ozeB8dXc4HKRSCxCM/lLmLDcZcvsrdcJBMJwYTpZr; AWSALBCORS=9oAuVBR1yW3a7L/06+e8bD/13HSIX8RKIKMomeGNDxmTZ6GH92Ag8tFapnyel2WcHVx7L0clf4LnIlFR2R7ozeB8dXc4HKRSCxCM/lLmLDcZcvsrdcJBMJwYTpZr; applebees#lang=en; ASP.NET_SessionId=hrksv30kwa3xqvri3ue04fi5; SC_ANALYTICS_GLOBAL_COOKIE=b5ca352a99bc4080893bff189902f4a3|False; HamburgerTest=Limited-With-Sub-Categories; menu-sort-test-1-2=positioning-override; CrossSellGuest=PreCheckout|1; __RequestVerificationToken=lGtt2N4U51ktQkrCWVGeWucjc5x_0RLKSsnle7j6Ts2qDU54cUONcDmDGBNWL665F3FBWQM0Fw0ybORR1wH94ztKK1CA4E6pVUaphKXPbOo1; ClientTime=12:14:20 PM; my-location=2a1cd2d2-8547-4f99-b658-d1630caee964|22205|CT|||False|False|80033|True; ADRUM_BTa=R:55|g:1e598474-65ce-4d51-beed-3eba6ac46ba3|n:DineEquityProd_99851457-4bb1-43c1-8014-65c9d9d9b004; ADRUM_BT1=R:55|i:3696806|e:57' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' -H 'TE: Trailers' --data 'ResultsPage=%2Flocations%2Fresults&LocationRoot=%7B3B216C7F-CB81-4126-A2CE-33AD79B379CF%7D&NumberOfResults=10000&LoadResultsForCareers=False&MaxDistance=5000&UserLatitude=&UserLongitude=&SearchQuery=TX&__RequestVerificationToken=RzstNlY8gbfFXvOtXU7Dhc-c0uZfwC2tAbDq37cqcQEJF_yS-Cu51iRmvmvj8-U_2IyFnaIYD-lTc4V03j5EO6PU0G6zaQ-SJC9dJmBuOzM1'"
cache_directory = File.expand_path(File.join('.', 'tmp'), __dir__)
FileUtils.mkdir_p(cache_directory)
cache_file = File.join(cache_directory, 'locations.json')
cached_response = File.read(cache_file) if File.exist?(cache_file)
response = if cached_response
cached_response
else
`#{curl_command}`
end
unless cached_response
File.open(cache_file, 'w') do |f|
f.write response
end
end
parsed_response = JSON.parse(response)
locations = parsed_response['Locations'].group_by { |location| location['Location']['State'] }
applebees_per_capita = STATES.map do |state, population|
[state, population, locations[state].length, locations[state].length.to_f / population.to_f]
end
pp applebees_per_capita
puts
puts applebees_per_capita.max_by(&:last).inspect
puts applebees_per_capita.min_by(&:last).inspect
puts applebees_per_capita.max_by { |l| l[2] }.inspect
# puts sprintf("%.20f", applebees_per_capita.max_by(&:last).last).sub(/0+$/, '').sub(/\.$/, '.0')
puts parsed_response['Locations'].length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment