From 94efbbbab0bb7cf3361e42cd12acdf1a30be37d5 Mon Sep 17 00:00:00 2001 From: Maciej Szlosarczyk Date: Thu, 1 Nov 2018 23:06:27 +0200 Subject: [PATCH] Use the same serializer for all contact related actions Also, rerun the response with an RFC4627 formatter --- .../api/v1/registrant/contacts_controller.rb | 31 ++-- doc/registrant-api/v1/contact.md | 156 ++++++++---------- lib/serializers/registrant_api/contact.rb | 36 ++++ .../registrant_api/contact_test.rb | 26 +++ .../serializers/registrant_api/domain_test.rb | 2 +- 5 files changed, 146 insertions(+), 105 deletions(-) create mode 100644 lib/serializers/registrant_api/contact.rb create mode 100644 test/lib/serializers/registrant_api/contact_test.rb diff --git a/app/controllers/api/v1/registrant/contacts_controller.rb b/app/controllers/api/v1/registrant/contacts_controller.rb index fcce291f9..2edbca039 100644 --- a/app/controllers/api/v1/registrant/contacts_controller.rb +++ b/app/controllers/api/v1/registrant/contacts_controller.rb @@ -1,3 +1,5 @@ +require 'serializers/registrant_api/contact' + module Api module V1 module Registrant @@ -19,7 +21,12 @@ module Api end @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 def show @@ -67,26 +74,8 @@ module Api contact.registrar.notify(action) end - render 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 } + serializer = Serializers::RegistrantApi::Contact.new(contact) + render json: serializer.to_json end private diff --git a/doc/registrant-api/v1/contact.md b/doc/registrant-api/v1/contact.md index 74c78bd5a..1adb70514 100644 --- a/doc/registrant-api/v1/contact.md +++ b/doc/registrant-api/v1/contact.md @@ -23,39 +23,32 @@ HTTP/1.1 200 Content-Type: application/json { - "contacts": [ + "contacts":[ { - "uuid": "84c62f3d-e56f-40fa-9ca4-dc0137778949", - "domain_names": ["example.com"], - "code": "REGISTRAR2:SH022086480", - "phone": "+372.12345678", - "email": "hoyt@deckowbechtelar.net", - "fax": null, - "created_at": "2015-09-09T09:11:14.130Z", - "updated_at": "2015-09-09T09:11:14.130Z", - "ident": "37605030299", - "ident_type": "priv", - "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": [ + "id":"84c62f3d-e56f-40fa-9ca4-dc0137778949", + "name":"Karson Kessler", + "code":"REGISTRAR2:SH022086480", + "ident":{ + "code":"37605030299", + "type":"priv", + "country_code":"EE" + }, + "email":"foo@bar.baz", + "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", + "statuses":[ "ok" - ], - "status_notes": { - } + ] } - ], - "total_number_of_records": 2 + ] } ``` @@ -77,33 +70,28 @@ HTTP/1.1 200 Content-Type: application/json { - "uuid": "84c62f3d-e56f-40fa-9ca4-dc0137778949", - "domain_names": ["example.com"], + "id": "84c62f3d-e56f-40fa-9ca4-dc0137778949", + "name": "Karson Kessler", "code": "REGISTRAR2:SH022086480", - "phone": "+372.12345678", - "email": "hoyt@deckowbechtelar.net", - "fax": null, - "created_at": "2015-09-09T09:11:14.130Z", - "updated_at": "2015-09-09T09:11:14.130Z", - "ident": "37605030299", - "ident_type": "priv", + "ident": { + "code": "37605030299", + "type": "priv", + "country_code": "EE" + }, + "email": "foo@bar.baz", + "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", - "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": [ "ok" - ], - "status_notes": {} + ] } ``` @@ -134,16 +122,16 @@ Accept: application/json Content-type: application/json { - "name": "John Doe", - "email": "foo@bar.baz", - "phone": "+372.12345671", - "fax": "+372.12345672", - "address": { - "street": "Main Street 123", - "zip": "22222", - "city": "New City", - "state": "New state", - "country_code": "LV" + "name":"John Doe", + "email":"foo@bar.baz", + "phone":"+372.12345671", + "fax":"+372.12345672", + "address":{ + "street":"Main Street 123", + "zip":"22222", + "city":"New City", + "state":"New state", + "country_code":"LV" } } @@ -155,26 +143,26 @@ HTTP/1.1 200 Content-Type: application/json { - "id": "84c62f3d-e56f-40fa-9ca4-dc0137778949", - "name": "Karson Kessler0", - "code": "REGISTRAR2:SH022086480", - "ident": { - "code": "37605030299", - "type": "priv", - "country_code": "EE" + "id":"84c62f3d-e56f-40fa-9ca4-dc0137778949", + "name":"Karson Kessler", + "code":"REGISTRAR2:SH022086480", + "ident":{ + "code":"37605030299", + "type":"priv", + "country_code":"EE" }, - "email": "foo@bar.baz", - "phone": "+372.12345671", - "fax": "+372.12345672", - "address": { - "street": "Main Street 123", - "zip": "22222", - "city": "New City", - "state": "New state", - "country_code": "LV" + "email":"foo@bar.baz", + "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", - "statuses": [ + "auth_info":"password", + "statuses":[ "ok" ] } @@ -186,8 +174,10 @@ HTTP/1.1 400 Content-Type: application/json { - "errors": { - "phone": ["Phone nr is invalid"] + "errors":{ + "phone":[ + "Phone nr is invalid" + ] } } ``` diff --git a/lib/serializers/registrant_api/contact.rb b/lib/serializers/registrant_api/contact.rb new file mode 100644 index 000000000..6c02909c3 --- /dev/null +++ b/lib/serializers/registrant_api/contact.rb @@ -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 diff --git a/test/lib/serializers/registrant_api/contact_test.rb b/test/lib/serializers/registrant_api/contact_test.rb new file mode 100644 index 000000000..cace77549 --- /dev/null +++ b/test/lib/serializers/registrant_api/contact_test.rb @@ -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 diff --git a/test/lib/serializers/registrant_api/domain_test.rb b/test/lib/serializers/registrant_api/domain_test.rb index bddb0efd7..6623a0be2 100644 --- a/test/lib/serializers/registrant_api/domain_test.rb +++ b/test/lib/serializers/registrant_api/domain_test.rb @@ -1,7 +1,7 @@ require 'test_helper' require 'serializers/registrant_api/domain' -class SerializersRegistrantApiDomainTest < ApplicationIntegrationTest +class SerializersRegistrantApiDomainTest < ActiveSupport::TestCase def setup @domain = domains(:airport) @serializer = Serializers::RegistrantApi::Domain.new(@domain)