incremental_export.rb 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Sequencer::Unit::Import::Zendesk::Mixin::IncrementalExport
  3. attr_accessor :previous_page
  4. def self.included(base)
  5. base.uses :client
  6. base.provides :import_job
  7. end
  8. def resource_collection
  9. require 'zendesk_api' # Only load this gem when it is really used.
  10. @resource_collection ||= "::ZendeskAPI::#{resource_klass}".constantize.incremental_export(client, 1)
  11. end
  12. def resource_iteration
  13. super do |record|
  14. # call passed/originally intended block
  15. yield(record)
  16. # add hook to check if object (e.g. ticket) count
  17. # update is needed because the request
  18. # might have changed
  19. update_count
  20. end
  21. end
  22. # The source if this is the limitation of not knowing
  23. # how much objects there are in total before requesting the endpoint
  24. # This is caused by the Zendesk API which only returns max. 1000
  25. # per request
  26. def update_count
  27. update_import_job
  28. self.previous_page = current_page
  29. end
  30. def update_import_job
  31. return if !update_required?
  32. state.provide(:import_job, updated_import_job)
  33. end
  34. def klass_key
  35. @klass_key ||= resource_klass.pluralize.to_sym
  36. end
  37. def updated_import_job
  38. import_job.result[klass_key][:total] += current_request_count
  39. import_job
  40. end
  41. def update_required?
  42. # means: still on first page
  43. return false if current_page.blank?
  44. previous_page != current_page
  45. end
  46. def current_request_count
  47. # access the internal instance method of the
  48. # Zendesk collection request to get the current
  49. # count of the fetched result (max. 1000)
  50. resource_collection.fetch.size
  51. end
  52. def current_page
  53. # access the internal instance method of the
  54. # Zendesk collection request to get the current
  55. # page number of the endpoint
  56. resource_collection.instance_variable_get(:@query)
  57. end
  58. end