Class: Lhj::Command::Service

Inherits:
Lhj::Command show all
Defined in:
lib/lhj/command/local/micro_service.rb

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #begin_title, #run, #stop

Constructor Details

#initialize(argv) ⇒ Service

Returns a new instance of Service.



10
11
12
13
14
15
16
# File 'lib/lhj/command/local/micro_service.rb', line 10

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @file_type = argv.option('file-type', 'm,h')
  @file_name = argv.option('file-name', 'service_map.csv')
  @service_map = {}
  super
end

Instance Method Details

#csv_file_nameObject



32
33
34
35
36
# File 'lib/lhj/command/local/micro_service.rb', line 32

def csv_file_name
  file_name = @file_name
  file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
  file_name
end

#file_string(file) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/lhj/command/local/micro_service.rb', line 57

def file_string(file)
  str = ''
  File.open(file, 'r+') do |f|
    f.each_line do |line|
      str += format_string(f, line)
    end
  end
  str
end

#format_string(file, line) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/lhj/command/local/micro_service.rb', line 67

def format_string(file, line)
  result = line
  if url_reg =~ line
    line.scan(url_reg).flatten.each do |key|
      result = result.gsub(key, @service_map[key]) if key && @service_map[key]
    end
  end
  result
end

#handleObject



18
19
20
21
# File 'lib/lhj/command/local/micro_service.rb', line 18

def handle
  read_csv
  update_source
end

#read_csvObject



23
24
25
26
27
28
29
30
# File 'lib/lhj/command/local/micro_service.rb', line 23

def read_csv
  path = File.join(@current_path, csv_file_name)
  Dir.glob(path).each do |p|
    CSV.foreach(p) do |row|
      @service_map[row[0]] = row[1] if row[0]
    end
  end
end

#update_file(file) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/lhj/command/local/micro_service.rb', line 48

def update_file(file)
  File.chmod(0o644, file)
  str = file_string(file)
  File.open(file, 'w+') do |f|
    f.write(str)
  end
  File.chmod(0o444, file) if file =~ /Pods/
end

#update_sourceObject



38
39
40
41
42
43
44
45
46
# File 'lib/lhj/command/local/micro_service.rb', line 38

def update_source
  Dir.glob("#{@current_path}/**/*.{m,h}").each do |f|
    if f =~ /Pods/
      update_file(f) if f =~ %r{Pods/ML}
    else
      update_file(f)
    end
  end
end

#url_regObject



77
78
79
80
81
82
83
# File 'lib/lhj/command/local/micro_service.rb', line 77

def url_reg
  @url_key_reg ||= begin
                     keys = @service_map.keys.join('|')
                     /(#{keys})/
                   end
  @url_key_reg
end