Skip to content

Instantly share code, notes, and snippets.

@apeckham
Forked from stympy/helpscout-export.rb
Last active December 7, 2017 14:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apeckham/f9a0f39e0841da41fcf3 to your computer and use it in GitHub Desktop.
Save apeckham/f9a0f39e0841da41fcf3 to your computer and use it in GitHub Desktop.
Script to export helpscout data as json
#!/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