Friday, 19 February 2016

Sending push notification with APNS(Apple push notifications service) and rails

Hello There, I got a problem in which I have to send the push notification on iphones when some event occurs by the rails application. For this I have used rubygem 'grocer'. Here is the sample implementation for this:

1. Introduction: 

APNS(Apple Push Notification Service) The Apple Push Notification Service (APNs) is a service created by Apple Inc. that forwards notifications of third party applications to the Apple devices; such notifications may include badges, sounds or custom text alerts. It was launched together with iOS 3.0 on June 17, 2009.[1] In iOS 5's Notification Center added pushing local notifications. APNs was also added as an API to Mac OS X v10.7 "Lion" for developers to take advantage of,[2] and was improved in OS X 10.8 "Mountain Lion" with the introduction of Notification Center.

Apple announced the service on June 9, 2008 with a stated release for that September; however, as stated by Scott Forstall at the iOS 3.0 preview event on March 17, 2009, the rollout was delayed after a decision to restructure the APNs for scalability purposes due to the allegedly "overwhelming" response to the announcement of the APNs. At both events, Forstall stated that push notifications better conserve battery than background processes (which are used in pull technology) for receiving notifications.

2. Install Gem: 

Follow these steps:
1. install Gem by using `gem install grocer`. or write `gem 'grocer'` in side your Gemfile and run 
'bundle install'.

3. Sending notifications:

a. Create a pusher object to send notifications:
  @pusher ||= Grocer.pusher(
      certificate: _pem_file,
      passphrase: passphrase,
      gateway: gateway,
      port: port,
      retries: retries  )

where _pem_file => is the location of your certificate.pem file.
passphrase => is the passphrase while creating the pem file.
gateway => 
1.For development and staging: 'gateway.sandbox.push.apple.com'
2. For production : 'gateway.push.apple.com'

port => 2195
retries => is maximum number of retries.

b. Create notification object:

notification = Grocer::Notification.new(
    device_token: YOUR_DEVICE_TOKEN,
    alert: YOUR_MESSAGE,
    badge: 42,
    sound: 'siren.aiff')

where device_token is the device token on which you want to send the notification.

c. Send Notifications: 
@pusher.push(notification)

this will send a notification on your given device.

Thanks for reading this blog.

Friday, 12 February 2016

Working with CSV in jquery(Papa parse)


Papa Parse is a powerful CSV (delimited text) parser that gracefully handles large files.you can easily use this and can parse large csv files efficiently.you can get papa parse here.

here is short example of using  Papa Parse:

1.Converts csv to json:
var result = Papa.parse(csv_file)


2.Parse local CSV files:

 $('input[type=file]').parse({
config: {
             complete: function(results) {
                  console.log("Parse results:", results.data);
                 }
             }
           });
 3.Parse remote CSV files:
Papa.parse("http://example.com/file_name.csv", {
        download: true,
        complete: function(results) {
                         console.log("Remote file parsed!", results);
                       }
       });
4.Get data keyed by field name:
var results = Papa.parse(csv, {
                          header: true
                          });         

Friday, 8 May 2015

Using gem dropbox-sdk-ruby in Rails Application

Dropbox-sdk-ruby is a ruby library for the Dropbox core API.


1.Install Gem: you can run `gem install dropbox-sdk` from the terminal or  write following line in your gem file.

gem 'dropbox-sdk'

2. Using Gem: Create a file inside the config/initializers/ of your rails application.

There are following steps to use dropbox-sdk.

2.1 Require  gem inside your file:

 require 'dropbox_sdk'

2.2 Create an client_object of dropbox to use dropbox-api:

     @client = DropboxClient.new(YOUR_APP_SECRET_TOKEN)

2.3  Writing files to dropbox:

     @client.put_file(path_to_write, file, overwrite=true)

2.4 Read files to dropbox:

     @client.get_file(path, rev = revision)

Note: This is is onluy overview of this gem for more detail please click here.

Friday, 1 May 2015

Working with ruby Hash

A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type.

Hashes enumerate their values in the order that the corresponding keys were inserted.

Creating Hash: There are several ways to create a Hash. They are:

1.Implicit way: You can create a hash implicitly like:

_hash = { 'key1' =>  'value1' , 'key2' => 'value2' }

Hashes allow an alternate syntax form when your keys are always symbols. Instead of

