Rails Kitchen

It's a place to write on stuff I learned recently.

Autocomplete Using Chosen in Rails Active_admin

| Comments

In one of my recent project, I need to implement an autocomplete in active_admin interface to select one or more users from the user list. I tried googling on this and zeroed in on the library Chosen.Chosen by default supports multiple select, selected state.Chosen is available as a gem for Ruby on Rails which fits well with Asset Pipeline.
Include Chosen gem in your Gemfile.
1
gem ‘chosen-rails’
Once the gem is installed, include chosen javascript assets to your js file.
1
//= require chosen-jquery
In /app/admin/modelname.rb
1
2
3
4
5
6
7
8
9
10
ActiveAdmin.register Modlename do
  #Customize creates and edit form
  form do |f|
    f.inputs do
       f.input :name
       f.input :othermodel, :input_html => { :class => "chosen-input" } # other model with has_many relation ship
    end
    f.buttons
  end   
end
in active_admin.js
1
2
3
$(document).ready(function(){
   $(".chosen-input").chosen();
});
if you are using coffee script instead of js then use this
1
2
3
4
5
$ ->
  # enable chosen js
  $('.chosen-input').chosen
    allow_single_deselect: true
    no_results_text: 'No results matched'
Then, include Chosen stylesheet assets to your css file.
1
*= require chosen
To include more details check Here
Thats it, you will have a nice autocomplte with multiselect . Happy coding…

Comments

Lobna Mohamed
Thanks for the good post, but does not save data to the database
I guess it related to permit_params I am trying this
permit_params :name, :othermodel_ids
but still not working, have you another idea??

Changing Kannel Log Level & Log Rotation

| Comments

If Kannel is configured so that the bearerbox, wapbox and/or smsbox log to file each of these log files will continue to grow unless administered in some way.  One way of reducing logs is by change log level. kannel allow to have 5 log-level ( 0 - debug, 1 - info, 2 - warning, 3 - error, 4 - panic). We can change log level by editing log-level component in configuration file and also done by admin HTTP command without restaring bearerbox.
1
http://localhost:13000/log-level?password=bar&level=2
Another way of administering log files is to ‘rotate’ the logs on a regular basis using a tool such as logrotate. A sample logrotate script is shown below.
Setup Logrotate

