tags:

views:

246

answers:

1

Hey everybody,

In a little app I'm building, I'm using the the twitter_oauth gem (source of the methods I'm using), which incidentally means I'm dealing with the JSON ruby gem.

I'm using the messages method, whose source is as follows:

def messages(page=1)
      oauth_response = access_token.get("/direct_messages.json?page=#{page}")
      JSON.parse(oauth_response.body)
end

It parses the JSON produced by Twitter, using the JSON.parse method. Now, what I want to do is filter the response, so as to show only the messages sent by a certain user. In other words, I want to be able to get an accounts message's on a per user basis.

I went through the JSON gem docs, but couldn't find an easy way to do this. I normally don't work with JSON (I prefer XML), but since the Twitter_oauth gem relies on it, I'm forced to learn it (unless I change the gem's source code or overwrite it - not of my preference).

Does any one know a pragmatic way of sorting JSON in Ruby?

+2  A: 

Why do you have to work with JSON at all? JSON.parse gives you back deserialized structures (Ruby arrays and hashes), so all you have to do (I presume, you haven't pasted sample JSON output here) is to do select on whatever output JSON.parse (or, in your case, messages) method returns.

Mladen Jablanović
Yes, I figured I could filter the response, but didn't know how the response was structured. I'm working on it now, thank you.
Joel M.
I agree, do your filtering on the objects, using normal Ruby...
glenn mcdonald