Sunday, 9 February 2020

Create React App in linux system

Creating React Application

To install react run
$  sudo npm install -g create-react-app

Install nde dependencies
$  sudo npm i -g npx
$  sudo apt install nodejs-legacy
$  sudo npm install

Check the below items are installed in your system
$ node -v
$  npx -v
$  npm -v

Install latest version of node
$ sudo npm install -g n
$ sudo n stable

Create the react app
$ npx create-react-app rajfirstreact

Go to the app folder
$ cd rajfirstreact

Run  the react application
$ npm start

You can see your react application running in port 3000
http://localhost:3000

Thursday, 30 January 2020

MailCatcher for Development mail checking in Rails

MailCatcher runs a super simple SMTP server which catches any message sent to it to display in a web interface. Run mailcatcher, set your favourite app to deliver to smtp://127.0.0.1:1025 instead of your default SMTP server, then check out http://127.0.0.1:1080 to see the mail that's arrived so far.
  1. gem install mailcatcher
  2. mailcatcher
  3. Click on http://127.0.0.1:1080
Add the below code in environments/development.rb

config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 }
You can trigger any mail then it will display immediately.

Thursday, 26 December 2019

Importing large data from Xls sheet with background job in rails

In controller need to add the import method with background job.

def import  
new_filename = "#{File.basename(params[:file].original_filename)}_new"
new_filepath = File.join(Dir.tmpdir, new_filename)
FileUtils.cp(params[:file].path, new_filepath)
BackJob.perform_later(new_filepath, 
File.extname(params[:file].original_filename))
redirect_to root_url, notice: 'It take sometime to import'
rescue StandardError => e  puts e.inspect
end

In view index.html.erb add the import button with files.

<%= form_tag import_lists_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>


In BackJob.rb add the import method for storing the data from xls

class BackJob < ApplicationJob
queue_as :default

def perform(fpath, file)
import(fpath, file)
end

def import(filepath, extname)
   spreadsheet = open_spreadsheet(filepath, extname)
    columns = %w[column_one column_tow ]
    (2..spreadsheet.last_row).each do |i|
      row = Hash[[db_columns, spreadsheet.row(i)].transpose]
      obj = ModelName.new
      obj.attributes = row.to_hash.slice(*columns)
      obj.save
   end
end


def open_spreadsheet(filepath, extname)
case extname    
when '.csv'      Roo::Csv.new(filepath)
when '.xls'      Roo::Excel.new(filepath)
when '.xlsx'      Roo::Excelx.new(filepath, file_warning: :ignore)
else raise 'Unknown file type: extname'
end
end
end

Monday, 16 December 2019

How to sort the array of string by alphabetical order with domain based components in ruby?


Input:

array = ["analytics.ramo.com", "ccc.ramo.com", "ccc.test.ramo", "ddd.kumar.ramo.com", "ec2.new.ramo.com", "ramo.com", "test-new.ramo.com", "top-test.ramo.com"] 

Solution:

array.sort_by do |domain|
  parts = domain.split('.').reverse
  parts.unshift(parts.count)
end


Output:

["ramo.com",
 "analytics.ramo.com",
 "ccc.ramo.com",
 "test-new.ramo.com",
 "top-test.ramo.com",
 "ccc.test.ramo",
 "ddd.kumar.ramo.com",
 "ec2.new.ramo.com"]

Monday, 4 November 2019

How to use module as Namespace in ruby?

 Module or Mix-in used as a Namespace in ruby. It is used to avoid collision between two classes.


module Rajkumar
    class Uthayaa
        attr_accessor :email

        def display_email
            puts "my email is #{@email}"
        end
    end
end

module Somu
    class Uthayaa
        attr_accessor :email

        def display_email
            puts "my email 2 is #{@email}"
        end
    end
end

puts raj = Rajkumar::Uthayaa.new
puts raj.email='rajutaya@gmail.com'
puts raj.display_email
puts raj = Somu::Uthayaa.new
puts raj.email='lalalslsls@gmail.com'
puts raj.display_email

How Database index is functioning?


Indexing is a way to optimize the performance of a database by minimizing the number of disk accesses required when a query is processed. It is a data structure technique which is used to quickly locate and access the data in a database.

Indexes are created using a few database columns.
  1. The first column is the Search key that contains a copy of the primary key or candidate key of the table. These values are stored in sorted order so that the corresponding data can be accessed quickly.
  2. Note: The data may or may not be stored in sorted order.
  3. The second column is the Data Reference or Pointer which contains a set of pointers holding the address of the disk block where that particular key value can be found.


Sunday, 3 November 2019

How to install skype on ubuntu 16.04


Kindly run the below commands in terminal for skype installation.

1. Add skype linux repository in your system.

   $ echo "deb [arch=amd64] https://repo.skype.com/deb stable main" | sudo tee /etc/apt/sources.list.d/skype-stable.list

2. To fetch and install Skype public key so that APT package manager verify from repository.

   $ wget https://repo.skype.com/data/SKYPE-GPG-KEY
$ sudo apt-key add SKYPE-GPG-KEY

3. To use https
$ sudo apt install apt-transport-https

4. Update the software source and install skype
$ sudo apt update
 $ sudo apt install skypeforlinux

5. Once installed type skypeforlinux in your application. You can use your skype.

Interactor in Rails

What is interactor? Interactor provides a common interface for performing complex user interactions An interactor is a simple, sin...