Ruby Dial Plans

Support zone
jack9901
Posts: 277
Joined: Tue Jan 29, 2008 7:30 pm

Post by jack9901 » Tue Apr 01, 2008 7:55 am

Try the following,

Code: Select all

def is_north_america
   return sys.RegexMatch(req.URI.User, "^1[2-9][0-9][0-9][2-9][0-9][0-9][0-9][0-9][0-9][0-9]$")
end

sys.Log(req.URI.User) if is_north_america  # this line is working, e.g. dial 16465551212
sys.Log(req.URI.User) if is_north_america  # the same thing is not working this time
It seems that sys.RegexMatch does not like to be used a second time in a method. It took some time to identify the problem in a more complex code that everything looked fine and the code lacked good debugging. :( Or maybe there is a simple mistake that I am not aware.

jack9901
Posts: 277
Joined: Tue Jan 29, 2008 7:30 pm

Post by jack9901 » Tue Apr 01, 2008 8:00 am

I guess markcs was trying to identify the string that starts with "*" using sys.RegexMatch(req.URI.User, "^*"). I tried "^*" and "^\*", both were working fine. Might be some other problem.

jack9901
Posts: 277
Joined: Tue Jan 29, 2008 7:30 pm

Post by jack9901 » Tue Apr 01, 2008 8:17 am

Another small trick, req.URI.User.to_str[2..-1] == ${EXTEN:2}. Here -1 is rewinding to the end of the string, so I don't have to define a Max_number_length.

The .Net String does have more functionality. For the time being, I'll stay with Ruby String for the simplicity.

Aaron
Site Admin
Posts: 4652
Joined: Thu Jul 12, 2007 12:13 am

Post by Aaron » Tue Apr 01, 2008 8:18 am

In theory a regular expression of ^* is invalid.

Without being escaped ^ means the start of the string and * is a 0 or more quantifier. Since you can't quantify ^ it's an invalid expression. How a particular regular expression engine will process it is unknown.

As jack9901 points out the correct form is ^\* with the \ escaping the * so it's a literal rather than a quantifier.

Regards,

Aaron

Aaron
Site Admin
Posts: 4652
Joined: Thu Jul 12, 2007 12:13 am

Post by Aaron » Tue Apr 01, 2008 9:04 am

jack9901 wrote:Try the following,

Code: Select all

def is_north_america
   return sys.RegexMatch(req.URI.User, "^1[2-9][0-9][0-9][2-9][0-9][0-9][0-9][0-9][0-9][0-9]$")
end

sys.Log(req.URI.User) if is_north_america  # this line is working, e.g. dial 16465551212
sys.Log(req.URI.User) if is_north_america  # the same thing is not working this time
It seems that sys.RegexMatch does not like to be used a second time in a method. It took some time to identify the problem in a more complex code that everything looked fine and the code lacked good debugging. :( Or maybe there is a simple mistake that I am not aware.
Hi jack9901,

I don't think it's the sys.RegexMatch causing that problem. The code below at the top of a dial plan works correctly.

Code: Select all

#Ruby
sys.Log("Matched .* try 1") if sys.RegexMatch(req.URI.User, ".*")
sys.Log("Matched .* try 2") if sys.RegexMatch(req.URI.User, ".*")
Regards,

Aaron

markcs
Posts: 42
Joined: Sat Feb 16, 2008 5:44 pm

Post by markcs » Tue Apr 01, 2008 1:17 pm

jack9901 wrote:I guess markcs was trying to identify the string that starts with "*" using sys.RegexMatch(req.URI.User, "^*"). I tried "^*" and "^\*", both were working fine. Might be some other problem.
jack9901 => Thats exactly what I was trying to do. I thought I had tried with "^\*" as thats how I would do it in perl... I will have to try again and see where I went wrong....

Do you have any tips on how to extract the HOST from req.Header.From.FromURI? This gets the value number@sip.provider.com. I want to be able to react on the incoming calls HOST address (sip.provider.com in this case). There is no req.Header.From.FromHost implemented. I tried a few things I found on the net with working with strings but they didn't work for me....

BTW: are there any good sites which describe what is implemented in iron ruby and tips on usage?

Thanks! I'm enjoying this.....

jack9901
Posts: 277
Joined: Tue Jan 29, 2008 7:30 pm

Post by jack9901 » Tue Apr 01, 2008 4:59 pm

markcs, try this:

from_uri = req.Header.From.FromURI.ToString.to_str
from_uri_user = from_uri[from_uri.index("@")+1..-1]
from_uri_host = from_uri[4..from_uri.index("@")-1]
from_name = req.Header.From.FromName.ToString.to_str

jack9901
Posts: 277
Joined: Tue Jan 29, 2008 7:30 pm

Post by jack9901 » Tue Apr 01, 2008 5:07 pm

Aaron wrote:

Code: Select all

#Ruby
sys.Log("Matched .* try 1") if sys.RegexMatch(req.URI.User, ".*")
sys.Log("Matched .* try 2") if sys.RegexMatch(req.URI.User, ".*")
Aaron
That's what I've tested. And if I use any Ruby String method in the subroutine(method) is_north_america to do the work, no problem at all. That's why it took quite some time to track down the strange thing.

Aaron
Site Admin
Posts: 4652
Joined: Thu Jul 12, 2007 12:13 am

Post by Aaron » Tue Apr 01, 2008 9:28 pm

markcs wrote: Do you have any tips on how to extract the HOST from req.Header.From.FromURI? This gets the value number@sip.provider.com. I want to be able to react on the incoming calls HOST address (sip.provider.com in this case). There is no req.Header.From.FromHost implemented. I tried a few things I found on the net with working with strings but they didn't work for me....
The FromURI is a SIPURI object and has a string property called Host so you can use req.Header.From.FromURI.Host.

Regards,

Aaron

Aaron
Site Admin
Posts: 4652
Joined: Thu Jul 12, 2007 12:13 am

Post by Aaron » Wed Apr 02, 2008 6:32 am

[quote="jack9901"]

Code: Select all

sys.RegexMatch(req.URI.User, "^1[2-9][0-9][0-9][2-9][0-9][0-9][0-9][0-9][0-9][0-9]$")
I've encountered a snag getting regular expression strings from the Ruby script back to the sipswitch. The strings are not getting passed back literally and regex escape patterns are not making it through. The workaround is to use replace any case where normally \ would be used with \\.

As an example the above regex could be written as:

Code: Select all

sys.RegexMatch(req.URI.User, "^1[2-9]\\d{2}[2-9]\\d{6}$")
Regards,

Aaron

Locked