_hash = { :key1 =>  'value1' , :key2 => 'value2' } 

You could write it as:

_hash = { key1:  'value1' , key2: 'value2' } 

2. Use ::new method to create Hash: You can also create hash  by ::new method.

_hash  = Hash.new

Default value of a Hash :  Hashes have a default value that is returned when accessing keys that do not exist in the hash. If no default is set nil is used. You can set the default value by sending it as an argument to ::new

 _hash  = Hash.new(10)

If you access a key which is not exist in the hash then default value will be return.In the above example

_hash[:key1]   // this will return 10 because key1 is not exists in hash, hence it will give result 10.

you can also set the default value as an array or another hash. Like:


_hash  = Hash.new([]) // setting default value as a blank array.

_hash[:a]    // will return a blank array.

_hash[:a]  << 20  // this will insert a value in default value that is an array.if you just print _hash then it is a blank hash no key in this hash but its default value has been changes from a blank array to a array that contains a value 20.


now if you print _hash[:b]  // will give result [20] because default value is changed.








Friday, 24 April 2015

Running rails application locally and make it available on public url


 However in practice you need to serve your local web server publicly.Its pretty good and easy to do that with the help of gem 'proxylocal'.

ProxyLocal proxies your local web-server and makes it publicly available over the internet. This application is splitted into client and server. The server is running on proxylocal.com. The client is written in ruby and distributed as gem.
  
1.Installation: Write inside your gem file gem 'proxylocal' or  run

gem install proxylocal inside your application.

2.Running your server: Just run your web server after that run following command 

 `proxylocal 3000 --host you_host_name` // assuming your server is running on port 3000.

now your local web server is available at:
http://ramlaxman.staging.t.proxylocal.com/


if you ar enot giving any host name in the command then proxylocal gem will give you a random path like `http://*.t.proxylocal.com/`.


references: proxylocal-gem.

Friday, 20 March 2015

How to serve a perticular sub url from different server by Nginx HTTP Proxy Pass

One reason to proxy to other servers from Nginx is the ability to scale out your infrastructure. Nginx is built to handle many concurrent connections at the same time. This makes it ideal for being the point-of-contact for clients. The server can pass requests to any number of backend servers to handle the bulk of the work, which spreads the load across your infrastructure. This design also provides you with flexibility in easily adding backend servers or taking them down as needed for maintenance.

Another instance where an http proxy might be useful is when using an application servers that might not be built to handle requests directly from clients in production environments. Many frameworks include web servers, but most of them are not as robust as servers designed for high performance like Nginx. Putting Nginx in front of these servers can lead to a better experience for users and increased security.

Here is an example of proxy pass in nginx:

The most straight-forward type of proxy involves handing off a request to a single server that can communicate using http.

example:

location /some_url {
    proxy_pass http://example.com;
}


In the above configuration, no URI is given at the end of the server in the proxy_pass definition. For definitions that fit this pattern, the URI requested by the client will be passed to the upstream server as-is.
when a request for /some_url/hello_url is handled by this block, the request URI will be sent to the example.com server as http://example.com/some_url/hello_url.

Sometimes, this kind of replacement is impossible. In these cases, the URI at the end of the proxy_pass definition is ignored and either the original URI from the client or the URI as modified by other directives will be passed to the upstream server.

For instance, when the location is matched using regular expressions, Nginx cannot determine which part of the URI matched the expression, so it sends the original client request URI. Another example is when a rewrite directive is used within the same location, causing the client URI to be rewritten, but still handled in the same block. In this case, the rewritten URI will be passed.

Example of proxy pass with headers:

location /learn {
rewrite   /learn(.*)$  $1/  break;
proxy_pass http://45.55.156.88:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}


when a request for /some_url ,the request URI will be sent to the example.com server as http://example.com/some_url.

Friday, 13 March 2015

Adding social share buttons in rails application by using 'shareable' gem

However in practice you need to add social sharing buttons inside your rails applications. Its very easy to add social buttons in rails application by using 'shareable' gem.

here is an example:

1. Install gem:

 put gem  'shareable' in your gem file.
then run bundle install.

2. Show buttons in rails view:
put <%= render_shareable %> in your view // this will add default buttons in your view.

3. Customizing buttons: you can customize buttons by using the helper methods.
 for more detail click on read more link. read more.