0

我正在尝试使用 ruby​​ gem 设置一个示例批量上传到 Keen.io。从他们的文档中:

Keen.publish_batch(
  :signups => [
    { :name => "Bob" },
    { :name => "Mary" }
  ],
  :purchases => [
    { :price => 10 },
    { :price => 20 }
  ]
)

我在 rake 任务中尝试生成类似的东西:

task backfill_keen_jobs: :environment do

   jobs_array = Array.new
   Job.last(5).each do |j|
      job_type = j.job_type.present? ? j.job_type.description : nil
      job = {
         :keen => {
            :timestamp => j.created_at.to_time.iso8601
         },
         :id => j.id,
         :title => j.title,
         :type => job_type,
         :company => j.company

      }

      jobs_array << JSON.generate(job)

   end
   puts jobs_array
   puts Keen.publish_batch(:jobs => [jobs_array.to_json])
end

我收到以下错误:

{"jobs"=>[{"success"=>false, "error"=>{"name"=>"InvalidPropertyNameError", "description"=>"An event should be a JSON object of properties."}}]}

我确定我只是以某种方式弄乱了 JSON 输出,但我似乎无法弄清楚如何/在哪里?

谢谢!

4

2 回答 2

1

如果您不在已创建的数组上调用 .to_json,这应该可以工作。

尝试,

Keen.publish_batch(:jobs => jobs_array)

与目前所说的相反

Keen.publish_batch(:jobs => [jobs_array.to_json])

希望这可以帮助!

于 2015-11-09T19:55:15.310 回答
0

这是答案,以防它对其他人有所帮助....

   jobs_array = Array.new
   Job.last(5).each do |j|
      job_type = j.job_type.present? ? j.job_type.description : nil
      job = {
         :keen => {
            :timestamp => j.created_at.to_time.iso8601
         },
         :id => j.id.to_i,
         :title => j.title,
         :type => job_type,
         :company => j.company
      }

      jobs_array << job

   end
   jobs_array
   Keen.publish_batch(:jobs => jobs_array)
于 2015-11-12T01:41:44.377 回答