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 :: rails scope syntax 
Ruby :: ruby for loop 
Ruby :: check type in ruby 
Ruby :: rails render head: :ok 
Ruby :: Seconds to HH:MM in Ruby 
Ruby :: ruby pry syntax 
Ruby :: rails cors allow all 
Ruby :: write csv with header ruby 
Ruby :: font awesome icon rails submit button 
Ruby :: ruby map hash 
Ruby :: ruby ternary operator 
Ruby :: format date rails created long 
Ruby :: ruby loop over files in a folder 
Ruby :: rails increment counter model 
Ruby :: rails add column next to 
Ruby :: ruby in array 
Ruby :: Backtracking solution in ruby 
Ruby :: text_field_tag transfer params rails 
Ruby :: ruby nil to float is 0.00 
Ruby :: how to write an array in ruby 
Ruby :: include? reverse ruby 
Ruby :: ruby find frequency in array self.all 
Ruby :: rails do something for 3 minutes 
Ruby :: simpleCov formatter set two formats 
R :: how to count the number of NA in r 
R :: how to read number of excel sheet in r 
R :: merge several data frames in r 
R :: select columns in r 
R :: remove rownumbers r 
R :: R remove commas 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =