Class: StaticImageDownloader::Images

Inherits:
Object
  • Object
show all
Defined in:
lib/static_image_download/images.rb

Constant Summary collapse

MAX_FILE_NAME_LENGTH =
100
IMAGE_EXT =
["jpg", "jpeg", "png", "gif", "ico", "svg", "bmp"]
EMPTY_FILE_NAME =
'EMPTY_'
DOWNLOAD_OPTIONS =
{
	'CURB_EASY'			=> :curb_simple,
	'HTTP_GET'			=> :http_get
}
@@DEFAULTDONWLOADOPTION =
'CURB_EASY'
@@HTTPONSUCCESS =
Regexp.new(/^20\d$/)
@@DEFAULTPATH =
"./"
@@DEFAULTTIMEOUT =
120
@@SUCCESSFULLPICTURES =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, file_path_name = @@DEFAULTPATH, download_option = @@DEFAULTDONWLOADOPTION, page_host = "") ⇒ Images

Returns a new instance of Images.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/static_image_download/images.rb', line 24

def initialize(src, file_path_name=@@DEFAULTPATH, download_option=@@DEFAULTDONWLOADOPTION, page_host="")
	@src 				= src
	@page_host 			= page_host	# Reserved for future
	@download_option	= download_option.nil? ? @@DEFAULTDONWLOADOPTION : download_option
	@file_path_name 	= file_path_name.nil? ? @@DEFAULTPATH : file_path_name.gsub(/\/+$/,'')
	
	file_base_name 		= @src.sub(/.*\//,'')
	file_base_name 		= EMPTY_FILE_NAME + rand(1000).to_s if !file_base_name || file_base_name.empty?
	if file_base_name.size > MAX_FILE_NAME_LENGTH
		file_base_name = file_base_name[-MAX_FILE_NAME_LENGTH..file_base_name.size]
	end
	
	@file_base_name = file_base_name
	@file_full_name = File.expand_path(File.join(@file_path_name, @file_base_name))
	
	@full_path_name = File.expand_path(File.join(@file_path_name)) 
	Dir::mkdir(@full_path_name) unless FileTest.directory?(@full_path_name)
end

Instance Attribute Details

#absolute_srcObject

Returns the value of attribute absolute_src.



6
7
8
# File 'lib/static_image_download/images.rb', line 6

def absolute_src
  @absolute_src
end

#file_base_nameObject

Returns the value of attribute file_base_name.



6
7
8
# File 'lib/static_image_download/images.rb', line 6

def file_base_name
  @file_base_name
end

#file_path_nameObject

Returns the value of attribute file_path_name.



6
7
8
# File 'lib/static_image_download/images.rb', line 6

def file_path_name
  @file_path_name
end

#full_path_nameObject

Returns the value of attribute full_path_name.



6
7
8
# File 'lib/static_image_download/images.rb', line 6

def full_path_name
  @full_path_name
end

#page_hostObject

Returns the value of attribute page_host.



6
7
8
# File 'lib/static_image_download/images.rb', line 6

def page_host
  @page_host
end

#srcObject

Returns the value of attribute src.



6
7
8
# File 'lib/static_image_download/images.rb', line 6

def src
  @src
end

Class Method Details

.default_download_optionObject



44
45
46
# File 'lib/static_image_download/images.rb', line 44

def default_download_option
	@@DEFAULTDONWLOADOPTION
end

.default_pathObject



48
49
50
# File 'lib/static_image_download/images.rb', line 48

def default_path
	@@DEFAULTPATH
end

.default_timeoutObject



52
53
54
# File 'lib/static_image_download/images.rb', line 52

def default_timeout
	@@DEFAULTTIMEOUT
end

.get_successfull_pictures_numberObject



56
57
58
# File 'lib/static_image_download/images.rb', line 56

def get_successfull_pictures_number
	@@SUCCESSFULLPICTURES.to_s
end

Instance Method Details

#download(download_option = @@DEFAULTDONWLOADOPTION, timeout = @@DEFAULTTIMEOUT, h = {:dup_file_names => true}) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/static_image_download/images.rb', line 66

def download(download_option=@@DEFAULTDONWLOADOPTION, timeout=@@DEFAULTTIMEOUT, h={:dup_file_names => true})
	#p "download_option=#{download_option}"
	begin
		response = nil
		status = Timeout::timeout(timeout) {
			h[:start_time] = Time.now
			response = method_to_value(download_option, h)
		}
	rescue => error
		p "#{error}"
		nil
	end
end

#method_to_value(option, h = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/static_image_download/images.rb', line 84

def method_to_value(option, h={})
	#p "option= #{option}"
	method = option_to_method(option)
	p "method= #{method}" if $debug_option
	begin
		response = send(method, h) || ""
		@@SUCCESSFULLPICTURES += 1 if response[:path]
		return response
	rescue => error
		p "method_to_value.error = #{error}"
		nil
	end
end

#option_to_method(option) ⇒ Object



80
81
82
# File 'lib/static_image_download/images.rb', line 80

def option_to_method(option)
	opt = DOWNLOAD_OPTIONS[option]
end