To run on the terminal:
bundle
bundle exec rspec conversion_spec.rb
To run on the terminal:
bundle
bundle exec rspec conversion_spec.rb
| require 'json' | |
| class Convert | |
| attr_reader :json | |
| def initialize(json) | |
| @json = JSON.parse(json) | |
| end | |
| def to_moj | |
| page_id = @json['path'].gsub('/', '') | |
| translator = { | |
| 'TextField' => 'text' | |
| } | |
| JSON.dump({ | |
| "_id" => "page._#{page_id}", | |
| "_type" => "page.singlequestion", | |
| "url" => @json['path'], | |
| "components" => @json['components'].map do |component| | |
| { | |
| "_id" => "page._#{page_id}--#{translator[component['type']]}.auto_name__1", | |
| "_type" => translator[component['type']], | |
| "hint" => component['hint'], | |
| "label" => component['title'], | |
| "name" => component['name'], | |
| } | |
| end | |
| }) | |
| end | |
| end |
| require_relative './conversion' | |
| RSpec.describe 'Converting from DEFRA to MoJ' do | |
| let(:defra) do | |
| <<-JSON | |
| { | |
| "components": [ | |
| { | |
| "type": "TextField", | |
| "hint": "Provide your full name", | |
| "title": "Name", | |
| "name": "userName", | |
| "options": {}, | |
| "schema": {} | |
| } | |
| ], | |
| "path": "/first-page", | |
| "next": [ | |
| { | |
| "path": "/second-page" | |
| } | |
| ] | |
| } | |
| JSON | |
| end | |
| let(:moj) do | |
| %{{"_id":"page._first-page","_type":"page.singlequestion","url":"/first-page","components":[{"_id":"page._first-page--text.auto_name__1","_type":"text","hint":"Provide your full name","label":"Name","name":"userName"}]}} | |
| end | |
| it 'converts to metadata' do | |
| expect(Convert.new(defra).to_moj).to eq(moj) | |
| end | |
| end |
| gem 'rspec' |