lib/embulk/input/zendesk/plugin.rb in embulk-input-zendesk-0.1.2 vs lib/embulk/input/zendesk/plugin.rb in embulk-input-zendesk-0.1.3

- old
+ new

@@ -56,16 +56,20 @@ # This is workaround for that if records.any? {|r| [Array, Hash].include?(r[hash[:name]].class) } hash[:type] = :json end - # NOTE: current version don't support JSON type - next if hash[:type] == :json - hash end + task[:includes].each do |ent| + columns << { + name: ent, + type: :json + } + end + return {"columns" => columns.compact} end def self.config_to_task(config) { @@ -79,10 +83,11 @@ start_time: config.param("start_time", :string, default: nil), retry_limit: config.param("retry_limit", :integer, default: 5), retry_initial_wait_sec: config.param("retry_initial_wait_sec", :integer, default: 1), incremental: config.param("incremental", :bool, default: true), schema: config.param(:columns, :array, default: []), + includes: config.param(:includes, :array, default: []), } end def init @start_time = Time.parse(task[:start_time]) if task[:start_time] @@ -93,12 +98,12 @@ args = [preview?] if !preview? && @start_time args << @start_time.to_i end - client = Client.new(task) last_data = client.public_send(method, *args) do |record| + record = fetch_related_object(record) values = extract_values(record) page_builder.add(values) end page_builder.finish @@ -113,9 +118,25 @@ return task_report end private + + def fetch_related_object(record) + (task[:includes] || []).each do |ent| + if preview? + # Fetching subresource consume ~2 sec for each record. it is too long to preview. so the dummy value used. + record[ent] = [{dummy: "(#{ent}) dummy value for preview"}] + else + record[ent] = client.fetch_subresource(record["id"], task[:target], ent) + end + end + record + end + + def client + Client.new(task) + end def preview? org.embulk.spi.Exec.isPreview() rescue java.lang.NullPointerException => e false