Skip to content

Instantly share code, notes, and snippets.

@tomas-stefano
Last active June 4, 2020 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomas-stefano/0e3f54b9ff28876b6674cff5db6c2a72 to your computer and use it in GitHub Desktop.
Save tomas-stefano/0e3f54b9ff28876b6674cff5db6c2a72 to your computer and use it in GitHub Desktop.
DEFRA to MoJ metadata (a simple version)

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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment