Skip to content
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

Open
ghost opened this issue Feb 20, 2017 · 4 comments
Open

How can I deal with nested data as object not as attributes? #435

ghost opened this issue Feb 20, 2017 · 4 comments

Comments

@ghost
Copy link

ghost commented Feb 20, 2017

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 as 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 via her

{
  "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_site_data(path)
      SiteData.where(path: path).all
    end
  end
end

class Part
  include Her::Model
  has_many :contents

  belongs_to :siteData
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
@ghost ghost changed the title How can I make object of nested data How can I deal with nested data as object? Feb 20, 2017
@ghost ghost changed the title How can I deal with nested data as object? How can I deal with nested data as object not as attributes? Feb 20, 2017
@edtjones
Copy link
Collaborator

edtjones commented Feb 20, 2017 via email

@ghost
Copy link
Author

ghost commented Feb 20, 2017

It means that parts, content model should have an id?

The raw response data via her, doesn't have an id for all models.
For handle data as object, should I insert an id in custom parser?
like

{
  "page": {
    "title": "Rock Fun",
    "parts": [
        {
          "id":0
          "kind": "header",
          "contents": {
            "id":0
            "logo": "https://example/assets/logo.png",
            "bg_img": "https://example/assets/bg.png"
          }
        },
        {
          "id":1
          "kind": "header",
          "contents": [{
            "id":0
            "logo": "https://example/assets/logo.png",
            "bg_img": "https://example/assets/bg.png"
          },
          {
            "id":1
            "logo": "https://example/assets/logo.png",
            "bg_img": "https://example/assets/bg.png"
          }]
        }
    ]
  }
}

I assume that API have one collection of resources. It is not be expected resource like

https://example.com/pages/mysite/:siteId

@edtjones
Copy link
Collaborator

edtjones commented Feb 20, 2017 via email

@ghost
Copy link
Author

ghost commented Feb 20, 2017

you mean that I have to modify api (https://example.com/pages/mysite) ?
I couldn't because not mine. also I cannot change request url and data.
So, have to solve it somehow in my project.

How I can solve the problem in my project ?
Is it impossible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant