-
-
Save apeckham/f9a0f39e0841da41fcf3 to your computer and use it in GitHub Desktop.
Script to export helpscout data as json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'helpscout' | |
require 'fileutils' | |
require 'json' | |
api_key = ARGV[0] | |
helpscout = HelpScout::Client.new(api_key) | |
module InstanceVariablesToJson | |
def to_json(*a) | |
Hash[instance_variables.map { |name| [name.to_s[1..-1], instance_variable_get(name)] }].to_json(*a) | |
end | |
end | |
class Module | |
def all_the_modules | |
[self] + constants.map { |const| const_get(const) } | |
.select { |const| const.is_a? Module } | |
.reject { |const| const.included_modules.include? HTTParty } | |
.flat_map { |const| const.all_the_modules } | |
end | |
end | |
HelpScout.all_the_modules.select { |c| c.is_a? Class }.each do |c| | |
c.include InstanceVariablesToJson | |
end | |
helpscout.mailboxes.each do |box| | |
FileUtils.mkdir(box.name) unless File.exists?(box.name) | |
puts "Fetching #{helpscout.conversation_count(box.id, 'all', nil)} conversations for #{box.name}" | |
helpscout.conversations(box.id, "all", nil).each do |c| | |
next if File.exists?(path = "#{box.name}/#{c.id}.json") | |
File.open(path, "w") { |f| f.puts helpscout.conversation(c.id).to_json; putc "."; sleep 0.4 } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment