-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I deal with nested data as object not as attributes? #435
Comments
Hi,
The only thing which I can think of here is that each part in the response should have an id. I can't see an id field in the child objects.
Ed
…--
Sent from my phone: please forgive the brevity.
Ed Jones
On 20 Feb 2017, at 05:37, yongwoon-kim ***@***.***> wrote:
I got the response via her request as following:
The response is nested.
I want to use contents data as a object.
But execute the program, I got a error as follow:
undefined method `logo' for #<ActiveSupport::HashWithIndifferentAccess:0x007f8bfd7a63a0>
How can I deal with as object about contents data?
If I deal with hash, Has no problem.
json.set! :content do
json.array! part.content do |data|
json.logo data[:logo]
json.bg_img data[:bg_img]
end
end
response
{
"page": {
"title": "Rock Fun",
"parts": [
{
"kind": "header",
"contents": {
"logo": "https://example/assets/logo.png",
"bg_img": "https://example/assets/bg.png"
}
},
{
"kind": "header",
"contents": [{
"logo": "https://example/assets/logo.png",
"bg_img": "https://example/assets/bg.png"
},
{
"logo": "https://example/assets/logo.png",
"bg_img": "https://example/assets/bg.png"
}]
}
]
}
}
connection.rb
CUSTOM_PARSER = Her::API.setup url: "https://example.com" do |conn|
conn.use CustomParser
conn.use Faraday::Adapter::NetHttp
end
parser.rb
class CustomParser < Response::Middleware
def on_complete(env)
json = MultiJson.load(env[:body], symbolize_keys: true)
item = json.is_a?(Array) ? json[:page] : [json[:page]]
env[:body] = {
data: item,
metadata: {
status: env[:status]
}
}
end
end
model.rb
class SiteData
include Her::Model
uses_api CustomParser
collection_path "/pages/mysite"
has_many :parts
class << self
def find_organization(path)
SiteData.where(path: path).all
end
end
end
class Parts
include Her::Model
has_many :contents
belongs_to :organization
end
class Content
include Her::Model
belongs_to :part
end
site_data.jbuilder
json.result do |json|
json.array! @Result do |obj|
json.title obj.title
json.set! :parts do
json.array! obj.parts do |part|
json.kind part.kind
json.set! :content do
json.array! part.content do |data|
json.logo data.logo
json.bg_img data.bg_img
end
end
end
end
end
end
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
It means that parts, content model should have an id? The raw response data via her, doesn't have an id for all models.
I assume that API have one collection of resources. It is not be expected resource like
|
Hi. You should not need a custom parser - if you include an id for each part in the json response the has_many method should make the child responses into objects.
…--
Sent from my phone: please forgive the brevity.
Ed Jones
On 20 Feb 2017, at 08:23, yongwoon-kim ***@***.***> wrote:
It means that parts, content model should have an id?
The raw response data via her, doesn't have an id.
For handle data as object, should I insert an id in custom parser?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
|
you mean that I have to modify api (https://example.com/pages/mysite) ? How I can solve the problem in my project ? |
I got the response via her request as following:
The response is nested.
I want to use contents data as a object.
But execute the program, I got a error as follow:
How can I deal with as object about contents data?
If I deal with as hash, Has no problem.
response via her
connection.rb
parser.rb
model.rb
site_data.jbuilder
The text was updated successfully, but these errors were encountered: