Skip to content

Instantly share code, notes, and snippets.

@marinados
Created February 25, 2016 14:38
Show Gist options
  • Save marinados/2d5404b78556f449fb83 to your computer and use it in GitHub Desktop.
Save marinados/2d5404b78556f449fb83 to your computer and use it in GitHub Desktop.
def change_token
next_token = tokens.rotate!.first
CapsuleCRM.configure do |config|
config.api_token = next_token
end
end
def get_classes
CapsuleCRM.constants.map do |sym|
CapsuleCRM.const_get(sym)
end
end
def process_objects
get_classes.each do |klass|
next unless klass.respond_to?(:all)
logger.info("Start export #{klass.to_s}")
name = klass.to_s.gsub('CapsuleCRM::', '') + '.json'
write_json(name: name, klass: klass)
extract_closed_tasks if klass = CapsuleCRM::Task
logger.info("Stop export #{klass.to_s}")
end
end
def write_json(name:, klass:)
file = create_file(name: name)
klass.all.each do |klass_member|
begin
file.write "#{klass_member.to_json}\n"
extract_info(klass_member)
rescue CapsuleCRM::Errors::InternalServerError
logger.info("Token switch")
change_token
extract_info(klass_member)
rescue => e
logger.error(e.message)
# logger.info("#{klass} #{klass_member.id}")
end
end
file.close
end
def create_file(name:)
File.open(name, 'a')
end
def extract_info(item)
extract_contacts(klass_member)
extract_additional_info(klass_member)
end
def extract_contacts(person)
return unless person.class == CapsuleCRM::Person
file = create_file(name: "Contacts.json")
contact_list = JSON.parse(person.contacts.to_json).delete_if { |_k, v| v.empty? }
contact_list.each do |_key, contact_type|
contact_type.each do |object|
object.merge!('person_id' => person.id)
end
end
file.write("#{contact_list.to_json}\n")
file.close
end
def extract_additional_info(item)
%i(histories tags custom_fields).each do |method|
next unless item.respond_to?(method)
file = create_file(name: "#{method.to_s.capitalize}.json")
item.send(method).each do |object|
file.write "#{object.to_json}\n"
end
file.close
end
end
def extract_closed_tasks
file = File.open('Task.json', 'a')
i = 0
size = 1000
begin
result = HTTParty.get("https://brandandcelebrities.capsulecrm.com/api/tasks?status=COMPLETED&start=#{i}&limit=1000",
basic_auth: {username: "#{token}", password: "x"},
headers: { 'Accept' => 'application/json' } )
if result['tasks']['task'].present?
result['tasks']['task'].each do |task|
file.write("#{task.to_json}\n")
end
size = result['tasks']['@size'].to_i
i += 1000
else
size = 0
end
end while size > 0
file.close
end
p CapsuleCRM::Case.all.first
p CapsuleCRM::Opportunity.all.first
# CapsuleImporter.new.process_objects
@yannvery
Copy link

When you call CapsuleCRM::Opportunity.all.first CapsuleCRM retrieves all opportunities ( a very long request ) and after get the first one.
However if you try CapsuleCRM::Opportunity.first, it'll respond quickly.

I made a test and called CapsuleCRM::Opportunity.all I got a response after 1 hour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment