Tuesday, 25 July 2017

Code Customization

https://github.com/eliotsykes/rails-security-checklist
https://www.owasp.org/index.php/Ruby_on_Rails_Cheatsheet
https://www.netsparker.com/blog/web-security/ruby-on-rails-security-basics/

Heroku Commands


  • db:migrate runs (single) migrations that have not run yet.
  • db:create creates the database
  • db:drop deletes the database
  • db:schema:load creates tables and columns within the (existing) database following schema.rb
  • db:setup does db:create, db:schema:load, db:seed
  • db:reset does db:drop, db:setup
Typically, you would use db:migrate after having made changes to the schema via new migration files (this makes sense only if there is already data in the database). db:schema:load is used when you setup a new instance of your app.
I hope that helps.

UPDATE for rails 3.2.12:
I just checked the source and the dependencies are like this now:
  • db:create creates the database for the current env
  • db:create:all creates the databases for all envs
  • db:drop drops the database for the current env
  • db:drop:all drops the databases for all envs
  • db:migrate runs migrations for the current env that have not run yet
  • db:migrate:up runs one specific migration
  • db:migrate:down rolls back one specific migration
  • db:migrate:status shows current migration status
  • db:rollback rolls back the last migration
  • db:forward advances the current schema version to the next one
  • db:seed (only) runs the db/seed.rb file
  • db:schema:load loads the schema into the current env's database
  • db:schema:dump dumps the current env's schema (and seems to create the db as well)
  • db:setup runs db:schema:load, db:seed
  • db:reset runs db:drop db:setup
  • db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration
  • db:migrate:reset runs db:drop db:create db:migrate
WunderGround API: 

wu_data.rb

# encoding: UTF-8

require "json"
require "open-uri"
require_relative 'wu'
require_relative 'forecast_io'

class WuData
  URL_API = "http://54.68.32.124"
  def initialize
    places = open(URL_API+"/sync_places").read
    @places = JSON.parse places
  end

  def get_wu
    @wu_data = {}
    @places.each do |place|
      query_start_time = DateTime.now
      _coords = "#{place["location"][0]}"+","+"#{place["location"][1]}"
      puts "#{_coords}===================="
      wu = Wu.new(_coords)
      wu_obj = wu.report
      @wu_data[place["id"]] = wu_obj
      sleep 1
      puts "  -> #{((DateTime.now - query_start_time) * 24 * 60 * 60).to_f}s"
    end
  end

  def wu_json
    File.open("wu_json/wu_data.json","w") do |f|
      f.write(@wu_data.to_json)
    end
  end
end

wu = WuData.new
wu.get_wu
wu.wu_json


sync_data.rb


Phone type format with javascript

<script type='text/javascript' src='http://digitalbush.com/wp-includes/js/jquery/jquery.js?ver=1.11.0'></script>
<script type="text/javascript" src="http://digitalbush.com/wp-content/uploads/2014/10/jquery.maskedinput.js"></script>


<script>
    jQuery(function($){       
       $(".phone").mask("(999) 999-9999");
    });
</script>    

File Handling operations (Read, Write, Open)


controller:-i
       f !Dir.exists?("public/default_sms")
          default_sms(params)
        else
          update_sms(params)
        end


  def default_sms(params)
    FileUtils.mkdir_p "#{Rails.public_path}/default_sms"
    @team_sms = File.open(Rails.root.join("public/default_sms","team_sms.txt"), 'w') do |file|     
      file.write(params[:notification][:team_sms])
    end
  end

  def update_sms(params)
    FileUtils.mkdir_p "#{Rails.public_path}/update_sms_#{current_user.id}"
    @team_sms = File.open(Rails.root.join("public/update_sms_#{current_user.id}","team_sms_#{current_user.id}.txt"), 'w') do |file|     
      file.write(params[:notification][:team_sms])
    end
end


View: check whether exists or not
    Dir.glob("public/update_sms_#{current_user.id}").present? ? File.read(Rails.root.join("public/update_sms_#{current_user.id}/team_sms_#{current_user.id}.txt")) : Dir.glob("public/default_sms").present? ? File.read(Rails.root.join("public/default_sms/team_sms.txt")) : nil
Gemfile aptualruby


source 'https://rubygems.org'

gem 'rails', '3.2.14'

gem 'mongoid'#, "3.1.5"
gem 'devise', '2.2.8'
gem "devise-async"
gem 'devise_invitable', '~> 1.1.8'
# gem 'devise_invitable', :git => 'git@github.com:scambra/devise_invitable.git'

gem "redis"
#for using redis as cache store
gem 'redis-rails'
gem 'mongoid-history'
gem 'whenever', :require => false

gem "resque", "1.25.1"
#NEDD vlad gem on every server since it is added in RakeFile
gem "vlad", :require => false
gem "vlad-git", :require => false
gem "mailcatcher"

if RUBY_PLATFORM =~ /mingw32/
  gem 'mini_magick', :path => '../minimagick', :ref => '6d0f8f953112cce6324a524d76c7e126ee14f392'
else
  gem "mini_magick"
  # Use unicorn as the app server
  gem 'unicorn'
end
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'

gem 'jquery-rails'

gem "nested_form"

group :development do
  gem "better_errors"
  gem "thin"
  gem 'debugger'
end

gem 'embedly'

gem "linkedin"
gem "omniauth"
gem "omniauth-linkedin"
gem "omniauth-facebook"
gem 'omniauth-twitter'
gem 'devise-encryptable'

Send mail with mailgun


Follow this link=>
http://www.leemunroe.com/send-automated-email-ruby-rails-mailgun/

or

1. create your credentials=>
(config/development.rb)
config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :authentication => :plain,
    :address => "smtp.mailgun.org",
    :port => 587,
    :domain => "sandboxec6b35c5c7ad40ff9a7fc2959942ec87.mailgun.org",
    :user_name => "postmaster@sandboxec6b35c5c7ad40ff9a7fc2959942ec87.mailgun.org",
    :password => "764e70a2f8cb5ec1611e843fb1cd54c8"
  }



2. Generate your mailer by typing this in Terminal
rails g mailer model_mailer new_record_notification

3. Generate your mailer by typing this in Terminalrails g mailer model_mailer new_record_notification
4. app/mailers/model_mailer.rb and change the default from email:
default from: "me@MYDOMAIN.com"
def new_record_notification(record)
  @record = record
  mail to: "recipient@MYDOMAIN.com", subject: "Success! You did it."
end

5. app/views/model_mailer/new_record_notification.text.erb
Hi,

A new record has been added: <%= @record.name %>

Thanks

6. Send the email(in controller)

def create
  @record = Record.new
    
  if @record.save
    ModelMailer.new_record_notification(@record).deliver
    redirect_to @record
  end
end


Dropbox uploading files


Dropbox uploading files=>
1. create developer account on dropbox (create credentials).

2. put credentials in app.rb
DROPBOX_APP_KEY = "1ikbatyv1u00ikq"
DROPBOX_APP_KEY_SECRET = "1bcaudn17ab9ach"
DROPBOX_APP_MODE = "dropbox"

3.  create routes
match '/home/authorize'   => 'home#authorize' , :via => [:get,:post] , :as => :dropbox_auth
  match '/home/callback' => 'home#callback' , :via => [:get,:post] , :as =>  :dropbox_callback

4. Logic in cotroller
def authorize  
dbsession = DropboxSession.new(DROPBOX_APP_KEY, DROPBOX_APP_KEY_SECRET)
#serialize and save this DropboxSession
session[:dropbox_session] = dbsession.serialize
filename =  params[:file].original_filename
extension = filename.split('.').last
tmp_file = "#{Rails.root}/tmp/#{filename}"
File.open(tmp_file, 'wb') do |f| f.write params[:file].read end
#pass to get_authorize_url a callback url that will return the user here
redirect_to dbsession.get_authorize_url url_for(:action => 'callback',:file => tmp_file)
end
  def callback
dbsession = DropboxSession.deserialize(session[:dropbox_session])
# debugger
if params[:not_approved].blank?
dbsession.get_access_token 
dbsession = DropboxSession.deserialize(current_user.dropbox_session)
# create the dropbox client object
client = DropboxClient.new(dbsession, DROPBOX_APP_MODE)

data = File.read(params[:file])

# client.put_file("test.png",data)
client.put_file(params[:file],data)
session[:dropbox_session] = dbsession.serialize
current_user.update_attributes(:dropbox_session => session[:dropbox_session])
session.delete :dropbox_session
flash[:success] = "You have successfully authorized with dropbox."
end
redirect_to root_url
  end


Box.net uploading fles=>
image and video validation with paperclip

validates_attachment_presence :source
  validates_attachment_content_type :source,
    :content_type => ['video/mp4'],
    :message => "Sorry, right now we only support MP4 video",
    :if => :is_type_of_video?
  validates_attachment_content_type :source,
     :content_type => ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'],
     :message => "Different error message",
     :if => :is_type_of_image?
  has_attached_file :source

  protected
  def is_type_of_video?
    source.content_type =~ %r(video)
  end

  def is_type_of_image?
    source.content_type =~ %r(image)
  end
Form fields Validation
 validates_length_of :first_name, maximum: 30
  validates_length_of :last_name, maximum: 30, message: "less than 30 if you don't mind"
  validates_length_of :fax, in: 7..32, allow_nil: true
  validates_length_of :phone, in: 7..32, allow_blank: true
  validates_length_of :user_name, within: 6..20, too_long: 'pick a shorter name', too_short: 'pick a longer name'
  validates_length_of :zip_code, minimum: 5, too_short: 'please enter at least 5 characters'
  validates_length_of :smurf_leader, is: 4, message: "papa is spelled with 4 characters... don't play me."
  validates_length_of :essay, minimum: 100, too_short: 'Your essay must be at least 100 words.',
                      tokenizer: ->(str) { str.scan(/\w+/) }

Fetch gmail contacts


script type="text/javascript" src="http://www.google.com/jsapi"></script>
     <script src="https://apis.google.com/js/client.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
      <script>
      function auth() {
      var config = {
      'client_id': '243480442475-05gg5q6tq1qv9derqadd3l4qcegusqn8.apps.googleusercontent.com',
      'scope': 'https://www.google.com/m8/feeds/'       
      };
      gapi.auth.authorize(config, function() {
      var contacts = fetch(gapi.auth.getToken());
      alert(contacts)
      });
      }
      
      function fetch(token) {
      $.ajax({
      url: 'https://www.google.com/m8/feeds/contacts/default/full?alt=json',
      dataType: 'jsonp',
      data: token
      }).done(function(data) {
      console.log(JSON.stringify(data));
      });
      }
      </script>

      

      <button onclick="auth();">GET CONTACTS FEED</button>

Facebook Share

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({appId: '782543028437100', status: true, cookie: true,
xfbml: true});
};
(function() {
var e = document.createElement('
script'); e.async = true;
e.src = document.location.protocol +
'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);
}());
</script>


<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>



<h1>Listing products</h1>

<table>
  <tr>
    <th>Name</th>
    <th>Description</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @products.each do |product| %>
  <tr>
 
    <td><%= product.name %></td>
    <td><%= product.description %></td>
    <%#= social_share_button_tag(product.name, :url => "http://localhost:3000/products/#{product.name}") %>
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, confirm: 'Are you sure?', method: :delete %></td>
  </tr>

  <a href="shareere" id = "share_button">shhhhh</a>

<script type="text/javascript">
$(document).ready(function(){
$('#share_button').click(function(e){
e.preventDefault();
FB.ui(
{
method: 'feed',
name: '<%=product.name%>',
link: ' http://localhost:3000',
picture: 'http://www.hyperarts.com/external-xfbml/share-image.gif',

description: 'This is the content of the "description" field, below the caption.',
message: ''
});
});
});
</script>
<% end %>
</table>

<br />

<%= link_to 'New Product', new_product_path %>

<style type=”text/css”> img#share_button {cursor: pointer;} </style>


Monday, 24 July 2017

Upwork meter Issue:

Solution:
wget http://security.ubuntu.com/ubuntu/pool/main/n/nss/libnss3_3.19.2-1ubuntu1_i386.debsudo dpkg -i libnss3_3.19.2-1ubuntu1_i386.deb
Redirect http:// to https://

1. enable rewrite.load  sudo a2enmod rewrite
for
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
2. Make sure database.yml. settings.yml, and other yml files should contain production mode for open https:// in first time
IMport with Redis Server:

namespace :import_db_to_redis do
  desc "Import API's data to REDIS"
  task import: :environment do

    @api_url = []

    for n in 0..1075
      @api_url << "https://data.gov.in/api/datastore/resource.json?resource_id=cb579e8f-e4e0-48eb-a058-1fc812a38ff2&api-key=ae0612afc91e5595f4806c0a93ed7e0f&offset=#{n}"
    end

    for n in 0..294
      @api_url << "https://data.gov.in/api/datastore/resource.json?resource_id=03a4f696-d003-4a06-ae85-0116b1c94d2e&api-key=ae0612afc91e5595f4806c0a93ed7e0f&offset=#{n}"
    end

    for n in 0..167
      @api_url << "https://data.gov.in/api/datastore/resource.json?resource_id=134fa677-4fa1-427b-9608-a0c226339ae2&api-key=ae0612afc91e5595f4806c0a93ed7e0f&offset=#{n}"
    end

    for n in 0..16
      @api_url << "https://data.gov.in/api/datastore/resource.json?resource_id=4fc1510b-883a-419a-9cf2-efc3221bdada&api-key=ae0612afc91e5595f4806c0a93ed7e0f&offset=#{n}"
    end

    for n in 0..444
      @api_url << "https://data.gov.in/api/datastore/resource.json?resource_id=189cab67-c7fa-4e58-995c-fb467434169d&api-key=ae0612afc91e5595f4806c0a93ed7e0f&offset=#{n}"
    end
  
    @api_url << "https://data.gov.in/api/datastore/resource.json?resource_id=0ee2ec3f-c1a2-4008-87de-ef5ece8adb9d&api-key=ae0612afc91e5595f4806c0a93ed7e0f"
  
    puts @api_url.uniq.count

    @api_url.each do |url|
        data=Nokogiri::HTML(open(url))
        @records = JSON.parse(data)["records"]  
        @dists = {}      
        @sub_dists = {}      
        @cities = {}      
      set_json_data    
        import_data
      end

      puts "Done!"
  end
  def set_json_data

    @records.group_by { |d| d["STATENAME"]}.each do |st_key, st_value|    
      puts "Collecting #{st_key} state records..."
      st_value.group_by { |d| d["DISTRICTNAME"]}.each do |key, value|
        puts "Collecting #{key} district records..."
        value.group_by { |d| d["SUBDISTRICTNAME"]}.each do |key1, value1|
          puts "Collecting #{key1} sub-district records..."
          @cities_array = []
          value1.each do |k,v|
            @cities_array << k["AreaName"]
          end
          @cities[key1] = @cities_array.uniq
        end

        @sub_dists_array = []
        value.each do |k,v|      
          @sub_dists_array << k["SUBDISTRICTNAME"]
        end
        @sub_dists[key] = @sub_dists_array.uniq      
      end
      @dists_array = []
      st_value.each do |st_k,st_v|
        @dists_array << st_k["DISTRICTNAME"]
      end
      @dists[st_key] = @dists_array.uniq    
    end
  end

  def import_data
    redis = REDIS.hgetall('State')  
    st = redis["Name"].present? ? JSON.parse(redis["Name"]) : []
    state = st.push(@records.first["STATENAME"]).uniq.sort
    dis = redis["Dist"].present? ? JSON.parse(redis["Dist"]) : {}
    dist = dis.merge(@dists)
    sub_dis = redis["SubDist"].present? ? JSON.parse(redis["SubDist"]) : {}
    sub_dists = sub_dis.merge(@sub_dists)
    ct = redis["City"].present? ? JSON.parse(redis["City"]) : {}
    cities = ct.merge(@cities)
  
    puts "Importing states records..."
    REDIS.mapped_hmset 'State', "Name" =>    state.to_json
    puts "Importing districts records..."
    REDIS.mapped_hmset 'State', "Dist" =>    dist.to_json
    puts "Importing sub-district records..."
    REDIS.mapped_hmset 'State', "SubDist" => sub_dists.to_json
    puts "Importing cities records..."
    REDIS.mapped_hmset 'State', "City" =>    cities.to_json
  end

end
Code Deployment with Github/Bitbucket

Circleci - https://circleci.com/signup/
Postgres  setting error.

PG::ConnectionBad: fe_sendauth: no password supplied

/etc/postgresql/9.5/main/pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust


If you want to keep your code maintainable, secure and optimized, take a look at some gems that have been doing the job for me:

1. TRACEROUTE

gem 'rack-mini-profiler'

2. RACK-MINI-PROFILER

gem 'memory_profiler'
# For call-stack profiling flamegraphs (requires Ruby MRI 2.0.0+)
gem 'flamegraph'
gem 'stackprof'     # For Ruby MRI 2.1+
gem 'fast_stack'    # For Ruby MRI 2.0

3. BULLET

gem 'bullet', group: 'development'

4. BRAKEMAN

gem 'brakeman', :require => false

5. DEADWEIGHT

gem 'colored'
gem 'deadweight', :require => 'deadweight/hijack/rails'

6. RAILS BEST PRACTICES

rails_best_practices .

7. RUBYCRITIC

gem "rubycritic", :require => false


def full_name
    puts "Johnnie Walker"
  end

  alias_method :name, :full_name

How to Install the Latest Versions of NodeJS and NPM for Ubuntu 14.04 LTS

down voteaccepted

Fresh installation Node JS

Use the NodeSource PPA. For details look at the installation instructions. First, choose the Node.js version you need and add the sources for it:
# for Node.js v4
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
# OR for Node.js v5
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -
# OR for Node.js v6
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
# OR for Node.js v7
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
Then install the Node.js package.
sudo apt-get install -y nodejs
P.S.: curl package must be installed on server for these code lines.

Upgrading

If you have nodejs already installed and want to update, then first remove current instalation and install it again using scripts above.
sudo apt-get purge nodejs npm