I’m having an issue with passing the generated JSON notation of my object to my Sinatra application. The problem I have is twofold:
- I have 2 classes that are mapped to a database using the Sequel gem. When they generate JSON it is ok and properly implemented.
- I have a custom class called registration that maps one of the classes with an additional field. The goal is to generate JSON out of this and pass that JSON to the application using cucumber (test purpose)
The application code responsible for handling the request has the following function defined:
post '/users' do
begin
hash = JSON.parse(self.request.body.read)
registration = Registration.new.from_json(@request.body.read)
registration.user.country = Database::Alaplaya.get_country_by_iso_code(registration.user.country.iso_code)
return 400 unless(registration.is_valid?)
id = Database::Alaplaya.create_user(registration.user)
# If the registration failed in our system, return a page 400.
return 400 if id < 1
end
- problem 1: I cannot use the params hash. It exists but is just an empty hash. Why?
- problem 2: I cannot deserialize the JSON generated by the class itself. Why?
The registration class looks like this:
require 'json'
class Registration
attr_accessor :user, :project_id
def to_json(*a)
{
'json_class' => self.class.name,
'data' => [@user.to_json(*a), @project_id]
}.to_json(*a)
end
def self.json_create(o)
new(*o['data'])
end
# Creates a new instance of the class using the information provided in the
# hash. If a field is missing in the hash, nil will be assigned to that field
# instead.
def initialize(params = {})
@user = params[:user]
@project_id = params[:project_id]
end
# Returns a string representing the entire Registration.
def inspect
"#{@user.inspect} - #{@user.country.inspect} - #{@project_id}"
end
# Returns a boolean valid representing whether the Registration instance is
# considered valid for the API or not. True if the instance is considered
# valid; otherwise false.
def is_valid?
return false if @user.nil? || @project_id.nil?
return false if !@user.is_a?(User) || !@project_id.is_a?(Fixnum)
return false if !@user.is_valid?
true
end
end
I had to implement the methods to generate the JSON output correctly. When I run this in console I get the following output generated:
irb(main):004:0> r = Registration.new(:user => u, :project_id => 1)
=> new_login - nil - 1
irb(main):005:0> r.to_json
=> "{\"json_class\":\"Registration\",\"data\":[\"{\\\"json_class\\\":\\\"User\\\
",\\\"login\\\":\\\"new_login\\\"}\",1]}"
Which looks like valid JSON to me. However when I POST this to the application server and try to parse this, JSON complains that at least 2 octets are needed and refuses to deserialize the object.
If you’re using Sequel as your ORM, try something like this:
In your model:
The server:
I think you may be overcomplicating your registration process. If the HTTP action is
POST /usersthen why not create a user? Seems like creating aregistrationis overly complex. Unless your user already exists, in which casePOST /userswould be incorrect. If what you’re really intending to do is add a user to to a project, then you shouldPUT /projects/:project_id/users/:user_idand the action would look something like this: