0

Building a small app and would like to return all a users favorited tweets. This is easy enough to do with the twitter gem. I would then like to further filter the returned results by only displaying the favorited tweets that contain a URL within the body.

Now as I understand it Using the the twitter gem running Twitter.favorites will return a json of all the favorited tweets. Now within each of the individual tweets it returns a entity property that contains a URL hash this will be empty if no URL is present in the tweet and a URL if one is present.

How would I implement a check to say if the URL is present then display tweet. I'm a noob with json and Apis.

Sam Mason
  • 1,037
  • 1
  • 8
  • 29

1 Answers1

1

I never used the twitter gem, but since the question remains unanswered for a couple of hours, I’ll try to do my best.

First of all the gem seems to return Array<Twitter::Tweet> rather than raw json. So you only need to filter the array:

favs_with_urls = Twitter.favorites.reject { |r| r.urls.empty }

The latter should do the trick.

BTW, if you for some reason need to parse json to a hash:

require 'json'
c = JSON.load(raw_json)
Community
  • 1
  • 1
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160