Module: VTools::SharedMethods::Common
Overview
both static & instance bindings
Constant Summary collapse
- @@logger =
nil
Instance Method Summary collapse
-
#config(data) ⇒ Object
config accessor.
-
#fix_encoding(output) ⇒ Object
encoding fixer for iso-8859-1.
-
#generate_path(file_name, scope = "video") ⇒ Object
function to create correct subdirectories to the file.
-
#hash_to_obj(hash) ⇒ Object
convert hash into object.
-
#json_to_obj(json_str) ⇒ Object
converts json to the ruby object returns nil on invalid JSON.
-
#keys_to_sym(hash) ⇒ Object
set symbols in place of string keys.
-
#log(level, message = "") ⇒ Object
logger mechanics.
-
#logger=(logger) ⇒ Object
custom logger.
-
#network_call(url) ⇒ Object
calls TCP/IP applications.
-
#parse_json(str) ⇒ Object
parse json string into hash.
-
#path_generator(scope = nil, &block) ⇒ Object
path generator setter.
Instance Method Details
#config(data) ⇒ Object
config accessor
54 55 56 |
# File 'lib/vtools/shared_methods.rb', line 54 def config data CONFIG[data] end |
#fix_encoding(output) ⇒ Object
encoding fixer for iso-8859-1
114 115 116 117 118 |
# File 'lib/vtools/shared_methods.rb', line 114 def fix_encoding(output) output[/test/] # Running a regexp on the string throws error if it's not UTF-8 rescue ArgumentError output.force_encoding("ISO-8859-1") end |
#generate_path(file_name, scope = "video") ⇒ Object
function to create correct subdirectories to the file
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vtools/shared_methods.rb', line 79 def generate_path file_name, scope = "video" generator = CONFIG[:"#{scope}_path_generator"] begin generator = instance_exec(file_name, &generator).to_s if generator.is_a? Proc rescue => e generator = nil raise ConfigError, "Path generator error: (#{e})" end storage = CONFIG[:"#{scope}_storage"].to_s storage += "/" unless storage.empty? storage += generator || "" path = (!storage || storage.empty? ? CONFIG[:PWD] : storage).to_s.strip.gsub(%r#/+#, '/').gsub(%r#/$#, '') # generate path begin FileUtils.mkdir_p path, :mode => 0755 rescue => e raise FileError, "Path generator error: #{e}" end unless File.exists?(path) path end |
#hash_to_obj(hash) ⇒ Object
convert hash into object
38 39 40 |
# File 'lib/vtools/shared_methods.rb', line 38 def hash_to_obj hash OpenStruct.new(hash) rescue raise ConfigError, "Can't convert setup to object" end |
#json_to_obj(json_str) ⇒ Object
converts json to the ruby object returns nil on invalid JSON
33 34 35 |
# File 'lib/vtools/shared_methods.rb', line 33 def json_to_obj json_str hash_to_obj(parse_json(json_str)) end |
#keys_to_sym(hash) ⇒ Object
set symbols in place of string keys
48 49 50 51 |
# File 'lib/vtools/shared_methods.rb', line 48 def keys_to_sym hash return hash unless hash.is_a? Hash hash.inject({}){ |opts,(k,v)| opts[k.to_sym] = v; opts } end |
#log(level, message = "") ⇒ Object
logger mechanics
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/vtools/shared_methods.rb', line 17 def log level, = "" if CONFIG[:logging] unless @@logger output = CONFIG[:log_file] || STDOUT logger = Logger.new(output, 1000, 1024000) logger.level = Logger::INFO @@logger = logger end @@logger.send(level, ) if @@logger end end |
#logger=(logger) ⇒ Object
custom logger
12 13 14 |
# File 'lib/vtools/shared_methods.rb', line 12 def logger= logger @@logger = logger end |
#network_call(url) ⇒ Object
calls TCP/IP applications
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/vtools/shared_methods.rb', line 59 def network_call url require "socket" url =~ %r#^([a-z]+://)?(?:www.)?([^/:]+)(:[\d]+)?(.*)$# protocol, host, port, route = ($1 || '')[0...-3], $2, ($3 || ":80")[1..-1].to_i, "/#{$4.to_s.gsub(/^\//, '')}" begin sock = TCPSocket.open(host, port) sock.print "GET #{route} HTTP/1.0\r\n\r\n" response = sock.read.split("\r\n\r\n", 2).reverse[0] sock.close rescue => e log :error, e end response end |
#parse_json(str) ⇒ Object
parse json string into hash
43 44 45 |
# File 'lib/vtools/shared_methods.rb', line 43 def parse_json str JSON.parse str rescue raise ConfigError, "Invalid JSON" end |
#path_generator(scope = nil, &block) ⇒ Object
path generator setter
104 105 106 107 108 109 110 111 |
# File 'lib/vtools/shared_methods.rb', line 104 def path_generator scope = nil, &block if scope scope = "thumb" unless scope == "video" CONFIG[:"#{scope}_path_generator"] = block else CONFIG[:thumb_path_generator] = CONFIG[:video_path_generator] = block end if block_given? end |