Use the same serializer for all contact related actions

Also, rerun the response with an RFC4627 formatter
This commit is contained in:
Maciej Szlosarczyk 2018-11-01 23:06:27 +02:00
parent 2a7b6c1eb8
commit 94efbbbab0
No known key found for this signature in database
GPG key ID: 41D62D42D3B0D765
5 changed files with 146 additions and 105 deletions

View file

@ -1,3 +1,5 @@
require 'serializers/registrant_api/contact'
module Api module Api
module V1 module V1
module Registrant module Registrant
@ -19,7 +21,12 @@ module Api
end end
@contacts = @contacts_pool.limit(limit).offset(offset) @contacts = @contacts_pool.limit(limit).offset(offset)
render json: @contacts serialized_contacts = @contacts.map do |item|
serializer = Serializers::RegistrantApi::Contact.new(item)
serializer.to_json
end
render json: serialized_contacts
end end
def show def show
@ -67,26 +74,8 @@ module Api
contact.registrar.notify(action) contact.registrar.notify(action)
end end
render json: { id: contact.uuid, serializer = Serializers::RegistrantApi::Contact.new(contact)
name: contact.name, render json: serializer.to_json
code: contact.code,
ident: {
code: contact.ident,
type: contact.ident_type,
country_code: contact.ident_country_code,
},
email: contact.email,
phone: contact.phone,
fax: contact.fax,
address: {
street: contact.street,
zip: contact.zip,
city: contact.city,
state: contact.state,
country_code: contact.country_code,
},
auth_info: contact.auth_info,
statuses: contact.statuses }
end end
private private

View file

@ -25,37 +25,30 @@ Content-Type: application/json
{ {
"contacts":[ "contacts":[
{ {
"uuid": "84c62f3d-e56f-40fa-9ca4-dc0137778949", "id":"84c62f3d-e56f-40fa-9ca4-dc0137778949",
"domain_names": ["example.com"], "name":"Karson Kessler",
"code":"REGISTRAR2:SH022086480", "code":"REGISTRAR2:SH022086480",
"phone": "+372.12345678", "ident":{
"email": "hoyt@deckowbechtelar.net", "code":"37605030299",
"fax": null, "type":"priv",
"created_at": "2015-09-09T09:11:14.130Z", "country_code":"EE"
"updated_at": "2015-09-09T09:11:14.130Z", },
"ident": "37605030299", "email":"foo@bar.baz",
"ident_type": "priv", "phone":"+372.12345671",
"fax":"+372.12345672",
"address":{
"street":"Main Street 123",
"zip":"22222",
"city":"New City",
"state":"New state",
"country_code":"LV"
},
"auth_info":"password", "auth_info":"password",
"name": "Karson Kessler0",
"org_name": null,
"registrar_id": 2,
"creator_str": null,
"updator_str": null,
"ident_country_code": "EE",
"city": "Tallinn",
"street": "Short street 11",
"zip": "11111",
"country_code": "EE",
"state": null,
"legacy_id": null,
"statuses":[ "statuses":[
"ok" "ok"
], ]
"status_notes": {
} }
} ]
],
"total_number_of_records": 2
} }
``` ```
@ -77,33 +70,28 @@ HTTP/1.1 200
Content-Type: application/json Content-Type: application/json
{ {
"uuid": "84c62f3d-e56f-40fa-9ca4-dc0137778949", "id": "84c62f3d-e56f-40fa-9ca4-dc0137778949",
"domain_names": ["example.com"], "name": "Karson Kessler",
"code": "REGISTRAR2:SH022086480", "code": "REGISTRAR2:SH022086480",
"phone": "+372.12345678", "ident": {
"email": "hoyt@deckowbechtelar.net", "code": "37605030299",
"fax": null, "type": "priv",
"created_at": "2015-09-09T09:11:14.130Z", "country_code": "EE"
"updated_at": "2015-09-09T09:11:14.130Z", },
"ident": "37605030299", "email": "foo@bar.baz",
"ident_type": "priv", "phone": "+372.12345671",
"fax": "+372.12345672",
"address": {
"street": "Main Street 123",
"zip": "22222",
"city": "New City",
"state": "New state",
"country_code": "LV"
},
"auth_info": "password", "auth_info": "password",
"name": "Karson Kessler0",
"org_name": null,
"registrar_id": 2,
"creator_str": null,
"updator_str": null,
"ident_country_code": "EE",
"city": "Tallinn",
"street": "Short street 11",
"zip": "11111",
"country_code": "EE",
"state": null,
"legacy_id": null,
"statuses": [ "statuses": [
"ok" "ok"
], ]
"status_notes": {}
} }
``` ```
@ -156,7 +144,7 @@ Content-Type: application/json
{ {
"id":"84c62f3d-e56f-40fa-9ca4-dc0137778949", "id":"84c62f3d-e56f-40fa-9ca4-dc0137778949",
"name": "Karson Kessler0", "name":"Karson Kessler",
"code":"REGISTRAR2:SH022086480", "code":"REGISTRAR2:SH022086480",
"ident":{ "ident":{
"code":"37605030299", "code":"37605030299",
@ -187,7 +175,9 @@ Content-Type: application/json
{ {
"errors":{ "errors":{
"phone": ["Phone nr is invalid"] "phone":[
"Phone nr is invalid"
]
} }
} }
``` ```

View file

@ -0,0 +1,36 @@
module Serializers
module RegistrantApi
class Contact
attr_reader :contact
def initialize(contact)
@contact = contact
end
def to_json
{
id: contact.uuid,
name: contact.name,
code: contact.code,
ident: {
code: contact.ident,
type: contact.ident_type,
country_code: contact.ident_country_code,
},
email: contact.email,
phone: contact.phone,
fax: contact.fax,
address: {
street: contact.street,
zip: contact.zip,
city: contact.city,
state: contact.state,
country_code: contact.country_code,
},
auth_info: contact.auth_info,
statuses: contact.statuses
}
end
end
end
end

View file

@ -0,0 +1,26 @@
require 'test_helper'
require 'serializers/registrant_api/contact'
class SerializersRegistrantApiContactTest < ActiveSupport::TestCase
def setup
@contact = contacts(:william)
@serializer = Serializers::RegistrantApi::Contact.new(@contact)
@json = @serializer.to_json
end
def test_returns_uuid_as_id
assert_equal(@contact.uuid, @json[:id])
end
def test_returns_indent_as_separate_object
expected_ident = { code: @contact.ident, type: @contact.ident_type,
country_code: @contact.ident_country_code }
assert_equal(expected_ident, @json[:ident])
end
def test_returns_address_as_separate_object
expected_address = { street: @contact.street, zip: @contact.zip, city: @contact.city,
state: @contact.state, country_code: @contact.country_code }
assert_equal(expected_address, @json[:address])
end
end

View file

@ -1,7 +1,7 @@
require 'test_helper' require 'test_helper'
require 'serializers/registrant_api/domain' require 'serializers/registrant_api/domain'
class SerializersRegistrantApiDomainTest < ApplicationIntegrationTest class SerializersRegistrantApiDomainTest < ActiveSupport::TestCase
def setup def setup
@domain = domains(:airport) @domain = domains(:airport)
@serializer = Serializers::RegistrantApi::Domain.new(@domain) @serializer = Serializers::RegistrantApi::Domain.new(@domain)