Skip to content

Instantly share code, notes, and snippets.

@jb41
Last active September 25, 2015 20:42
Show Gist options
  • Save jb41/52dcbf3a64d138ea37a7 to your computer and use it in GitHub Desktop.
Save jb41/52dcbf3a64d138ea37a7 to your computer and use it in GitHub Desktop.
Exporting Users table from Rails to Sheetsu
class TransferUsersToSpreadsheet
# Link to your
SHEETSU_API_URL = "put link to your API (from sheetsu.com) here"
# Fields to import
FIELDS_TO_IMPORT = [
'id', 'email', 'remember_created_at', 'sign_in_count', 'last_sign_in_at', 'name',
'slug', 'role', 'created_at', 'gender', 'age_range', 'birthday', 'location'
]
# Main method
def self.call
uri = URI.parse(SHEETSU_API_URL)
# Iterating over all users
User.all.each_with_index do |user, index|
# Get attributes
body = user.attributes.slice(*FIELDS_TO_IMPORT)
# Send request
send_post(uri, body)
puts "User #{user.email} saved"
# Don't kill API :)
sleep 2 if index % 5 == 0
end
end
private
# Method for sending requests
def self.send_post(uri, body)
Net::HTTP.new(uri.host, uri.port).start do |client|
request = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' })
request.body = body.to_json
response = client.request(request)
response
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment