Last active
June 24, 2016 10:04
-
-
Save raviwu/a0d1f9ce03afe59684cc48d78a346742 to your computer and use it in GitHub Desktop.
ParseInstallationWrapper Setup
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
# In app/services/parse_installation_wrapper.rb | |
class ParseInstallationWrapper | |
def self.check_and_unregister(options = {}) | |
# provide no params will clear out the Parse installation with no device_token | |
object_ids = get_push_registered_installation_object_ids(options) | |
delete_push_installations(object_ids) | |
end | |
private | |
def self.delete_push_installations(object_ids) | |
return false unless object_ids.kind_of? Array | |
results = [] | |
object_ids.each do |object_id| | |
parse_delete_installation_api_url = "https://api.parse.com/1/installations/#{object_id}" | |
uri = URI.parse(parse_delete_installation_api_url) | |
conn = Faraday.new(:url => uri) do |faraday| | |
faraday.request :url_encoded # Form-encode POST params. | |
faraday.response :logger # Log requests to STDOUT. | |
faraday.adapter :net_http # Make requests with Net::HTTP. | |
end | |
resp = conn.delete do |req| | |
req.url parse_delete_installation_api_url | |
req.headers["X-Parse-Application-Id"] = ParseConfig["ApplicationID"] | |
req.headers["X-Parse-Master-Key"] = ParseConfig["MasterKey"] | |
end | |
results << JSON.parse(resp.body) | |
end | |
results | |
end | |
def self.get_push_registered_installation_object_ids(options = {}) | |
request_params = { | |
"deviceToken" => options[:device_token] | |
} | |
object_ids = [] | |
device_token_check_result = check_push_installations(request_params) | |
object_ids.concat(device_token_check_result) if device_token_check_result.kind_of? Array | |
if options[:email].present? | |
request_params = { | |
"userId" => options[:email] | |
} | |
email_check_result = check_push_installations(request_params) | |
object_ids.concat(email_check_result) if email_check_result.kind_of? Array | |
end | |
object_ids | |
end | |
def self.check_push_installations(request_params) | |
parse_check_installation_api_url = "https://api.parse.com/1/installations" | |
uri = URI.parse(parse_check_installation_api_url) | |
conn = Faraday.new(:url => uri) do |faraday| | |
faraday.request :url_encoded # Form-encode POST params. | |
faraday.response :logger # Log requests to STDOUT. | |
faraday.adapter :net_http # Make requests with Net::HTTP. | |
end | |
resp = conn.get do |req| | |
req.url parse_check_installation_api_url | |
req.headers["X-Parse-Application-Id"] = ParseConfig["ApplicationID"] | |
req.headers["X-Parse-Master-Key"] = ParseConfig["MasterKey"] | |
req.params['where'] = request_params.to_json | |
end | |
result = JSON.parse(resp.body) | |
if result['results'].present? | |
result = result['results'].map { |i| i['objectId'] } | |
else | |
result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment