2

I'm having a surprising amount of difficulty saving data imported from Zendesk's API into a rails DB. I'm using the following code to iterate and create users; while the data is present in the file, nothing is being saved. This is the code I'm using in my User model to create records that is Not working when I run User.save_data_from_apiin my console:

class User < ApplicationRecord

def self.save_data_from_api
uri = URI.parse("https://techcompany.zendesk.com/api/v2/users.json")
request = Net::HTTP::Get.new(uri)
request.basic_auth("adminname@blah.com", "properpassword")

req_options = {
use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
info = response.body
user_data = JSON.parse(info)

users = user_data.map.first do |line|
    u = User.new
    u.zendesk_id = line['users']['id']
    u.save
    u
end

end

end

Here is an example of the JSON file I'm working with:

    ["users", [{"id"=>333653850, 
    "url"=>"https://randomtechco.zendesk.com/api/v2/users/333653850.json", 
    "name"=>"Randy Buckles", "email"=>"rbuckles@randotechco.com", 
    "created_at"=>"2014-08-06T14:31:24Z", "updated_at"=>"2018-04- 
    04T14:22:06Z", "time_zone"=>"Pacific Time (US & Canada)", "phone"=>nil, 
    "shared_phone_number"=>nil, "photo"=> 
 {"url"=>"https://randomtechco.zendesk.com/api/v2/attachments/68955389.jso  ", "id"=>68955389, "file_name"=>"Work.jpg", "content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work.jpg", "mapped_content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work.jpg", "content_type"=>"image/jpeg", "size"=>2528, "width"=>80, "height"=>80, "inline"=>false, "thumbnails"=> 

 [{"url"=>"https://randomtechco.zendesk.com/api/v2/attachments/68955399.json", "id"=>68955399, "file_name"=>"Work_thumb.jpg", "content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work_thumb.jpg", "mapped_content_url"=>"https://randomtechco.zendesk.com/system/photos/6895/5389/Work_thumb.jpg", "content_type"=>"image/jpeg", "size"=>2522, "width"=>32, "height"=>32, "inline"=>false}]}, "locale_id"=>1, "locale"=>"en-US", "organization_id"=>nil, "role"=>"admin", "verified"=>true, "external_id"=>nil, "tags"=>[], "alias"=>"", "active"=>true, "shared"=>false, "shared_agent"=>false, "last_login_at"=>"2018-04-04T14:21:44Z", "two_factor_auth_enabled"=>nil, "signature"=>"Thanks for contacting the helpdesk!\n-Randy", "details"=>"", "notes"=>"", "role_type"=>nil, "custom_role_id"=>nil, "moderator"=>true, "ticket_restriction"=>nil, "only_private_comments"=>false, "restricted_agent"=>false, "suspended"=>false, "chat_only"=>false, "default_group_id"=>21692179, "user_fields"=>{}}, {"id"=>333666109, "url"=>"https://randomtechco.zendesk.com/api/v2/users/333666100.json", "name"=>"Caller +1 (800) 446-9635", "email"=>nil, "created_at"=>"2014-08-06T14:56:02Z", "updated_at"=>"2014-08-06T14:56:02Z", "time_zone"=>"Pacific Time (US & Canada)", "phone"=>"+18004469635", "shared_phone_number"=>false, "photo"=>nil, "locale_id"=>1, "locale"=>"en-US", "organization_id"=>nil, "role"=>"end-user", "verified"=>true, "external_id"=>nil, "tags"=>[], "alias"=>nil, "active"=>true, "shared"=>false, "shared_agent"=>false, "last_login_at"=>nil, "two_factor_auth_enabled"=>false, "signature"=>nil, "details"=>nil, "notes"=>nil, "role_type"=>nil, "custom_role_id"=>nil, "moderator"=>false, "ticket_restriction"=>"requested", "only_private_comments"=>false, "restricted_agent"=>true, "suspended"=>false, "chat_only"=>false, "default_group_id"=>nil, "user_fields"=>{}}
4

1 回答 1

2

问题是map.first。当map在没有块的情况下调用它时,它会返回一个枚举器(https://ruby-doc.org/core-2.2.0/Array.html#method-i-map)。

因此,当您调用.firstmap它返回的枚举器时,它会返回枚举器的第一个元素,并且该块被简单地忽略,因为first不需要一个块。

为了使您的代码执行,删除.first,你会很好去;)

于 2018-04-04T18:37:48.407 回答