Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

rails check if a URL is valid

require 'uri'

url =~ URI::regexp
# => 0 if it match, nil if not
Comment

check validate url ruby

require "net/http"

def url_exist? url_string
  Timeout.timeout 3 do	# set timeout 3s because some invalid url take long time to respond
    url = URI.parse url_string
    new_url = url.scheme || "http://#{url_string}"
    url = URI.parse new_url if url.scheme.nil?
    req = Net::HTTP.new url.host, url.port
    req.use_ssl = url.scheme == 'https'
    path = url.path if url.path.present?
    res = req.request_head(path || '/')
    res.code != "404"
  end
rescue 
  false
end
Comment

PREVIOUS NEXT
Code Example
Ruby :: ruby join hash to string 
Ruby :: string ruby 
Ruby :: ruby shorthand if 
Ruby :: ruby frozen_string_literal 
Ruby :: rspec create list 
Ruby :: ruby rails migrate check status 
Ruby :: stripe test keys 
Ruby :: ruby function arguments 
Ruby :: ! in ruby 
Ruby :: how to add variable inside string ruby 
Ruby :: rails humanize date 
Ruby :: activerecord exclude 
Ruby :: ruby new class params 
Ruby :: sequel ruby different table name 
Ruby :: generate float array in ruby 
Ruby :: rails date field default today 
Ruby :: update_all 
Ruby :: is codegrepper rafe approved? 
Ruby :: Many to Many Active Record 
Ruby :: ruby substring 
R :: r find elements in common between vectors 
R :: How to Export a DataFrame to Excel File in R 
R :: r column to rownames 
R :: create dataframe or table in r 
R :: how to write dictionary in R 
R :: r: rename a column 
R :: r change column based on condition 
R :: how many pairwise combinations 
R :: make the first row as header in r 
R :: how to load html file to r studio 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =