Search code examples
ruby-on-railsjsondatabaseimportzendesk-api

Saving Data from JSON api to Ruby on Rails DB


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("[email protected]", "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"=>"[email protected]", 
    "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"=>{}}

Solution

  • The problem is map.first. When map is called without a block it returns an Enumerator (https://ruby-doc.org/core-2.2.0/Array.html#method-i-map).

    Thus, when you call .first on enumerator returned by map it returns the first element of the enumerator and the block is simply ignored since first doesn't expect a block.

    In order to make your code execute, remove the .first and you'll be good to go ;)