Module: LazyApiDoc

Defined in:
lib/lazy_api_doc.rb,
lib/lazy_api_doc/version.rb,
lib/lazy_api_doc/generator.rb,
lib/lazy_api_doc/route_parser.rb,
lib/lazy_api_doc/variants_parser.rb,
lib/generators/lazy_api_doc/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Error, Generator, RouteParser, VariantsParser

Constant Summary collapse

VERSION =
"0.2.5".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.example_file_ttlObject

Returns the value of attribute example_file_ttl.



11
12
13
# File 'lib/lazy_api_doc.rb', line 11

def example_file_ttl
  @example_file_ttl
end

.pathObject

Returns the value of attribute path.



11
12
13
# File 'lib/lazy_api_doc.rb', line 11

def path
  @path
end

Class Method Details

.add(lazy_example) ⇒ Object



29
30
31
# File 'lib/lazy_api_doc.rb', line 29

def add(lazy_example)
  generator.add(lazy_example)
end

.add_spec(rspec_example) ⇒ Object

rubocop:disable Metrics/AbcSize



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lazy_api_doc.rb', line 33

def add_spec(rspec_example) # rubocop:disable Metrics/AbcSize
  add(
    'controller' => rspec_example.instance_variable_get(:@request).params[:controller],
    'action' => rspec_example.instance_variable_get(:@request).params[:action],
    'description' => rspec_example.class.description,
    'source_location' => [rspec_example.class.[:file_path], rspec_example.class.[:line_number]],
    'verb' => rspec_example.instance_variable_get(:@request).method,
    'params' => rspec_example.instance_variable_get(:@request).params,
    'content_type' => rspec_example.instance_variable_get(:@request).content_type.to_s,
    'request' => {
      'query_params' => rspec_example.instance_variable_get(:@request).query_parameters,
      'full_path' => rspec_example.instance_variable_get(:@request).fullpath
    },
    'response' => {
      'code' => rspec_example.response.status,
      'content_type' => rspec_example.response.content_type.to_s,
      'body' => rspec_example.response.body
    }
  )
end

.add_test(mini_test_example) ⇒ Object

rubocop:disable Metrics/AbcSize



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lazy_api_doc.rb', line 54

def add_test(mini_test_example) # rubocop:disable Metrics/AbcSize
  add(
    'controller' => mini_test_example.instance_variable_get(:@request).params[:controller],
    'action' => mini_test_example.instance_variable_get(:@request).params[:action],
    'description' => mini_test_example.name.gsub(/\Atest_/, '').humanize,
    'source_location' => mini_test_example.method(mini_test_example.name).source_location,
    'verb' => mini_test_example.instance_variable_get(:@request).method,
    'params' => mini_test_example.instance_variable_get(:@request).params,
    'content_type' => mini_test_example.instance_variable_get(:@request).content_type.to_s,
    'request' => {
      'query_params' => mini_test_example.instance_variable_get(:@request).query_parameters,
      'full_path' => mini_test_example.instance_variable_get(:@request).fullpath
    },
    'response' => {
      'code' => mini_test_example.response.status,
      'content_type' => mini_test_example.response.content_type.to_s,
      'body' => mini_test_example.response.body
    }
  )
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (LazyApiDoc)

    the object that the method was called on



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

def configure
  yield self
end

.generate_documentationObject



75
76
77
78
79
80
# File 'lib/lazy_api_doc.rb', line 75

def generate_documentation
  layout = YAML.safe_load(File.read("#{path}/layout.yml"))
  layout["paths"] ||= {}
  layout["paths"].merge!(generator.result)
  File.write("#{path}/api.yml", layout.to_yaml)
end

.generatorObject



25
26
27
# File 'lib/lazy_api_doc.rb', line 25

def generator
  @generator ||= Generator.new
end

.load_examplesObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/lazy_api_doc.rb', line 93

def load_examples
  valid_time = Time.now.to_i - example_file_ttl
  examples = Dir["#{path}/examples/*.json"].flat_map do |file|
    meta = JSON.parse(File.read(file))
    next [] if meta['created_at'] < valid_time # do not handle outdated files

    meta['examples']
  end
  generator.clear
  examples.each { |example| add(example) }
end

.reset!Object



17
18
19
20
21
22
23
# File 'lib/lazy_api_doc.rb', line 17

def reset!
  config_file = './config/lazy_api_doc.yml'
  config = File.exist?(config_file) ? YAML.safe_load(ERB.new(File.read(config_file)).result) : {}

  self.path = ENV['LAZY_API_DOC_PATH'] || config['path'] || 'public/lazy_api_doc'
  self.example_file_ttl = ENV['LAZY_API_DOC_EXAMPLE_FILE_TTL'] || config['example_file_ttl'] || 1800 # 30 minutes
end

.save_examples(process_name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/lazy_api_doc.rb', line 82

def save_examples(process_name)
  FileUtils.mkdir_p("#{path}/examples")
  File.write(
    "#{path}/examples/#{process_name}_#{ENV['TEST_ENV_NUMBER'] || SecureRandom.uuid}.json",
    {
      created_at: Time.now.to_i,
      examples: generator.examples
    }.to_json
  )
end