Searching for multiple labels Gmail API - Rails - ruby-on-rails

I am using this gem in my rails app to interact with the Gmail API: https://github.com/gmailgem/gmail
I am able to search for emails containing one label:
require 'gmail'
gmail = Gmail.connect("email#gmail.com", "testpwd")
gmail.mailbox('Urgent')
But when I try to search for multiple labels, I get an error. How do I find all email that contain two specific labels, such as email that contain both the label "Urgent" and "Priority"?

You could try using intersection...
urgent_priority_emails = gmail.mailbox('Urgent').emails & gmail.mailbox('Priority').emails
However, I have a recollection that this may not work, because the emails are treated as different objects even though they are the same messages.
An alternative that may work...
urgent_email_message_ids = gmail.mailbox('Urgent').emails.map{|email|email.message_id}
urgent_priority_emails = gmail.mailbox('Priority').emails.select{|email| urgent_email_message_ids.include?(email.message_id)}

The gmail gem let's you use the Google search filter, like this:
gmail.mailbox('Urgent').emails(gm: 'label:'Priority')

Related

Twilio REST API: request messages after sid

Problem
Using the Twilio REST API, I want to request only messages that I haven't downloaded yet. It seems the cleanest way to do this would be to download only messages after a specified SID.
Information not in the docs
The Twilio filter docs don't have this option. They only describe to, from, and date_sent.
However, it appears that Twilio does have this feature. You can see in their paging information, that the the nextpageuri contains AfterSid.
When browing the website, the URL contains /user/account/log/messages?after=SMXXXXXX
What I've tried so far
Using the twilio-ruby client, I have tried the following without success:
list = #client.account.sms.messages.list({after: 'SMXXXXXX'})
list = #client.account.sms.messages.list({AfterSid: 'SMXXXXXX'})
list = #client.account.sms.messages.list({after_sid: 'SMXXXXXX'})
From Dan Markiewicz - Twilio Customer Support
Unfortunately, we do not support filtering by this field in our API at this time. Your best option would be to get the DateCreated info on the SID you want to filter by and then use that to filter the messages by only those sent after that date. Since the date filter only supports filtering down to the day, it may return some number of unwanted messages that were sent that day but before the message you want to filter by. However, each message in the list will have a full date_created field down to the second, so you should be able to filter these out fairly easily on your end. This should produce the result you need.
After looking at the documentation you outlined, it looks like what you want to accomplish can't be done by the twilio-ruby gem. This link shows which filters are supported by the list method inside the gem in regards to messages.
If you look at the source here, starting on line 45 the gem uses next_page_uri as a way of determining the offset of where the next page should begin. For instance:
calls = twilio_client.account.calls.list # returns the initial set of calls.
calls.next_page # this uses next_page_uri to return the next set of calls internally.
This isn't something that can be changed via the gem currently.

Using Ruby/rails variables in a mandrill template

Is it possible to use your rails variables on a Mandrill template?
I have an app where a user gets notified via email on certain actions, and right now it's done with action mailer without using Mandrill so it's just a text email with no styling. Obviously, I'd prefer to use a mandrill template I already have to add some dynamic content via variables.. I see a ton of companies using variables in email notifications so i assume it's possible, i just haven't found many useful articles that explain how it's done. If you can point me towards a useful article or just answer the question it'd be really helpful! Right now, I already made the template using Mailchimp, then sent it to mandrill and it's ready for use. My app already has the Mandrill configurations and works properly (i use it for static email that don't include variables). All i really need to do is configure it to allow me to use variables.
Thanks in advance. Happy holidays and war eagle!
One way is to use the merge tags in the Mandrill template, and then either global_merge_vars (all recipients) or merge_vars (per recipients) message options to populate the template.
It is not very exciting approach, but it works fine.
In short, the solution is to put tags like:
*|MYTAG|*
anywhere in the template. Then, the send calls just need to populate the right option. Here for all participants:
mandrill = Mandrill::API.new('my_api_key')
template_name = "my-template-name"
template_content = [] # See doc, not related to the issue at hand.
message = {
to: [{email: 'smith#example.com'}],
headers: {"Reply-To" => "noreply#example.com"},
merge: true,
global_merge_vars: [
{name: 'mytag', content: "Hello, World"}
]
}
mandrill.messages.send_template(template_name, template_content, message)
This should send an email with the tag replaced with the corresponding contents (Hello, World here).

How to retrieve hashtaged tweets from a list of users

Is there a way retrieving all the tweets from a list of profiles (3) which are tagged with certain #hashtag in a single call to the Twitter API using 1.1?
If not, obviously, I'd be retrieving a hundred tweets from each user, and filtering out those which do not have the #hashtag .. but it's not very efficient, right?
Thanks in advance
Note: I've updated the library so I suggest you grab the newly updated version before trying this - I've made it so you don't need to manually encode each individual character.
This page shows you how to use search, and has a number of operators down toward the bottom of the page. One of these is the OR operator:
Getting tweets for multiple users
OR - Either this or that
love OR hate - containing either "love" or "hate" (or both)
From - From this user
from:twitterapi sent from the user #twitterapi
So, armed with the above knowledge, I'm guessing your query would look like this:
Translated into a GET request:
?q=from:user1+OR+from:user2
Getting tweets for specific hashtags
So that's how you get tweets for multiple users. The next step you want is for certain hashtags.
#haiku - containing the hashtag "haiku"
That translated individually into the correct format becomes:
?#haiku (or %2C haiku, depending on the library / urlencoding you're using)
Combining the above
The standard AND operator looks like this:
twitter search - containing both "twitter" and "search". Default operator
For a get request:
?twitter+search
So let's combine all this!
?q=#hashtag1+#hashtag2+from:user1+OR+from:user2
And, because you're using my lib, here's the code for that:
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#hashtag1+OR+#hashtag2+from:username1+OR+from:username2';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($json));

Search engine similar to gmail

I'm looking for a search engine, which will let my users to search my website using syntax similar to this gmail's one.
My website is a map-based directory of restaurants and shops so it would be lovely to make it possible to search it using strings like those:
Restaurant's name city:Boston diet:vegetarian
Restaurant's name country:Belgium tags:fast-food
Restaurant's name country:Poland diet:vegan tags:pizza
etc...
Have you any idea what can i use to achieve such a functionality? I've browsed all of the solutions from ruby-toolbox but most of them requires to have some kind of special search server set up. I can do that on my VPS but at first i would love to hear your opinion which one is the most powerfull, dev-friendly and which one covers the functionality described above. Thank you in advance! :)
How about https://github.com/makandra/dusen gem?
It supports gmail-like token search!
You could try to use regexp to extract search params from request:
search_pairs = params[:search].scan(/([a-zA-Z]+):([a-zA-Z]+)/)
>> [ ['country', 'Poland'], ['diet', 'vegan'] ]

Can't parse new google urls - HTTP_REFERER doesn't contain parameters anymore

It seems a little odd to my, but although everybody knows about the new google search urls (see Google using # instead of search? in URL. Why?) no one has a problem with the HTTP_REFERER.
I'm using the referrer to parse the google string for the searchquery (&q= ) but as this is all in a hash-tag it wont be sent to the server and all i get is "http://www.google.de/".
So do you know a way of getting the query the user searched for, befor landing on my site?
Due to late-2011 Google security changes, this is no longer possible when the search was performed by a signed-in Google user. See:
http://googleblog.blogspot.com/2011/10/making-search-more-secure.html
http://analytics.blogspot.com/2011/10/making-search-more-secure-accessing.html
Since there are multiple q's in the query string you have to match the "q" parameter globally and take the last one:
/[?|&|#]q=([^&|^#]+)/ig
Get rid of "site:" searches (there are others, but I haven't done them)
/[\+|?|&]?site:([^&|^#])+/g, '');
Then parse the results.
/[\w^'\(\)\{\}]+|"[^"]+"/g
This has been working well for me.

Resources