Skip to content

Instantly share code, notes, and snippets.

@cw2908
Last active September 26, 2019 17:01
Show Gist options
  • Save cw2908/257d7c0ea1ca870ff733d45b58606b61 to your computer and use it in GitHub Desktop.
Save cw2908/257d7c0ea1ca870ff733d45b58606b61 to your computer and use it in GitHub Desktop.
Simple Script for exporting sites to csv
## To run: 'gem install samanage' then 'ruby simple_site_export.rb API_TOKEN' or replace 12345 below with your token
require 'samanage'
token = ARGV[0] || '12345'
@samanage = Samanage::Api.new(token: token)
def log_to_csv(row: , filename: 'Sites Export.csv')
write_headers = !File.exists?(filename)
CSV.open(filename, 'a+', write_headers: write_headers, force_quotes: true, headers: row.keys) do |csv|
csv << row.values
end
end
# This @samanage.sites method just calls api.samanage.com/sites.json?page=1
# then page=2 up to the total page number
@samanage.sites do |site|
puts "Writing: #{site['name']}"
log_to_csv(row: site)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment