Skip to content

Instantly share code, notes, and snippets.

@sulf
Created November 3, 2012 00:47
Show Gist options
  • Save sulf/4005289 to your computer and use it in GitHub Desktop.
Save sulf/4005289 to your computer and use it in GitHub Desktop.
Parse relation generator command line client
#!/usr/bin/env ruby
# Copyright (c) 2012 Ulf Schwekendiek (sulf@me.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'optparse'
# This hash will hold all of the options
# parsed from the command-line by
# OptionParser.
options = {}
optparse = OptionParser.new do |opts|
# Set a banner, displayed at the top
# of the help screen.
opts.banner = "Usage: add_parse_relation.rb options\n\nExample: add_parse_relation.rb -s Category -i RFGRk1kJDM -r vendors -t Vendor -o BWw44qhcl2\n\n"
# Define the options, and what they do
options[:source_class] = nil
opts.on( '-s', '--source_class CLASS', 'Set the source CLASS for you relation' ) do |source_class|
options[:source_class] = source_class
end
options[:target_class] = nil
opts.on( '-t', '--target_class CLASS', 'Set the target CLASS for you relation' ) do |target_class|
options[:target_class] = target_class
end
options[:relation_name] = nil
opts.on( '-r', '--relation_name NAME', 'Set the source object relation NAME' ) do |relation_name|
options[:relation_name] = relation_name
end
options[:source_object_id] = nil
opts.on( '-i', '--source_object_id ID', 'Set the source object ID for your source class object' ) do |source_object_id|
options[:source_object_id] = source_object_id
end
options[:target_object_id] = nil
opts.on( '-o', '--target_object_id ID', 'Set the target object ID for your target class object' ) do |target_object_id|
options[:target_object_id] = target_object_id
end
options[:application_id] = nil
opts.on( '-a', '--application_id ID', 'Set the parse application ID' ) do |application_id|
options[:application_id] = application_id
end
options[:rest_api_key] = nil
opts.on( '-k', '--rest_api_key API_KEY', 'Set the parse API_KEY' ) do |rest_api_key|
options[:rest_api_key] = rest_api_key
end
# This displays the help screen, all programs are
# assumed to have this option.
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
# Parse the command-line. Remember there are two forms
# of the parse method. The 'parse' method simply parses
# ARGV, while the 'parse!' method parses ARGV and removes
# any options found there, as well as any parameters for
# the options. What's left is the list of files to resize.
optparse.parse!
# Example
# =======
# add_parse_relation.rb -s Category -i RFGRk1kJDM -r vendors -t Vendor -o BWw44qhcl2 -k kp4koVNHABeMe8v8CVKA9yRDSpLrOrrGJkjJ9Jri -a Ln1wRxMKmeH9Qkq8jKnGcUSP2l5kRTzrbvY3wAnc
puts `curl -X PUT -H "X-Parse-Application-Id: #{options[:application_id]}" -H "X-Parse-REST-API-Key: #{options[:rest_api_key]}" -H "Content-Type: application/json" -d '{"#{options[:relation_name]}":{"__op":"AddRelation","objects":[{"__type":"Pointer","className":"#{options[:target_class]}","objectId":"#{options[:target_object_id]}"}]}}' https://api.parse.com/1/classes/#{options[:source_class]}/#{options[:source_object_id]}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment