Issue importing JSON data into database - ruby-on-rails

I've written a script that imports paginated JSON data from an api, encodes the data to UTF-8 and then writes it to a file.
I need to iterate over the data in the file to create records in my database, but am running to an error loading the file so I can do so.
The stack trace isn't telling me much about what's wrong; any help would be greatly appreciated!
code:
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
stack trace:
> /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
Sample File Data:
{"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"=>{}},

Related

sqlite3 to postgreSQL migration issues with Rails and ActiveRecord

Prior information: I have very little knowledge in Ruby on Rails and its Gems and Frameworks. So things which seem to be obvious may not be the case to me.
I need to migrate the database from sqlite3 to postgreSQL in our application containing AngularJS in the frontend and Ruby on Rails with ActiveRecord in the backend.
I installed the Ruby Gem pg and changed the database.yml as following:
# This was for sqlite
#adapter: sqlite3
#database: db/gfms.sqlite3
#pool: 5
#timeout: 5000
# This is for postgreSQL
adapter: postgresql
encoding: unicode
database: llqa
pool: 5
username: postgres
password: admin
host: localhost
port: 1234
timeout: 5000
schema_search_path: "gfms"
I'm having several issues now. Some queries return no records where they should, but don't generate an error. But:
A more specific problem I'm having is this error:
E, [2017-04-21T07:34:30.381466 #10961] ERROR -- : PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: ..."configtables" WHERE (parent IS NULL OR parent = "" OR pare...
^
: SELECT COUNT(*) FROM "configtables" WHERE (parent IS NULL OR parent = "" OR parent = 'model')
2017-04-21 07:34:30 - ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: ..."configtables" WHERE (parent IS NULL OR parent = "" OR pare...
^
: SELECT COUNT(*) FROM "configtables" WHERE (parent IS NULL OR parent = "" OR parent = 'model'):
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `block in exec_no_cache'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:378:in `block in log'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.16/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract_adapter.rb:372:in `log'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `exec_no_cache'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/postgresql_adapter.rb:954:in `select'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:265:in `execute_simple_calculation'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:227:in `perform_calculation'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:119:in `calculate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.16/lib/active_record/relation/calculations.rb:34:in `count'
/Users/ndinatale/leanlogic-qa-prototype/server/lib/models_base.rb:135:in `get_list'
/Users/ndinatale/leanlogic-qa-prototype/server/routes/02_rest_misc.rb:266:in `block in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1611:in `block in compile!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `[]'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (3 levels) in route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:994:in `route_eval'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:975:in `block (2 levels) in route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1015:in `block in process_route'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `catch'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1013:in `process_route'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:973:in `block in route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `each'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:972:in `route!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1085:in `block in dispatch!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1082:in `dispatch!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `block in call!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `block in invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `catch'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1067:in `invoke'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:907:in `call!'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:895:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:225:in `context'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/session/abstract/id.rb:220:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/xss_header.rb:18:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/path_traversal.rb:16:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/json_csrf.rb:18:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/base.rb:49:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-protection-1.5.3/lib/rack/protection/frame_options.rb:31:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/nulllogger.rb:9:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/head.rb:13:in `call'
/Library/Ruby/Gems/2.0.0/gems/rack-1.6.5/lib/rack/methodoverride.rb:22:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/show_exceptions.rb:25:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:182:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:2013:in `call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `block in call'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1785:in `synchronize'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1785:in `synchronize'
/Library/Ruby/Gems/2.0.0/gems/sinatra-1.4.8/lib/sinatra/base.rb:1487:in `call'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/configuration.rb:224:in `call'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/server.rb:600:in `handle_request'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/server.rb:435:in `process_client'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/server.rb:299:in `block in run'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/thread_pool.rb:120:in `call'
/Library/Ruby/Gems/2.0.0/gems/puma-3.8.2/lib/puma/thread_pool.rb:120:in `block in spawn_thread'
The function where the error happens (the line is commented):
def get_list(limit = nil, page = nil)
coll = all
coll = (block_given?) ? (yield coll) : coll
rowcnt = coll.count # This is the line where the error happens
pages = (limit && limit > 0) ? (rowcnt.to_f/limit).ceil : 1
coll = coll.limit(limit) if limit && limit > 0
coll = coll.offset((page-1)*limit) if limit && limit > 0 && page && page > 1
ret = { rows: coll.all.to_a, rowcnt: rowcnt, page: page || 1, pages: pages, limit: limit || 0 }
def ret.serialize(include = nil)
self[:rows].collect! do |row|
hash = include ? (row.serializable_hash(include: include.incllist)) : row.serializable_hash
(hash['code'] && hash['code'].end_with?('(TR)')) ? nil : ((block_given?) ? yield(hash,row) : hash)
end
self[:rows].compact!
return self.rest_success,true,self
end
return ret
end
This is just an example. My question however is: Is there something in the configuration of ActiveRecord, Ruby or postgreSQL itself so it behaves equally like with sqlite3? What needs to be done, if anything?

I am using https://github.com/tyrauber/stock_quote to fetch stock data. However I am getting below mentioned error while doing so

I am new to ruby on rails. :(
while doing a search I am getting StockQuote::NoDataForStockError in StocksController#search ...........................
My model
class Stock < ActiveRecord::Base
def self.find_by_ticker(ticker_symbol)
where(ticker: ticker_symbol).first
end
def self.new_from_lookup(ticker_symbol)
looked_up_stock = StockQuote::Stock.quote(ticker_symbol)
return nil unless looked_up_stock.name
new_stock = new(ticker: looked_up_stock, name: looked_up_stock.name)
new_stock.last_price = new_stock.price
new_stock
end
def price
closing_price = StockQuote::Stock.quote(ticker).close
return "#{closing_price} (closing)" if closing_price
opening_price = StockQuote::Stock.quote(ticker).open
return "#{opening_price (opening)}" if opening_price
"Unavailable"
end
end
Errors I am getting in console while doing a search.
StockQuote::NoDataForStockError: StockQuote::NoDataForStockError
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/stock_quote-1.2.3/lib/stock_q
uote/stock.rb:134:in `parse'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/stock_quote-1.2.3/lib/stock_q
uote/stock.rb:86:in `block in quote'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:228:in `call'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:228:in `process_result'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:178:in `block in transmit'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/2.2.0/net/http.rb:853:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:172:in `transmit'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:64:in `execute'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/rest-client-1.6.7/lib/restcli
ent/request.rb:33:in `execute'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/stock_quote-1.2.3/lib/stock_q
uote/stock.rb:84:in `quote'
from C:/Sites/tracker/app/models/stock.rb:19:in `price'
from C:/Sites/tracker/app/models/stock.rb:14:in `new_from_lookup'
from (irb):9
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/console.rb:110:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/console.rb:9:in `start'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/commands_tasks.rb:68:in `console'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby2.2.0/lib/ruby/gems/2.2.0/gems/railties-4.2.5.1/lib/rails/co
mmands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
If you look at the code for that gem you can see that a StockQuote::NoDataForStockError is returned when the response code is not 200. You'll need to delve into what it doesnt like about the data you are providing. For example you should be able to query the response a bit more and at least determine what url is being sent.
The solution for page interrupting is to use begin rescue blocks every time when we call methods from StockQuote module. In begin part of block use regular call and in rescue part set value to nil and in that case return value 'Unavailable' and similar.begin rescue in Ruby is like try catch in Java. Example:
def price
begin
closing_price = StockQuote::Stock.quote(ticker).close
rescue
closing_price = nil
end
return "#{closing_price} (Closing)" if closing_price
begin
opening_price = StockQuote::Stock.quote(ticker).open
rescue
opening_price = nil
end
return "#{opening_price} (Opening)" if opening_price
'Unavailable'
end

SPARQL Query fails in Ruby-RDF/sparql-client

The example code:
gem 'sparql-client', '~> 1.1.2'
require 'sparql/client'
sparql = SPARQL::Client.new("http://dbpedia.org/sparql")
# ASK WHERE { ?s ?p ?o }
result = sparql.ask.whether([:s, :p, :o]).true?
from the github repo here gives the following error:
/home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/reader.rb:410:in `fail_subject': ERROR [line 1] Expected subject (found: "{ \"head\": { \"link\": [] }, \"boolean\": true}") (RDF::ReaderError)
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/ntriples/reader.rb:210:in `block in read_triple'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/ntriples/reader.rb:204:in `loop'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/ntriples/reader.rb:204:in `read_triple'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/reader.rb:391:in `read_statement'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/reader.rb:308:in `block in each_statement'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/reader.rb:308:in `loop'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/reader.rb:308:in `each_statement'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/util/aliasing.rb:46:in `block in alias_method'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/rdf-1.1.4.2/lib/rdf/mixin/countable.rb:12:in `empty?'
from /home/USER/.rvm/gems/ruby-2.1.2/gems/sparql-client-1.1.2/lib/sparql/client/query.rb:268:in `true?'
from /home/USER/tutorial_workspace/sentiment_dev/sparql_query.rb:6:in `<main>'
Someone got an idea to fix it?

ruby on rails scopes - ArgumentError: wrong number of arguments(1 for 0)

I'm getting a wrong arguments error when invoking a scope:
Report.start_driver_time(Unit.find(3007),Driver.find(2),3,2013)
scope :start_driver_time, lambda { |unit, driver, month, year|
where("unit_id = ? AND
driver_id = ? AND
extract(MONTH FROM time) = ? AND
extract(YEAR FROM time) = ?",
unit.id, driver.id, month, year)
.order("time asc")
.min(:time)
}
#select(:time) select([:id, :time])
scope :next_driver_time, lambda {|unit, time|
joins(:alerts => {:alert_code => :alert_unit_codes})
.where("reports.unit_id = ? AND
reports.time >= ? AND
alert_unit_codes.name = ?",
unit.id, time, "Seat Belt OFF")
.min(:time)
}
scope :reports_within_driving_period, lambda { |start_time, end_time|
where("unit_id = ? AND
reports.time >= ? AND
reports.time <= ?",
start_time, end_time)
}
stacktrace:
ArgumentError: wrong number of arguments(1 for 0)
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation/delegation.rb:37:in `min'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation/delegation.rb:37:in `method_missing'
from /Users/johnmerlino/Documents/github/XKTrackingSystem/app/models/report.rb:40:in `block in <class:Report>'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/named.rb:180:in `call'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/named.rb:180:in `block (2 levels) in scope'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/default.rb:41:in `block in unscoped'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation.rb:241:in `block in scoping'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping.rb:98:in `with_scope'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/relation.rb:241:in `scoping'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/default.rb:41:in `unscoped'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.5/lib/active_record/scoping/named.rb:180:in `block in scope'
from (irb):1
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.5/lib/rails/commands/console.rb:47:in `start'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.5/lib/rails/commands/console.rb:8:in `start'
from /Users/johnmerlino/.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.5/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p0 :002 > exit
I think you intend to use minimum instead of min? I haven't found a reference to min, so it might be using a different method that uses no parameters.

Job failed to load: uninitialized constant Syck::Syck

I am running a method in the background using delayed-job (collectiveideas fork)
The method I am running is shown below, which fetches the price of the isbn:
def update_prices(isbn13)
#amazon = Nokogiri::XML(open("http://www.amazon.com/s/?url=search-alias%3Daps&field-keywords=#{isbn13}"))
price = #amazon.search('span.listprice').first.text.gsub('$', '')
book = Book.find_by_isbn13(isbn13)
book.update_attribute(:amazon_us_price, price)
end
handle_asynchronously :update_prices
Now when I run this, the job is added to the database but encounters an error when running.
The error is quite large, but I think the gist of it comes from the first line:
Job failed to load: uninitialized constant Syck::Syck.
{Job failed to load: uninitialized constant Syck::Syck. Handler: "--- !ruby/struct:Delayed::PerformableMethod
object: &id005 !ruby/object:BooksController
_action_name: show
_config: !omap []
_env: &id001
GATEWAY_INTERFACE: CGI/1.1
PATH_INFO: /books/9780425160985
QUERY_STRING: \"\"
REMOTE_ADDR: 127.0.0.1
REMOTE_HOST: localhost
REQUEST_METHOD: GET
REQUEST_URI: http://localhost:3000/books/9780425160985
SCRIPT_NAME: \"\"
SERVER_NAME: localhost
SERVER_PORT: \"3000\"
SERVER_PROTOCOL: HTTP/1.1
SERVER_SOFTWARE: WEBrick/1.3.1 (Ruby/1.9.2/2010-08-18)
HTTP_HOST: localhost:3000
HTTP_CONNECTION: keep-alive
HTTP_REFERER: http://localhost:3000/books/search?utf8=%E2%9C%93&keywords=hornets+nest&commit=Search
HTTP_ACCEPT: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
HTTP_USER_AGENT: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.107 Safari/534.13
HTTP_ACCEPT_ENCODING: gzip,deflate,sdch
HTTP_ACCEPT_LANGUAGE: en-US,en;q=0.8
HTTP_ACCEPT_CHARSET: ISO-8859-1,utf-8;q=0.7,*;q=0.3
HTTP_COOKIE: _bookcloud_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRiIlMTNkZWFlYTdiYjVjN2IzZDAyOTU1MDc1NTc1ZDJlZDBJIhBfY3NyZl90b2tlbgY7AEZJIjFrUHJoT0RlZmZoSzIrSVdFdFJNbGxMckxENEZnMTM1STZuQUJCdHJuak1BPQY7AEY%3D--c2b7958a5461253129b6d363e3d547bd44c83f9d
rack.version:
- 1
- 1
rack.input: !ruby/object:StringIO {}
rack.errors: !ruby/object:IO {}
rack.multithread: false
rack.multiprocess: false
rack.run_once: false
rack.url_scheme: http
HTTP_VERSION: HTTP/1.1
REQUEST_PATH: /
action_dispatch.parameter_filter:
- :password
action_dispatch.secret_token: ab805f7bf1e681785a2ac4813edc43678cbe855682bc7ca9dc5f2116fc892d8ca41466f9bc261ef4ae15ad83c1c38780780d2f6bfe9c00c14abd68542a535400
action_dispatch.remote_ip: !ruby/object:ActionDispatch::RemoteIp::RemoteIpGetter
check_ip_spoofing: true
env: *id001
trusted_proxies: !ruby/regexp /(^127\\.0\\.0\\.1$|^(10|172\\.(1[6-9]|2[0-9]|30|31)|192\\.168)\\.)/i
rack.session: !map:ActionDispatch::Session::AbstractStore::SessionHash
session_id: 13deaea7bb5c7b3d02955075575d2ed0
_csrf_token: kPrhODeffhK2+IWEtRMllLrLD4Fg135I6nABBtrnjMA=
rack.session.options: !map:ActionDispatch::Session::AbstractStore::OptionsHash
:path: /
:domain:
:expire_after:
:secure: false
:httponly: true
:id: 13deaea7bb5c7b3d02955075575d2ed0
rack.request.cookie_string: _bookcloud_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRiIlMTNkZWFlYTdiYjVjN2IzZDAyOTU1MDc1NTc1ZDJlZDBJIhBfY3NyZl90b2tlbgY7AEZJIjFrUHJoT0RlZmZoSzIrSVdFdFJNbGxMckxENEZnMTM1STZuQUJCdHJuak1BPQY7AEY%3D--c2b7958a5461253129b6d363e3d547bd44c83f9d
rack.request.cookie_hash:
_bookcloud_session: BAh7B0kiD3Nlc3Npb25faWQGOgZFRiIlMTNkZWFlYTdiYjVjN2IzZDAyOTU1MDc1NTc1ZDJlZDBJIhBfY3NyZl90b2tlbgY7AEZJIjFrUHJoT0RlZmZoSzIrSVdFdFJNbGxMckxENEZnMTM1STZuQUJCdHJuak1BPQY7AEY=--c2b7958a5461253129b6d363e3d547bd44c83f9d
action_dispatch.cookies: !map:ActionDispatch::Cookies::CookieJar
_bookcloud_session: BAh7B0kiD3Nlc3Npb25faWQGOgZFRiIlMTNkZWFlYTdiYjVjN2IzZDAyOTU1MDc1NTc1ZDJlZDBJIhBfY3NyZl90b2tlbgY7AEZJIjFrUHJoT0RlZmZoSzIrSVdFdFJNbGxMckxENEZnMTM1STZuQUJCdHJuak1BPQY7AEY=--c2b7958a5461253129b6d363e3d547bd44c83f9d
action_dispatch.request.unsigned_session_cookie:
session_id: 13deaea7bb5c7b3d02955075575d2ed0
_csrf_token: kPrhODeffhK2+IWEtRMllLrLD4Fg135I6nABBtrnjMA=
action_dispatch.request.path_parameters:
:controller: books
:action: show
:isbn13: \"9780425160985\"
action_controller.instance: *id005
action_dispatch.request.content_type:
action_dispatch.request.request_parameters: !map:ActiveSupport::HashWithIndifferentAccess {}
rack.request.query_string: \"\"
rack.request.query_hash: {}
action_dispatch.request.query_parameters: !map:ActiveSupport::HashWithIndifferentAccess {}
action_dispatch.request.parameters: &id002 !map:ActiveSupport::HashWithIndifferentAccess
controller: books
action: show
isbn13: \"9780425160985\"
action_dispatch.request.formats:
- !ruby/object:Mime::Type
string: text/html
symbol: :html
synonyms:
- application/xhtml+xml
_headers:
Content-Type: text/html
_params: *id002
_request: &id003 !ruby/object:ActionDispatch::Request
env: *id001
filtered_parameters:
controller: books
action: show
isbn13: \"9780425160985\"
fullpath: /books/9780425160985
method: GET
request_method: GET
_response: !ruby/object:ActionDispatch::Response
blank: false
block:
body: []
cache_control: {}
cookie: []
etag:
header: {}
length: 0
request: *id003
sending_file: false
status: 200
writer: !ruby/object:Proc {}
_response_body:
_status: 200
action_has_layout: true
author: Patricia Daniels Cornwell
book: !ruby/ActiveRecord:Book
attributes:
isbn13: \"9780425160985\"
title: Hornet's Nest
author: Patricia Daniels Cornwell
price:
created_at: &id004 2011-03-03 10:41:27.757528 Z
updated_at: *id004
amazon_us_price:
id: 38
lookup: !ruby/object:Nokogiri::XML::Document
decorators:
errors: []
node_cache:
- !ruby/object:Nokogiri::XML::Element {}
- !ruby/object:Nokogiri::XML::Element {}
- !ruby/object:Nokogiri::XML::Element {}
lookup_context: !ruby/object:ActionView::LookupContext
details:
:handlers:
- :erb
- :rjs
- :builder
- :rhtml
- :rxml
:formats:
- :html
:locale:
- :en
- :en
details_key:
frozen_formats: false
skip_default_locale: false
view_paths: !seq:ActionView::PathSet
- !ruby/object:ActionView::FileSystemResolver
cached: {}
caching: false
path: /Users/nick/Dropbox/rails/bookcloud/app/views
title: Hornet's Nest
view_context_class:
method_name: :update_prices_without_delay
args:
- \"9780425160985\"
"
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/backend/base.rb:81:in `rescue in payload_object'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/backend/base.rb:79:in `payload_object'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/backend/base.rb:87:in `invoke_job'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:120:in `block (2 levels) in run'
/Users/nick/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:57:in `timeout'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:120:in `block in run'
/Users/nick/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:119:in `run'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:177:in `reserve_and_run_one_job'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:104:in `block in work_off'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:103:in `times'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:103:in `work_off'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:78:in `block (2 levels) in start'
/Users/nick/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:77:in `block in start'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:74:in `loop'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/worker.rb:74:in `start'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#rorticket/gems/delayed_job-2.1.4/lib/delayed/tasks.rb:9:in `block (2 levels) in <top (required)>'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:636:in `block in execute'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:597:in `block in invoke_with_call_chain'
/Users/nick/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2029:in `block (2 levels) in top_level'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2029:in `block in top_level'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2001:in `block in run'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/gems/rake-0.8.7/bin/rake:31:in `<top (required)>'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/bin/rake:19:in `load'
/Users/nick/.rvm/gems/ruby-1.9.2-p0#global/bin/rake:19:in `<main>'
Any idea what may be causing the error? I have never heard of Syck before.
Include the following lines of code in boot.rb (config\boot.rb)
require 'yaml'
YAML::ENGINE.yamler= 'syck'
For those that this didn't work for, like me, I've found include the Psych gem to handle YAML parsing fixed the issue. See: Delayed_job: Job failed to load: uninitialized constant Syck::Syck

Resources