ruby - Sinatra JSON.parse undefined method when moving helper to another file -
ruby - Sinatra JSON.parse undefined method when moving helper to another file -
i have route helper works fine when it's in "helpers do" block in route file, when move helper helpers.rb file, error. here helper:
def processputrequest(patient_id, request, _class, foreign_key, route_fragment) p = patient.find(patient_id) info = request.body.read puts 'request body' + info info = json.parse(data) host_with_port = request.host_with_port puts 'hash data: ' + data.inspect savelistdata(params[:id], data, _class, foreign_key) url = {'url' => "http://#{host_with_port}/patients/#{params[:id]}#{route_fragment}"}.to_json rack::response.new(url) end
here logging when helper in route file:
request body{"list_data":[{"id":8440,"value":"removal of ear wax (procedure)"},{"id":9827,"value":"foreign body in nose (disorder)"}]} hash data: {"list_data"=>[{"id"=>8440, "value"=>"removal of ear wax (procedure)"}, {"id"=>9827, "value"=>"foreign body in nose (disorder)"}]}
and when move helpers file:
request body{"list_data":[{"id":8440,"value":"removal of ear wax (procedure)"},{"id":9827,"value":"foreign body in nose (disorder)"}]} nomethoderror - undefined method `parse' sinatra::json:module:
am missing simple? edit, got debugger working. "data" after request.body.read in routes file:
"{"list_data":[{"id":8440,"value":"removal of ear wax (procedure)"},{"id":9827,"value":"foreign body in nose (disorder)"}]}"
in helpers file:
"{"list_data":[{"id":8440,"value":"removal of ear wax (procedure)"},{"id":9827,"value":"foreign body in nose (disorder)"}]}"
so content looks identical me. can literally cutting , paste method between 2 files, works fine in routes file, fails undefined method parse in helpers file. i'm guessing i've somehow defined module improperly or have dandling or missing character, rubymine showing no errors, , method @ to the lowest degree partially execute, method getting sourced.
complete helpers file:
module sinatra module dynformat content_types={'xml' => 'text/xml','json' => 'application/json'} def dynamicformat(data,format=params[:format]) content_type content_types[format], :charset => 'utf-8' case format when 'xml' data.to_xml when 'json' data.to_json end end end helpers dynformat module routehelpers def processputrequest(patient_id, request, _class, foreign_key, route_fragment) p = patient.find(patient_id) info = request.body.read puts 'request body' + info info = json.parse(data) host_with_port = request.host_with_port puts 'hash data: ' + data.inspect savelistdata(params[:id], data, _class, foreign_key) url = {'url' => "http://#{host_with_port}/patients/#{params[:id]}#{route_fragment}"}.to_json rack::response.new(url) end def savelistdata(patient_id, list_data, _class, foreign_key) p = patient.find(patient_id) _class = eval _class list_data = list_data['list_data'] list_data.each |x| _class.create(:patient_id => patient_id, foreign_key => x['id'] ) end end end helpers routehelpers end
when set method in external file, putting module under sinatra
namespace:
module sinatra module dynformat #... end module routehelpers def processputrequest(patient_id, request, _class, foreign_key, route_fragment) # code refers json... end end end
when method calls json.parse
ends finding sinatra::json
module, not top level json
module intended, , error undefined method `parse' sinatra::json:module
.
when include method in helpers
block in main file method isn’t defined under sinatra
module, json
refers right top level module.
if need include sinatra::json
, can explicitly refer top level module using ::
:
data = ::json.parse(data)
also note don’t need define helper modules under sinatra
(though docs suggest do). can move them out of sinata
module, , utilize e.g. sinatra.helpers routehelpers
register them helpers.
ruby json sinatra helpers
Comments
Post a Comment