Class: PostMdize::Mdize

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
lib/post_mdize.rb

Class Method Summary collapse

Class Method Details

.body_mdize(item) ⇒ Object



51
52
53
# File 'lib/post_mdize.rb', line 51

def self.body_mdize(item)
  item['request']['body']['raw'] || ''
end

.hash_mdize(hash, file, intense = 2) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/post_mdize.rb', line 59

def self.hash_mdize(hash, file, intense = 2)
  hash = {} if hash == ""
  space = ' ' * intense
  if hash.is_a?(Array)
    file.write("[\r\n")
    hash.each do |element|
      hash_mdize(element, file, intense + 2)
    end
    file.write("#{' ' * (intense - 2)}]")
  else
    file.write("#{' ' * (intense - 2)}{\r\n")
    strings =
    hash.map do |k, v|
      if v.is_a?(Hash) || v.is_a?(Array)
        file.write("#{space}\"#{k}\": ")
        hash_mdize(v, file, intense + 2)
      else
        file.write("#{space}\"#{k}\": \"#{v}\"")
        file.write(",\r\n") unless v == hash.values.last
      end
    end
    file.write("\r\n#{' ' * (intense - 2)}}\r\n")
  end
end

.path_mdize(item) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/post_mdize.rb', line 42

def self.path_mdize(item)
  path_segs = item['request']['url']['path']
  path_segs.map!.with_index do |segment, index|
    segment.match?(/\-/) ? "{:#{self.new.singularize(path_segs[index-1])}_uuid}" : segment
  end

  "#{path_segs.join('/')}"
end

.perform(input_file, output_file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/post_mdize.rb', line 16

def self.perform(input_file, output_file)
  file = File.read(input_file)
  hash = JSON.deep_parse(file)

  outfile = File.open("./#{output_file}", "w") do |file|
    file.write("# README\r\n")
    hash['item'].each do |item|
      file.write("\r\n* __#{item['request']['method']}__ #{path_mdize(item)}\r\n")
      file.write("#### #{item['name']}\r\n")
      file.write("params\r\n")
      file.write("\r\n```\r\n")
      file.write("header:\r\n")
      hash_mdize(item['request']['header'], file)
      file.write("\r\nbody:\r\n")
      hash_mdize(body_mdize(item), file)
      file.write("\r\n```\r\n")
      file.write("RESPONSE:\r\n")
      file.write("\r\n```\r\n")
      hash_mdize(response_mdize(item), file)
      file.write("\r\n```\r\n")
    end
    puts "Generated:", file.path
  end

end

.response_mdize(item) ⇒ Object



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

def self.response_mdize(item)
  item['response'].first['body'] #unless item['response']['body'].empty?) || ''
end