Twilio fetch all call logs from last 5 days in ruby - ruby-on-rails

I want to fetch the call logs for the last 5 days, I read on the documentation
You can also specify an inequality, such as EndTime<=YYYY-MM-DD, to read calls that ended on or before midnight of this date
I am trying the following with no luck
#client.calls.list(to: phone_number, end_time: ">=#{Time.now - 5.days}")

Twilio developer evangelist here.
There are several things here, and I need to apologise for at least one of them.
Firstly, the less than/greater than equal that the Twilio API implemented was actually a bit of a hack with the way the parameters are formatted. The parameter as the docs point out is EndTime<=YYYY-MM-DD but this is made of the parameter name EndTime< and the parameter value YYYY-MM-DD separated by =. I apologise that this seemed like a cool hack but actually made things harder.
The Ruby library actually tries to unpick this and make it more sensible again. You can use the parameter end_time_after instead of trying to form the correct end_time format.
Second, I ran the string you were using for the end_time and it produced this:
irb(main):001:0> ">=#{Time.now - 5.days}"
=> ">=2021-02-28 14:35:44 +1100"
So when a time is stringified in Ruby, it doesn't just show up in the YYYY-MM-DD format.
So, to fix your API call, should format the date to YYYY-MM-DD and use end_time_after. Note, since you're using ActiveSupport, you can also call on 5.days.ago instead of Time.now - 5.days.
This should work for you:
#client.calls.list(to: phone_number, end_time_after: "#{5.days.ago.strftime("%Y-%m-%d")}")
Let me know if this helps at all.

Related

Bit.ly custom query - ruby gem

According to https://dev.bitly.com/link_metrics.html#v3_link_clicks,
we can send multiple parameters like link, unit, etc.
Here i am using this gem https://github.com/philnash/bitly
i am actually looking for Clicks by Day,
i tried
Bitly.client.clicks("bitly_short_url").clicks_by_day, works fine
but i need to change Time Zone value
i tried this
Bitly.client.info({"link" =>"bitly_short_url", "timezone" => -6}).clicks_by_day
and i am getting error.
Is there any other way to get result for different timezone?
Please help
Try this,
Bitly.client.clicks({"link" =>"bitly_short_url", "timezone" => -6}).clicks_by_day
info API doesn't take link or timezone as parameters. But, clicks API takes that. You are passing parameters of clicks API to info API. That is the reason you are getting errors.

Split datetime value received from external API in Rails app

