Skip to content

Instantly share code, notes, and snippets.

@luizfonseca
Last active August 29, 2015 14:00
Show Gist options
  • Save luizfonseca/11166126 to your computer and use it in GitHub Desktop.
Save luizfonseca/11166126 to your computer and use it in GitHub Desktop.
matched_users = []
missing_users = []
matched_subscriptions = []
missing_subscriptions = []
@sheet = JSON.load(open('https://spreadsheets.google.com/feeds/list/0AksoO03hL7CRdGR2eDhmODQ5VVhoX0RBSVNoT1U2Vnc/od6/public/values?alt=json'))
@entries = @sheet['feed']['entry']
@entries.each do |entry|
email = entry['gsx$email']['$t']
user = User.find_by email: email
if user.present?
matched_users << {
id: user.id,
email: user.email,
}
if user.subscriptions.any?
matched_subscriptions << {
email: user.email,
code: user.subscriptions.last.code,
opt: user.subscriptions.last.payment_option,
state: user.subscriptions.last.state,
new_code: entry['gsx$codigo']['$t'],
new_state: entry['gsx$status']['$t'],
new_opt: entry['gsx$paymentoption']['$t'],
count_subscriptions: user.subscriptions.count
}
else
missing_subscriptions << { code: entry['gsx$codigo']['$t'], opt: entry['gsx$paymentoption']['$t'] }
end
else
missing_users << email
puts "Missing user: #{email}"
end
end
matched_subscriptions.each do |s|
user = User.find_by email: s[:email]
next if s[:count_subscriptions] != 1
next if s[:opt] == 'creditcard'
if s[:state] == 'active' && s[:new_state] == 'Autorizado'
next
elsif s[:state] == 'active' && s[:new_state] == 'Concluído'
next
elsif s[:state] == 'active' && s[:new_state] == 'Cancelado'
user.subscriptions.last.update_attribute :state, 'canceled'
elsif s[:state] == 'active' && s[:new_state] == 'Boleto Impresso'
user.subscriptions.last.update_attribute :state, 'waiting'
elsif s[:state] == 'active' && s[:new_state] == 'Iniciado'
user.subscriptions.last.update_attribute :state, 'waiting'
else
puts "#{user.email} + #{user.subscriptions.last.id} : Current state: #{s[:state]} -> New state: #{s[:new_state]} [#{user.subscriptions.last.created_at.strftime('%Y')}]"
puts "#{user.subscriptions.last.payments.inspect}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment