Jignesh Gohel
Jignesh Gohel

Reputation: 6552

Koala ruby gem get_object syntax for nested properties in fields argument

Facebook API supports choosing nested object's selected fields in the response in following manner:

GET graph.facebook.com
  /me?
    fields=albums{name, photos{name, picture}}

Reference: https://developers.facebook.com/docs/graph-api/using-graph-api#reading - Section: Making Nested Requests

When using Koala Ruby Gem to send request to Facebook API what is the syntax for specifying nested fields?

On this wiki page https://github.com/arsduo/koala/wiki/Graph-API#getting-public-data following example is given:

client = Koala::Facebook::API.new(oauth_token)
client.get_connection('someuser', 'posts',
                    {limit: @options[:max_items],
                      fields: ['message', 'id', 'from', 'type',
                                'picture', 'link', 'created_time', 'updated_time'
                        ]})

So to add the fields supported by a Facebook API end-point we pass in a hash containing fields as the key and an array of supported fields name like ['message', 'id', 'from'].

If any of the field represents a object which has its own properties, like in case of a Page's Rating endpoint which returns a list of OpenGraphRating nodes and an OpenGraphRating object has following property open_graph_story which is an object, then how do we specify nested properties when using Koala's fields syntax?

When using the endpoint directly it goes like:

page-id/ratings?fields=created_time,has_rating,has_review,open_graph_story{id},rating,review_text,reviewer

Upvotes: 1

Views: 452

Answers (1)

Krishna
Krishna

Reputation: 1

I also got stuck at the same thing. I found out that the koala takes the fields as an array of strings and then make query string out of it.So for nested properties we can directly write as

     client.get_connection('someuser', 'posts',
                {limit: @options[:max_items],
                  fields: ['albums{name, photos{name, picture}}','comments.summary(true).limit(100){likes.summary(true),from,message,created_time,comments.summary(true).limit(100){likes.summary(true),from,message,created_time}}', 'id', 'from', 'type',
                             'link', 'created_time', 'updated_time'
                    ]})

Upvotes: 0

Related Questions