I have a datetime value which comes from the API in this format: 2015-07-07T17:30:00+00:00. I simply want to split it up between the date and time values at this point. I am not using an Active Record model and I prefer not to use an sql database if I can.
The way I have set up the app means that the value is "stored" like this in my view: #search.dining_date_and_time
I have tried two approaches to solving this problem:
Manually based on this previous stackoverflow question from 2012: Using multiple input fields for one attribute - but the error I get is the attribute is "nil" even though I put a "try"
Using this gem, https://github.com/ccallebs/split_date_time which is a bit more recent and seems to be a more elegant solution, but after closely following the doc, I get this error, saying my Search model is not initalized and there is no method: undefined method dining_date' for #<Search not initialized>
This is when instead I put #search.dining_date in the view, which seems to be the equivalent of the doc's example (its not that clear). The doc also says the method will be automatically generated.
Do I need to alter my model so I receive the data from the API in another way? ie. not get the variable back as #search.dining_date_and_time from the Search model for any of this to work?
Do I need an Active Record model so that before_filter or before_save logic works - so i can (re)concatenate after splitting so the data is sent back to the API in a format it understands. Can I avoid this - it seems a bit of overkill to restructure the whole app and put in a full database just so I can split and join date/time as needed.
Happy to provide further details, code snippets if required.
As I am not using a conventional Rails DB like MySql Lite or Postgresql, I found that the best solution to the problem was by using this jQuery date Format plugin: https://github.com/phstc/jquery-dateFormat to split the date and time values for display when I get the data back from the API.
The Github docs were not too expansive, but once I put the simply put the library file in my Rails javascript assets folder, I just had to write a few lines of jQuery to get the result and format I wanted:
$(function() {
var rawDateTime = $('#searchDiningDateTime').html();
// console.log(rawDateTime);
var cleanDate = $.format.date(rawDateTime, "ddd, dd/MM/yyyy");
// console.log(cleanDate);
$('#searchDiningDateTime').html(cleanDate);
var cleanTime = $.format.date(rawDateTime, "HH:mm");
// console.log(cleanTime);
$('#searchTime').html(cleanTime);
});
Next challenge: rejoin the values on submit, so the API can read the data by sending/receiving a valid request/response. (The values can't be split like this when sent to the remote service).

Clojure - parse string to date

I'm trying to parse a unformatted string which contains a date (e.g. today = "08082013") to the format "08.08.2013".
This works:
(.parse (java.text.SimpleDateFormat. "ddMMyyyy") today) => <Date Sun Jan 01 00:00:00 CET 1950>
But when I do (.parse (java.text.SimpleDateFormat. "dd.MM.yyyy") today) I get the error "Unparseable date: "08082013"
Why? How can I get my desired date format?
To get from string to date, use parse.
To get from date to string, use format.
Both use a formatter to describe the transition.
=>(.format
(java.text.SimpleDateFormat. "dd.MM.yyyy")
(.parse
(java.text.SimpleDateFormat. "ddMMyyyy")
"08082013"))
"08.08.2013"
If you are playing around with date and time I recommend checking out this Clojure lib,
https://github.com/clj-time/clj-time
It is kind of the time lib most Clojure programmers use, and is based on the java lib joda time, that by many is agreed to be better than the Java build in one.
The .parse method of SimpleDateFormat will not generate a string, it will read a string and generate a java.util.Date object. If you want to generate a dotted string, you need SimpleDateFormat with the dots in place and call .format on it, given a java.util.Date.
See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html or take a look at clj-time
Using clj-time, you can use this:
(defn format-date
[date-str]
(f/unparse (f/formatter "dd.MM.yyyy") (f/parse custom-formatter date-str)))
(format-date "08082013") ;=> "08.08.2013"
Clojure is based on Java, so many of your options will end up calling Java in the end. The way I understand it, Java has two time apis. The old one is at java.text.SimpleDateFormat and is not recommended anymore. The new one is under java.time and is "much better".
With that in mind, you have several options for date manipulation in Clojure:
You can call either the new or old Java functions directly. NeilsK's answer provides examples on how to do this with the old api. You should be able to adapt it fairly easily to the new java.time api with some help from the Clojure documentation on java interop.
Everyone is saying use clj-time but I would not recommend it as it's based on a Java library called JodaTime which was deprecated when Java's new time API came out.
I personally have had a lot of success with a library called clojure.java-time which is a simple Clojure wrapper for Java's new time api.
juxt/tick looks very promising as it seeks to provide that new api in a clojure-friendly format across not only java platforms, but also in the browser and on dotnet. It was in alpha at the time of this writing but I will be keeping an eye on it.

JavaMail: how to get new messages comparing with time-stamps

I'm trying to get messages after a certain time-stamp, the way I've coded it was suggested by another programmer in this site:
GregorianCalendar date = new GregorianCalendar();
SearchTerm newer = new ReceivedDateTerm(ComparisonTerm.GT,date.getTime());
Message msgs[] = folder.search(newerThen);
The issue is that I get all the messages since the date, not the specific time. I was wondering if there is some work-around to emulate this. I mean, for an instance, if I want to get all the messages since today in the midday I would get those messages spicifically and not those ones received in today's morning.
Thanks in advance,
EDIT:
A new thought concerning to this: perhaps some date manipulation could do the job. I mean, comparing the minutes in the timestamp and filter programmatically those messages that don't fit the criteria. I know it's not the best way, but it could work.
PS: I'm using IMAP and trying to get mails from gmail, but I guess it should work no matter what the mail-server is.
Unfortunately, no. In this case, the IMAP protocol is being used by the JavaMail classes, and IMAP's SEARCH command takes only dates, not times (see the SINCE and SENTSINCE criteria).
You could use the setTime() method to query for some specific time.
Example:
setTime(timeInMilliseconds)

Ruby: How to check if a string is a valid time?

I'm pulling data from a feed that I have no control over and I need to verify if a string I'm given is a valid time.
Most of the time I'm correctly sent something like "2:35" or "15:41" but other times it's things like "AM" or "PM" (and no numbers)...so I ultimately just need to ignore those.
So, how can I verify if the data is a valid time?
You haven't exactly specified what you assume to be a valid time (e.g. whether you should accept optional seconds), so here's one guess:
data =~ /^([01]?[0-9]|2[0-3])\:[0-5][0-9]$/
Using Time.parse() is not a good solution, as shown in the example of the comments.
I'll leave the answer here for 'historical reasons', to keep the comments, and as a warning for future readers!
You can use Time.parse() and check for the ArgumentError exception for invalid times.
Extra advantage is that you also have the time in a usable format to work with if it is valid!

Resources