Module: VTools::SharedMethods::Common

Included in:
Instance, Static
Defined in:
lib/vtools/shared_methods.rb

Overview

both static & instance bindings

Constant Summary collapse

@@logger =
nil

Instance Method Summary collapse

Instance Method Details

#config(data) ⇒ Object

config accessor



55
56
57
# File 'lib/vtools/shared_methods.rb', line 55

def config data
  CONFIG[data]
end

#fix_encoding(output) ⇒ Object

encoding fixer for iso-8859-1



107
108
109
110
111
# File 'lib/vtools/shared_methods.rb', line 107

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vtools/shared_methods.rb', line 80

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 || ""

  (!storage || storage.empty? ? CONFIG[:PWD] : storage).to_s.strip.gsub(%r#/+#, '/').gsub(%r#/$#, '')
end

#hash_to_obj(hash) ⇒ Object

convert hash into object



39
40
41
# File 'lib/vtools/shared_methods.rb', line 39

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



34
35
36
# File 'lib/vtools/shared_methods.rb', line 34

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



49
50
51
52
# File 'lib/vtools/shared_methods.rb', line 49

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



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vtools/shared_methods.rb', line 18

def log level, message = ""

  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, message) if @@logger
  end
end

#logger=(logger) ⇒ Object

custom logger



13
14
15
# File 'lib/vtools/shared_methods.rb', line 13

def logger= logger
  @@logger = logger
end

#network_call(url) ⇒ Object

calls TCP/IP applications



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vtools/shared_methods.rb', line 60

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



44
45
46
# File 'lib/vtools/shared_methods.rb', line 44

def parse_json str
  JSON.parse str rescue raise ConfigError, "Invalid JSON"
end

#path_generator(scope = nil, &block) ⇒ Object

path generator setter



97
98
99
100
101
102
103
104
# File 'lib/vtools/shared_methods.rb', line 97

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