Last active
May 19, 2016 14:50
-
-
Save bre7/7099ebaf4499bf85717736207e28af4c to your computer and use it in GitHub Desktop.
Script to export Parse.com's data as JSON.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rest-client' | |
require 'json' | |
require 'colorize' | |
# USAGE: Add your AppId and Master Key. Execute script (Custom classes are parsed from Schema). | |
# WARNING: PAGINATION IS MISSING. | |
ParseApiBaseUrl = "https://api.parse.com" | |
Headers = { | |
"X-Parse-Application-Id" => "<INSERT_APP_ID_HERE>", | |
"X-Parse-Master-Key" => "<INSERT_MASTER_KEY_HERE>", | |
:content_type => :json, | |
:accept => :json | |
} | |
def getEndpointData(endpointName, reserved = false, prettyPrinted = true) | |
endpointRelativeUrl = reserved ? "/1/#{endpointName.downcase}" : "/1/classes/#{endpointName}" | |
begin | |
RestClient::Request.execute( | |
method: :get, | |
url: ParseApiBaseUrl + endpointRelativeUrl, | |
headers: Headers | |
) { |response, request, result, &block| | |
if reserved | |
puts " Parsing #{endpointName} @ #{endpointRelativeUrl}" | |
else | |
puts "Parsing #{endpointName} @ #{endpointRelativeUrl}" | |
end | |
jsonResponse = JSON.parse(response) | |
if jsonResponse.has_key?("error") | |
raise jsonResponse["error"] | |
end | |
jsonResponseToSave = prettyPrinted ? JSON.pretty_generate(jsonResponse) : jsonResponse | |
filename = endpointName.end_with?("s") ? "#{endpointName}.json" : "#{endpointName}.json" | |
File.open(filename, 'w') { |file| | |
file.write(jsonResponseToSave) | |
file.close | |
} | |
if reserved | |
puts " File saved: #{filename}".green | |
else | |
puts " File saved: #{filename}".green | |
end | |
if endpointName == "Schemas" | |
classes = jsonResponse["results"] | |
.map { |val| val["className"] } | |
.select { |item| item.start_with?("_") === false } | |
puts " _Schema parsed. Classes found: #{classes}".blue | |
classes.each { |val| | |
getEndpointData(val, false) | |
} | |
end | |
} | |
rescue => e | |
puts " Error: #{e}".red | |
end | |
end | |
start = Time.now | |
puts "\n\nParsing Parse.com's reserved endpoints:" | |
[ "Sessions", "Users", "Roles", "Installations", "Schemas" ] | |
.each { |value| getEndpointData(value, true) } | |
puts "\n" | |
puts "Elapsed: #{Time.now - start}s".magenta |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment