Check if a favorited tweet contains a URL within the body - ruby

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.

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)

Related

Why does twitter API add url at the end of the text

When getting tweet information using the twitter API, the returned text or full_text field has a URL appended at the end of the text. For example:
"full_text": "Just another Extended Tweet with more than 140 characters, generated as a documentation example, showing that [\"truncated\": true] and the presence of an \"extended_tweet\" object with complete text and \"entities\" #documentation #parsingJSON #GeoTagged https://twitter.com/FloodSocial/status/994633657141813248"
https://twitter.com/FloodSocial/status/994633657141813248 is appended at the end(The appended url is acutally a shortened url but stackoverflow does not allow shortened url in the body so I just replace it with the full URL). Why does the API add this and is there a way to get the text without the added URL?
Are you using the correct twitter gem? using gem install twitter and setting up a client according to the docs, you should be able to just get the tweet/status by it's ID. But whatever example you are using doesn't show how you got the full text
text = client.status('994633657141813248').text
=>"Just another Extended Tweet with more than 140 characters, generated as a documentation example, showing that https://twitter.com/FloodSocial/status/994633657141813248"
The url is truncated as a plain string so not sure what you even do to get the string you formulated.
But if you have some long string somehow with the url embedded, you could do
text.split(/\shttp?s/).first
That looks like a quote Tweet where the original Tweet URL is included?
[edit - I was wrong with the above statement]
I see what is happening. The original Tweet links to an image on Twitter (https://twitter.com/FloodSocial/status/994633657141813248/photo/1, via a shortened tco link). Twitter hides the image URL in the rendered Tweet, but returns it in the body of the text. That's the expected behaviour in this case. You can also see the link parsed out in the extended_entities segment of the Tweet data, as well as the image data itself in the same area of the Tweet. If you want to omit the URL from the text data, you'll need to trim it yourself.

Docusign APi with RUby: IS there a way to download a document with all the tab content populated

I need to download an envelope from docusign by populating the tab data in ruby on rails.
I have used get_combined_document_from_envelope but it does not seems to get all the data.
def method_name
output_pdf = docusign.get_combined_document_from_envelope(
envelope_id: document.external_key,
local_save_path: "docusign_docs/file_name.pdf",
return_stream: false
)
end
I need the output file to have all the tabs populated.
The pdf would have the data inside the document as part of the completed doc (assuming it's visible). If you want to get the data downloaded separately, you would need to use API calls to get the envelope and get the tabs information. see this https://developers.docusign.com/esign-rest-api/guides/features/tabs

How can I turn a mongoid document into JSON stripping out embedded document ids, but keeping the main document id?

I have a document that has some embedded documents (and some of those have further embedded documents).
I want to return the document content as JSON but without all the IDs for the embedded documents.
I can do this: mydoc.to_json(:except => :_id)
but that strips all BSON IDs, including the one for mydoc.
Is there some other way to only strip the embedded document IDs?
Keep in mind that to_json is actually two steps here:
Call as_json to get a Hash.
Convert that Hash to a string of JSON.
So you could use as_json instead of to_json to build a Hash without any _ids, then put the _id you care about back in, and finally convert to JSON:
mydoc.as_json(:except => :_id).merge('_id' => mydoc.id).to_json

Typeerror when parsing JSON from API

I'm implementing a simple tumblr api tool for my girlfriend, and I'm having some trouble working with the data handed to me by Tumblr.
The structure handed back can be found here (scroll down a little for the example.)
The following string of hash keys works while I'm working manually in IRB:
followers_result['response']['users']
But when I launch the server and try to walk the json hash, I get a "Can't convert string to integer" typeerror. Using the following code, response['users'] is identified as the problem field. (I expanded the hashtree query a bit here.)
followers_result = JSON.parse(#followers_response.body)
response = followers_result['response']
users = response['users']
users.each do |follower|
followers << follower['name']
end
I'm having the same problem with my other request, which requires a few more nodes down the tree... Anyone know why this would be different between server and irb?
(One difference is that the server is requesting via OAuth whereas my irb tests are requesting via net/http, but I'm taking the exact same OAuth query and adding my API key on to do the tests with. I'm not sure how to manually request it via OAuth to make sure that the server is getting the same document because of the three-legged authentication.)
Thanks for any help/suggestions,
Cameron

Filtering the result of a JSON.parse response in Ruby (and the JSON gem)

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?
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.

Resources