Skip to content

Instantly share code, notes, and snippets.

@resistorsoftware
Created January 19, 2012 21:07
Show Gist options
  • Select an option

  • Save resistorsoftware/1642615 to your computer and use it in GitHub Desktop.

Select an option

Save resistorsoftware/1642615 to your computer and use it in GitHub Desktop.
discount code
require 'csv'
require 'haddock'
require 'mechanize'
# codes = []
# Haddock::Password.diction = "words"
# 500.times do |x|
# code = Haddock::Password.generate(8)
# codes << code unless codes.include?(code)
# end
#
# puts "We generated #{codes.length} codes"
# CSV.open("./codes.csv", "wb") do |csv|
# codes.each {|c| csv << [c]}
# end
def enter_magic_land
agent = Mechanize.new
base = 'republicans-suck-dingo-farts.myshopify.com'
url = "https://#{base}/admin/auth/login"
page = agent.get(url)
form = page.forms.first
form.login = "dingo@balls.com"
form.password = "dingoballs"
agent.submit(page.forms.first)
page = agent.get("https://#{base}/admin/marketing")
csv = CSV.read("codes.csv")
csv.each do |code|
page.form_with(:action => "https://#{base}/admin/discounts") do |f|
f['discount[code]'] = code.first
discount_value_fields = f.fields_with(:id => 'discount_value')
discount_value_fields.each do |textfield|
textfield.value = 25.0
end
f["discount[starts_at]"] = Date.today.strftime('%F')
f["discount[ends_at]"] = Date.today.+(64).strftime('%F')
f["discount[minimum_order_amount]"] = 140.00
f["discount[usage_limit]"] = 1
f["type"] = 'fixed_amount'
end.click_button
end
end
enter_magic_land
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment