Fetch Images from Web Url

Wednesday, March 21, 2012 · Posted in ,

You can find images from given web-url by using following code:

For this first you need to write below statements in your .rb file.

require "open-uri"
require "nokogiri"
require "image_size"


now use below code:

  def fetch_images(url)
    return [] if url.nil?
    url_content = open(url)
    url_data = Nokogiri::HTML(url_content)
    images = []
    url_data.xpath("/html/body//img").each do |img|
       src = img["src"]
       full_img_src = src.match(/^(http|https):\/\//) ? src : File.join(url,src)
       if valid_image(full_img_src)
        images << full_img_src
       end
    end
    return images
  rescue
     return 0
  end

  def valid_image(img_url)
    return false if img_url.nil?
    valid_img = false
    img_data = open(img_url, "rb").read
    min_size  = [100,100]
    img_size = ImageSize.new(img_data).size
    if img_size[0] >= min_size[0] and img_size[1] >= min_size[1]
      valid_img = true
    end
    return valid_img
  rescue
    return false
  end

Here I have restricted images to fetch only (100x100) sized images.

multiple database connections in rails

Friday, July 29, 2011

One of my colleague has done multiple db connection in rails


Here is his work,


First set connection parameters  for both database with different constant variables and define in environment file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
DB1 = {
 
:adapter => 'mysql',
 
:database => DATABASE1,
 
:username => USERNAME,
 
:password => PASSWORD,
 
:host => HOST
 
}
 
DB2 = {
 
:adapter => 'mysql',
 
:database => DATABASE2,
 
:username => USERNAME,
 
:password => PASSWORD,
 
:host => HOST
 
}
Add connection.rb file in lib folder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module Connection
 
  def self.included(base)
 
    base.class_eval do
 
    parameters = self::DB
 
    ActiveRecord::Base.establish_connection(
 
      :adapter  => parameters[:adapter],
 
      :host     => parameters[:host],
 
      :username => parameters[:username],
 
      :password => parameters[:password],
 
      :database => parameters[:database]
 
      )
 
   end
 end
end
include connection file and set DATABASE in models
in Company model
1
2
DB =  DB1
include Connection
in Website model
1
2
3
DB = DB2
 
include Connection


From, http://blog.brijeshshah.com/multiple-database-connections-in-rails/


it's really helpful.

Powered by Blogger.