我有以下representer
适用于平面 JSON 的工作:
# song_representer_spec.rb
require 'rails_helper'
require "representable/json"
require "representable/json/collection"
class Song < OpenStruct
end
class SongRepresenter < Representable::Decorator
include Representable::JSON
include Representable::JSON::Collection
items class: Song do
property :id
nested :attributes do
property :title
end
end
end
RSpec.describe "SongRepresenter" do
it "does work like charm" do
songs = SongRepresenter.new([]).from_json(simple_json)
expect(songs.first.title).to eq("Linoleum")
end
def simple_json
[{
id: 1,
attributes: {
title: "Linoleum"
}
}].to_json
end
end
我们现在正在实现JSONAPI 1.0的规范,我不知道如何实现能够解析以下 json 的表示器:
{
"data": [
"type": "song",
"id": "1",
"attributes":{
"title": "Linoleum"
}
]
}
提前感谢您的提示和建议
更新:
包含工作解决方案的Gist