Skip to content

Instantly share code, notes, and snippets.

@kliph
Last active September 27, 2017 01:23
Show Gist options
  • Save kliph/79c64b75a3d9e2ab4065e9def58fc473 to your computer and use it in GitHub Desktop.
Save kliph/79c64b75a3d9e2ab4065e9def58fc473 to your computer and use it in GitHub Desktop.
class Parser
def self.parse(foo)
foo[:results] = foo[:results].reduce([]) do |acc, result|
if result[:assays].is_a? Array
res = result[:assays].reduce([]) do |coll, assay|
if assay[:range].is_a? String
coll.push({range: Hash[[:low, :high].zip(assay[:range].split('-').map(&:to_i))]})
else
coll.push assay
end
end
acc.push({assays: res})
else
acc.push({assays: [result[:assays]]})
end
end
foo
end
end
describe Parser do
let(:raw) {
{
results: [
{
assays: [
{range: '1-2'},
{
range: {
low: 1,
high: 2
}
}
]},
{
assays:
{
range: {
low: 3,
high: 100
}
}
}
]}
}
it 'transforms the range into a hash of low and high' do
expected = Parser.parse(raw)[:results][0][:assays][0][:range]
expect(expected).to eq({low: 1, high: 2})
end
it 'only returns arrays of assays' do
expected = Parser.parse(raw)
expect(expected[:results].map{|x| x[:assays]}.map(&:class).uniq).to eq [Array]
end
it 'only returns hashes in the assay arrays' do
expected = Parser.parse(raw)
expect(expected[:results].map{|x| x[:assays].map(&:class)}.flatten.uniq).to eq [Hash]
end
it 'handles negative numbers'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment