Create a gist now

Instantly share code, notes, and snippets.

a script to help automate adding or updating discount codes for multiple events
#!/usr/bin/ruby
#
# This work is licensed under a Creative Commons Attribution 3.0 Unported License.
# http://creativecommons.org/licenses/by/3.0/
#
# With some slight modifications, this script should create
# a new discount code for each of the 'live' events which are
# owned by the user (who is identified by the user_key value).
#
# See the above license info and Eventbrite API terms for usage limitations.
#
# @ryanjarvinen / ryanj@eventbrite.com
#
# Resources:
# http://developer.eventbrite.com
# http://eventbrite.github.com
# http://developer.eventbrite.com/terms/
# http://developer.yahoo.com/ruby/ruby-rest.html
# http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html
#require our dependent gems
require 'net/http'
require 'uri'
require 'json'
# Set up our authentication tokens -
# for more info see: http://developer.eventbrite.com/doc/auth/
# set your API key (a.k.a. application key), available here: http://www.eventbrite.com/api/key/
# and your user_key - available here: http://www.eventbrite.com/userkeyapi
auth_parameters = {
#'app_key' => 'YOUR_APP_KEY_HERE',
#'user_key' => 'YOU_USER_KEY_HERE'
}
# define our basic API request paths -
# http://developer.eventbrite.com/doc/users/user_list_events/
user_list_events_path = '/json/user_list_events?'
# http://developer.eventbrite.com/doc/discount/discount_new/
discount_new_path = '/json/discount_new?'
# http://developer.eventbrite.com/doc/discount/discount_update/
discount_update_path = '/json/discount_update?'
# apply a filter to the user_list_events API request -
# find all the 'live' events owned by this user...
# Other available request parameters are described here:
# http://developer.eventbrite.com/doc/users/user_list_events/
user_list_events_parameters = {
'event_statuses' => 'live'
}
# skip over some events where the discount should not be available?
excluded_event_ids = [1575246602, 1575246603]
# Discount code details (same for each live event, please edit this to match your needs):
# see http://developer.eventbrite.com/doc/discount/discount_new/ for more info
discount_parameters = {
# specify the discount code:
'code' => 'early_bird',
# you must choose either amount_off OR percent_off, but not both
#'amount_off' => '5.00',
'percent_off' => '25.0',
# A comma-separated list of ticket_ids to apply the discount to.
# If this is left blank, the discount will apply to all tickets in this event
#'tickets' => '',
# Maximum number of times that this discount can be used. if omitted, there will be no limit.
#'quantity_available' => '',
# The discount start date and time
#'start_date' => ,
# The discount end date and time
#'end_date' => ''
}
#initialize our http object, for contacting the API
http = Net::HTTP.new('www.eventbrite.com', 443)
http.use_ssl=true
#make our API request -
print "\nFinding events...\n"
list_events_params = auth_parameters.update(user_list_events_parameters).collect {|k, v| "#{k}=#{v}"}.join('&')
response = http.request_get( URI.escape( user_list_events_path + list_events_params ))
#parse our JSON response data...
events_list = JSON.parse( response.body )
discount_params = auth_parameters.update(discount_parameters).collect {|k, v| "#{k}=#{v}"}.join('&')
print "\nApplying discounts...\n"
for event in events_list['events']
if excluded_event_ids && !excluded_event_ids.include?( event['event']['id'] )
# adding a new discount code to the following event:
print "\nAdding discount for event: \"" + event['event']['title'] + "\", event_id: " + event['event']['id'].to_s + "\n"
response = http.request_get( URI.escape( discount_new_path + discount_params + "&event_id="+ event['event']['id'].to_s ))
print JSON.parse(response.body)
print "\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment