Last active
April 6, 2016 06:25
-
-
Save arukoh/c9800485805f9e0e94bd to your computer and use it in GitHub Desktop.
Test users migration script for Facebook App.
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
#!/bin/env ruby | |
require "koala" | |
require "highline/import" | |
def prompt_app_info_and_validate(id = nil, secret = nil) | |
id ||= ask("App ID: ") { |q| q.echo = true } | |
secret ||= ask("App Secret: ") { |q| q.echo = "*" } | |
begin | |
users = Koala::Facebook::TestUsers.new(app_id: id, secret: secret) | |
graph = Koala::Facebook::API.new(users.app_access_token) | |
{ name: graph.get_object(id)["name"], users: users, graph: graph } | |
rescue OAuthException => e | |
puts "[Error] Invalid App ID or Secret. #{e.message}" | |
exit 1 | |
rescue => e | |
puts "[Error] Some error has occurred. #{e.message}" | |
end | |
end | |
def start_migration?(message = 'Continue?') | |
answer = '' | |
choice = %w[y n a q] | |
until choice.include?(answer) | |
answer = ask("#{message} [#{choice.join("/")}] ") { |q| q.limit = 1; q.case = :downcase } | |
end | |
answer | |
end | |
# Get Facebook application information. | |
apps = { src: nil, dst: nil } | |
puts "Please enter the migration source of Facebook application information." | |
apps[:src] = prompt_app_info_and_validate | |
puts | |
puts "Please enter the migration destination of Facebook application information." | |
apps[:dst] = prompt_app_info_and_validate | |
puts | |
if apps[:src][:users].app_id == apps[:dst][:users].app_id | |
puts "[Completed] Same app_id is specified" | |
exit 0 | |
end | |
# Show migration information. | |
src_users = apps[:src][:users].list | |
dst_users = apps[:dst][:users].list | |
target_user_ids = src_users.map{|u| u["id"] } - dst_users.map{|u| u["id"] } | |
puts <<MIGRATION_INFO | |
========== Migration Info ========== | |
Migration source | |
app name:\t#{apps[:src][:name]} | |
user count:\t#{src_users.size} | |
Migration destination | |
app name:\t#{apps[:dst][:name]} | |
user count:\t#{dst_users.size} | |
Migration target user count is #{target_user_ids.size}. | |
=================================== | |
MIGRATION_INFO | |
if target_user_ids.size == 0 | |
puts "[Completed] Target users not found" | |
exit 1 | |
end | |
puts "TestUser migration will start.\n----" | |
mode = "y" | |
src_users.each do |user| | |
id = user["id"] | |
next unless target_user_ids.include?(id) | |
unless mode == "a" | |
mode = start_migration?("[Start][id=#{id}] User migration will start ?") | |
case mode | |
when "n" | |
puts "[Skip][id=#{id}] User migration has skipped." | |
next | |
when "q" | |
puts "[Canceled] Migration canceled." | |
exit 0 | |
end | |
end | |
begin | |
access_token = user["access_token"] | |
installed = !access_token.nil? | |
permissions = nil | |
if installed | |
token_data = apps[:src][:graph].get_object("debug_token", input_token: access_token) | |
permissions = token_data["data"]["scopes"].join(",") | |
end | |
apps[:dst][:users].create(installed, permissions, { uid: id, owner_access_token: apps[:src][:users].app_access_token }) | |
puts "[Success][id=#{id}] User migration has succeeded, installed=#{installed}, permissions=#{permissions}" | |
rescue => e | |
puts "[Error][id=#{id}] User migration has failed. #{e.message}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gemfile
Gemfile.lock