Class: Prop2redis::RedisLoader

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_properties(properties_filename) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/prop2redis.rb', line 9

def self.load_properties(properties_filename)
  properties = {}
  File.open(properties_filename, 'r') do |properties_file|
    properties_file.read.each_line do |line|
      line.strip!
      if (line[0] != ?# and line[0] != ?=)
        i = line.index('=')
        if (i)
          properties[line[0..i - 1].strip] = line[i + 1..-1].strip
        else
          properties[line] = ''
        end
      end
    end
  end
  properties
end

.mainObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/prop2redis.rb', line 27

def self.main
  abort "Usage: prop2redis <propertyfile>" if ARGV.length != 1

  property_file = ARGV[0]
  abort "Property file must exist." if !File.exist?(property_file)
  abort "Property file must be a file." if !File.file?(property_file)

  redis_url = ENV['REDIS_URL']
  abort "REDIS_URL environment variable must be set" if redis_url.nil?

  URI uri = URI(redis_url)
  abort "REDIS_URL must have scheme 'redis'." if uri.scheme != 'redis'
  abort "REDIS_URL must have a namespace query parameter." if ( uri.query.nil? )

  query = uri.query
  query_hash = CGI::parse(query)
  abort "REDIS_URL must have a namespace query parameter." if (query_hash['namespace'].nil?)
  namespace=query_hash['namespace']

  abort "REDIS_URL must have a path." if uri.path.nil?

  hash = load_properties(property_file)
  db = uri.path
  host = uri.host
  port = uri.port.nil? ? 6379 : uri.port
  db = uri.path
  redis = Redis.new(:host=>host, :port=>port, :db=>db)
  redis.multi do
    hash.each do |key, value|
      redis.hset namespace, to_env_format(key), value
    end
  end
end

Instance Method Details

#to_env_format(key) ⇒ Object

Convert to ENV format. ie, “my.property” –> “MY_PROPERTY”



62
63
64
# File 'lib/prop2redis.rb', line 62

def to_env_format(key)
  key.upcase.gsub(/[\.|\-]/, "_")
end