我编写了一个脚本,用于从api导入分页JSON数据,将数据编码为UTF-8,然后将其写入文件。
我需要迭代文件中的数据以在我的数据库中创建记录,但是我正在运行加载文件的错误,所以我可以这样做。
堆栈跟踪并没有告诉我什么是错的;任何帮助将不胜感激!
代码:
require 'net/http'
require 'uri'
require 'json'
require 'faker'
#imports APR User data from the zendesk api, iterates through it and populates the database with it.
uri = URI.parse("https://aprtechsupport.zendesk.com/api/v2/users.json")
request = Net::HTTP::Get.new(uri)
request.content_type = "application/json"
request.basic_auth("cbradford@blah.com", "blah32")
req_options = {
use_ssl: uri.scheme == "https",
}
@response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
res = @response.body
res.force_encoding("utf-8")
resEncoded = JSON.parse(res)
users = resEncoded
#data = {}
#data.merge(users)
results = []
results << resEncoded
puts results
while users["next_page"]
newUri = users["next_page"]
uriLoop = URI.parse(newUri)
puts newUri
requestLoop = Net::HTTP::Get.new(uriLoop)
requestLoop.content_type = "application/json"
requestLoop.basic_auth("cbradford@blah.com", "blah32")
req_optionsLoop = {
use_ssl: uriLoop.scheme == "https",
}
@responseLoop = Net::HTTP.start(uriLoop.hostname, uriLoop.port, req_optionsLoop) do |http|
http.request(requestLoop)
end
dataEncoded = @responseLoop.body
dataEncoded.force_encoding("utf-8")
resLoop = JSON.parse(dataEncoded)
results << resLoop
updateUri = resLoop["next_page"]
users["next_page"] = updateUri
end
puts "hash created Successfully!"
puts results
File.write('blahzendeskusers1.json', results)
puts "File Created Successfully!"
file = File.read('blahzendeskusers1.json')
usersLoop = JSON.load(file)["users"]
usersLoop.each do |a|
User.find_or_create_by(:zendesk_id_int => a["id"], :url => a["url"], :name => a["name"], :email => a["email"],
:phone => a["phone"], :role => a["role"], :default_group_id => a["default_group_id"], :external_id => a["external_id"],
:tags => a["tags"]
)
end
堆栈跟踪:
> /Users/christopherbradford/.rbenv/versions/2.3.1/lib/ruby/2.3.0/json/common.rb:156:in
> `parse'
> /Users/christopherbradford/.rbenv/versions/2.3.1/lib/ruby/2.3.0/json/common.rb:156:in
> `parse'
> /Users/christopherbradford/.rbenv/versions/2.3.1/lib/ruby/2.3.0/json/common.rb:335:in
> `load'
> /Users/christopherbradford/railsProjects/aprzendeskdata/db/seeds.rb:84:in
> `<top (required)>'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.5/lib/active_support/dependencies.rb:286:in `load'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.5/lib/active_support/dependencies.rb:286:in `block in load'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.5/lib/active_support/dependencies.rb:258:in `load_dependency'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/activesupport-5.1.5/lib/active_support/dependencies.rb:286:in `load'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/railties-5.1.5/lib/rails/engine.rb:549:in
> `load_seed'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.5/lib/active_record/tasks/database_tasks.rb:270:in
> `load_seed'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.5/lib/active_record/railties/databases.rake:184:in
> `block (2 levels) in <top (required)>'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/task.rb:271:in
> `block in execute'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/task.rb:271:in
> `each'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/task.rb:271:in
> `execute'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/task.rb:213:in
> `block in invoke_with_call_chain'
> /Users/christopherbradford/.rbenv/versions/2.3.1/lib/ruby/2.3.0/monitor.rb:214:in
> `mon_synchronize'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/task.rb:193:in
> `invoke_with_call_chain'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/task.rb:182:in
> `invoke'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:160:in
> `invoke_task'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:116:in
> `block (2 levels) in top_level'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:116:in
> `each'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:116:in
> `block in top_level'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:125:in
> `run_with_threads'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:110:in
> `top_level'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:83:in
> `block in run'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:186:in
> `standard_exception_handling'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/lib/rake/application.rb:80:in
> `run'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/gems/rake-12.3.1/exe/rake:27:in
> `<top (required)>'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/bin/rake:23:in `load'
> /Users/christopherbradford/.rvm/gems/ruby-2.4.1/bin/rake:23:in
> `<main>' Tasks: TOP => db:seed
示例文件数据:
{"users"=>[{"id"=>360403052546, "url"=>"https://aprtechsupport.zendesk.com/api/v2/users/360403052546.json", "name"=>"Caller +1 (650) 434-4313", "email"=>nil, "created_at"=>"2018-03-05T19:36:00Z", "updated_at"=>"2018-03-05T19:36:00Z", "time_zone"=>"Pacific Time (US & Canada)", "phone"=>"+16504344313", "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"=>{}}, {"id"=>360404721566, "url"=>"https://aprtechsupport.zendesk.com/api/v2/users/360404721566.json", "name"=>"renglehart", "email"=>"renglehart@apr.com", "created_at"=>"2018-03-06T00:29:39Z", "updated_at"=>"2018-03-07T19:00:58Z", "time_zone"=>"Pacific Time (US & Canada)", "phone"=>nil, "shared_phone_number"=>nil, "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"=>"2018-03-07T19:00:58Z", "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"=>{}},