Step 1 : If logrotate is not already on your VPS, install it now.
1
sudo apt-get install logrotate
Step 2 : Add following script in file /etc/logrotate.d
1
2
3
4
5
6
7
8
9
10
11
12
/var/log/kannel/*.log {daily
                         missingok
                         rotate 365
                         compress
                         delaycompress
                         notifempty
                         create 640 kannel adm
                         sharedscripts
                         postrotate
                         killall -HUP bearerbox smsbox wapbox || true > /dev/null 2> /dev/null
                         endscript
                        }
In this example the Kannel log files found in /var/log/kannel are rotated and compressed daily over 365 days. To verify if a particular log is rotating or not and to check the last date and time of its rotation, check the /var/lib/logrotate/status file.
1
cat /var/lib/logrotate/status

Installing and Configuring SQL-Based Queue Engine for Kannel (SQL Box)

| Comments

SQL box is a special Kannel box that sits between bearer box and SMS box and uses a database queue to store and forward messages. Messages are queued on a configurable table (defaults to send_sms) and moved to another table (defaults to sent_sms) afterwards.
After installing gateway, we need to compile SQL box.
Steps:

  1 - cd to directory where Kannel is unzipped and then type the following commands
1
2
3
4
cd addons/sqlbox/
sudo ./bootstrap
sudo ./configure --with-kannel-dir=/usr/local/kannel-1.5.0/
sudo make bindir=/usr/local/kannel/kannel-1.5.0/
2 -Create a file named smsbox.conf and the following configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#SQL BOX CONFIG

group = sqlbox
id = sqlbox-db
smsbox-id = sqlbox
global-sender = ""
bearerbox-host = localhost
bearerbox-port = 13010
smsbox-port = 13020
smsbox-port-ssl = false
sql-log-table = sent_sms
sql-insert-table = send_sms
log-file = "/var/log/kannel/kannel-sqlbox.log"

# Database connection examples. Please uncomment as needed
# Example MYSQL Connection

group = mysql-connection
id = sqlbox-db
host = localhost
username = username
password = password
database = kannel_db
3 - create  send_sms table using following sql command
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
CREATE TABLE `send_sms` (
                          `sql_id` bigint(20) NOT NULL auto_increment,
                          `momt` enum('MO','MT') default NULL,
                          `sender` varchar(20) default NULL,
                          `receiver` varchar(20) default NULL,
                          `udhdata` blob,<br />  `msgdata` text,
                          `time` bigint(20) default NULL,
                          `smsc_id` varchar(255) default NULL,
                          `service` varchar(255) default NULL,
                          `account` varchar(255) default NULL,
                          `id` bigint(20) default NULL,
                          `sms_type` bigint(20) default NULL,
                          `mclass` bigint(20) default NULL,
                          `mwi` bigint(20) default NULL,
                          `coding` bigint(20) default NULL,
                          `compress` bigint(20) default NULL,
                          `validity` bigint(20) default NULL,
                          `deferred` bigint(20) default NULL,
                          `dlr_mask` bigint(20) default NULL,
                          `dlr_url` varchar(255) default NULL,
                          `pid` bigint(20) default NULL,
                          `alt_dcs` bigint(20) default NULL,
                          `rpi` bigint(20) default NULL,
                          `charset` varchar(255) default NULL,
                          `boxc_id` varchar(255) default NULL,
                          `meta_data` varchar(255) default NULL,
                          `foreign_id` bigint(20) default NULL,
                          `binfo` varchar(255) default NULL,
                          PRIMARY KEY (`sql_id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1;
4 - Create  sent_sms table using same structure with changing table name
Now start bearerbox and sqlbox using commands
1
2
sudo /usr/local/kannel-1.5.0/sbin bearerbox /home/shaiju/kannel/kannel.conf
sudo /usr/local/kannel-1.5.0/sbin sqlbox /home/shaiju/kannel/sqlbox.conf
Example
As when you’re using the sendsms interface, you don’t need to specify all the columns in order to succesfully enqueue a message.
Here’s an example query you can use to send a simple message using Sqlbox:
1
INSERT INTO send_sms (  momt, sender, receiver, msgdata, sms_type, smsc_id) VALUES (  'MT', 'TESTID', '9995323922', 'Hello world', 2,'testsmsc1');
The former example would send a message with text “Hello world” to number “9995323922”. If possible, the sender would be set to “TESTID”.You can add other parameters to specify routing, charset encoding and any other settings your setup may require
If you are keen to learn more about this , Refer Here

Kannel Monitoring Tool - Ruby Gem

| Comments

Last few months I have been facing lots of issues in Kannel SMS gateway.Some time sms are getting queued up, in some other time smsc went to the reconnecting stage. I searched for a tool which will monitor Kannel status unfortunately I didn’t find a tool to monitor and give an alert. so decided to write a tool which will monitor Kannel activities.
1
kannel_monitor 0.0.2
This gem will monitor
  •    kannel status
  •    kannel sms queue
  •    smsc status
  •    smsc queue and will send a email alerts when something went wrong.
Just install gem in server using
1
gem install kannel_monitor
run monitoring activity using command
1
kannel_monitor 'path to kannel_monitor.yml'
Example of yml file is given Here.
you need to set a crone job to run this in a specific intervals. If a smsc is permanently reconection stage and you don’t want to monitor that smsc, then you can skip that particular smsc by adding in sms_to_be_skipped 

A Gem for Generating Custom Error Pages in Rails Application

| Comments

Writing a Ruby Gem was my long time wish, but i couldn’t find an idea to make a gem. Finally got it, for which i didn’t find a gem, a generator which will automatically generate a custom code for handling 404 and 500 errors and pages in rails application (Thanks to Sreekanth for noting there is no  gem for this taks). Here it is 
1
rails_error_pages 1.0.5
How to use :
Simply add it to your Gemfile and bundle install.
1
gem 'rails_error_pages'
Next step is to use a generator, to add auto generated code into our application using
1
rails g rails_error_pages
That’s it! run your application and try with a false URL.

Configuring MySQL DLR Storage

| Comments

To support mysql, we have to recompile kannel with –mysql
1
./configure --prefix=/usr/local/kannel --with-mysql
To store DLR information into a MySQL database you may use the dlr-storage = mysql configuration directive in the core group.In addition to that you must have a dlr-db group defined that specifies the table field names that are used to the DLR attributes and a mysql-connection group that defines the connection to the MySQL server itself.
Here is the example configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
group = core
.........
dlr-storage = mysql
group = mysql-connection
id = mydlr
host = localhost
username = foo
password = bar
database = dlr
max-connections = 1
group = dlr-db
id = mydlr
table = dlr
field-smsc = smsc
field-timestamp = ts
field-destination = destination
field-source = source
field-service = service
field-url = url
field-mask = mask
field-status = status
field-boxc-id = boxc
Create table dlr

1
2
3
4
5
6
7
8
9
CREATE TABLE dlr (smsc varchar(40),
                  ts varchar(40),
                  destination varchar(40),
                  source varchar(40),
                  service varchar(40),
                  url varchar(255),
                  mask int(10),
                  status int(10),
                  boxc varchar(40))

Kannel Installation and Configuration as SMS Gateway Server

| Comments

Kannel is an open source WAP gateway and  SMS gateway for GSM networks.  The main use for Kannel is to link HTTP based services to various SMS centers using obscure protocols.Kannel is mainly being developed on Linux systems, and should be fairly easy to port to other Unix-like systems. However, kannel don’t yet support other platforms.

Kannel Installation and Configuration as SMS gateway Server
Installation
1 Before installing kannel you will need to install libxml. Download the source and follow the next steps for installation.We can download it from following link. ftp://gd.tuwien.ac.at/pub/libxml/
2 Unzip it into some directory
3 Cd into directory where libxml is unzipped and then type the following commands
4 Type ./configure
5 Type make
6 Type make install
   now you have libxml
7 Download latest version of Kannel http://www.kannel.org/download.shtml and Unzip it into some directory  or use svn co https://svn.kannel.org/gateway/trunk  to download latest version of gateway
8 cd into directory where Kannel is unzipped and then type the following commands
9 Type  ./configure –prefix=/usr/local/kannel-1.5.0 –enable-start-stop-daemon –with-mysql
10 Type make
11 Type make install

 Now kannel is ready

Next step is to add configuration

Kannel consists of three programs, usually run as  daemons:  bearerbox, wapbox, and smsbox. They implement the connection to the phone (SMS or UDP), the WAP protocol stack, and the SMS based services, respectively.

We will need only  bearerbox and smsbox as we are going to setup it as SMS gateway server. Bearerbox is the main queue and smsbox handles the smscs.

Create a file named kannel.conf and add following configuration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#---------------------------------------------
# CORE
#
# There is only one core group and it sets all basic settings of the bearerbox (and system).
group = core
admin-port = 13000
admin-password = passwd##
smsbox-port = 13010
log-file = "/var/log/kannel/kannel.log"
# Log level is DEBUG
log-level = 
access-log = "/var/log/kannel/access.log"
store-type = spool
# Let no SMS be lost as far as possible
store-location = "/var/spool/kannel"
# Acceptable retries for temporary failures in sending SMS
sms-resend-retry = 3
#---------------------------------------------
# SMSBOX SETUP
#
# Smsbox(es) do higher-level SMS handling after they have been received from SMS centers by #bearerbox, or before they are given to bearerbox for delivery
group = smsbox
bearerbox-host = localhost
sendsms-port = 13020
log-file = "/var/log/kannel/smsbox.log"
# Log level is DEBUG
log-level = 0
access-log = "/var/log/kannel/access.log"
#---------------------------------------------
# SEND-SMS USERS
# These users are used when Kannel smsbox sendsms interface is used to send PUSH sms messages
group = sendsms-user
username = tester
password = foobar
concatenation = true
max-messages = 4
#---------------------------------------------
# SMSC CONNECTIONS
# SMSC connections are created in bearerbox and they handle SMSC 
# protocol and message relying.
# Example complete configuration
group = smsc
smsc = smpp
smsc-id = PROMO-SERVICE-MESSAGE
allowed-smsc-id = PROMO-SERVICE-MESSAGE
host = X.X.X.X
port = XXXX
transceiver-mode = true
smsc-username = promouser
smsc-password = promopasswd
source-addr-ton = 0
source-addr-npi = 1
dest-addr-ton = 0
dest-addr-npi = 1
system-type = ""
address-range = ""
Now start SMS box and bearer box using
1
2
sudo /usr/local/kannel-1.5.0/sbin/bearerbox /home/shaiju/kannel/kannel.conf
sudo /usr/local/kannel-1.5.0/sbin/smsbox /home/shaiju/kannel/kannel.conf
To see running process use
1
sudo ps -ef | grep kannel
To stop the kannel service
1
/etc/init.d/kannel stop
To see kannel logs
1
tail -f /var/log/kannel/kannel.log
Now you can send sms using send sms url
1
http://kannel.machine.ip:13010/cgi-bin/sendsms?username=tester&password=foobar&from=SENDERID&to=9999999999&text=test sms &smsc=PROMO-SERVICE-MESSAGE
If you are keen to learn more about this , Refer Here
For Sample kannel configuration, check Here

Comments

Anthony
Thanks for this. The send SMS from kannel is not any troublesome. Would you do a guide on receiving SMSs? I have tried everything and but still I cannot received SMS from the same e220 modem that works fine sending from kannel. Thanks again, you guide so far has been very usefull
Sreekanth G S
Start bearerbox first. Smsbox needs to connect to an already running bearerbox instance.