crapidocs 0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b995140375b7d77b964c283f94f807214ff86e39
4
- data.tar.gz: ff4721f9536bb7060a2b995ce38a408af25faaa3
3
+ metadata.gz: bd4e5f30914afdc7a2ee4d2faf1d3697de2397ef
4
+ data.tar.gz: fa43f26801d4135f60e2ebe2fd94f065ab665a80
5
5
  SHA512:
6
- metadata.gz: 86e217248f99650808772dda6d324313d50f1e03f47919a25480f88c668b5e68b4c6f132354a0c362505bf060522c7608956025e935164900a831116e0ce2f72
7
- data.tar.gz: 3835c184c6e0e75dbd35eb828e924b8307d9c04ae60ace13e485aa94c9964d9b067f54eda76a0ee37afca7a15b20d1857e5545a14f128e23de4d10afe52e0864
6
+ metadata.gz: bc1d214ebecba8346c73917a77d665be77200f8f8b8fdbe94a8a7c226fb33519e93e7d32c9772eaa55dfd189bd983980c12461e8993c0eac7040ea0a5964b1de
7
+ data.tar.gz: 5beee50d7511be80b705ad0434daeadf376eb30a56f84f727c9b343cf9e318061d5473e4079b042f19daba58d080cff6a903d7d847a5ed384d316c46306f6daa
@@ -0,0 +1,23 @@
1
+ require 'erubis'
2
+
3
+ module CrapiDocs
4
+ class Formatter
5
+ def initialize(session)
6
+ @session = session
7
+ end
8
+
9
+ def to_md
10
+ Erubis::Eruby.new(load_template).result(binding)
11
+ end
12
+
13
+ private
14
+
15
+ def load_template
16
+ File.open("#{TEMPLATE_DIR}/layout.md.erb").read
17
+ end
18
+
19
+ def anchor(s)
20
+ s.gsub(/[^a-zA-Z0-9]/, '')
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,92 @@
1
+ module CrapiDocs
2
+ class Session
3
+ def initialize(pattern)
4
+ @pattern = pattern
5
+ @actions = {}
6
+ end
7
+
8
+ def track(args, result)
9
+ uri, env = args
10
+ status, headers, body = result
11
+ body = body.respond_to?(:body) ? body.body : ''
12
+ method = env['REQUEST_METHOD']
13
+
14
+ return unless relevant?(uri)
15
+
16
+ action = {
17
+ request: {
18
+ method: method,
19
+ body: env['rack.input'].string,
20
+ headers: env,
21
+ uri: uri
22
+ },
23
+ response: {
24
+ status: status,
25
+ headers: headers,
26
+ body: body
27
+ }
28
+ }
29
+
30
+ path = cleaned_path(uri)
31
+ @actions[path] ||= {}
32
+ @actions[path][method] ||= []
33
+ @actions[path][method] << action
34
+ end
35
+
36
+ def paths
37
+ @actions.keys.sort
38
+ end
39
+
40
+ def methods(path)
41
+ @actions[path].keys.sort
42
+ end
43
+
44
+ def params(path, method)
45
+ reqs = @actions[path][method].map { |a| a[:request] }
46
+ params = merge_params(reqs)
47
+ return nil unless params.keys.any?
48
+ params
49
+ end
50
+
51
+ def body(path, method)
52
+ res = @actions[path][method]
53
+ .map { |a| a[:response] }
54
+ .find { |r| r[:status] / 100 == 2 }
55
+ return nil unless res
56
+ JSON.parse(res[:body])
57
+ end
58
+
59
+ private
60
+
61
+ def merge_params(reqs)
62
+ params = reqs.reduce({}) do |hash, r|
63
+ ps = if r[:headers]['Content-Type'] =~ /json/
64
+ JSON.parse(r[:body]) rescue {}
65
+ else
66
+ Rack::Utils.parse_nested_query(r[:body])
67
+ end
68
+ hash.merge(ps)
69
+ end
70
+ params.delete('format')
71
+ params
72
+ end
73
+
74
+ def cleaned_path(uri)
75
+ last = nil
76
+ uri.path.split('/').reject(&:blank?).reduce('') do |cleaned, part|
77
+ part = ":#{last.singularize}_id" if part =~ /^\d+$/
78
+ part = ':token' if tokenish?(part)
79
+ last = part
80
+ "#{cleaned}/#{part}"
81
+ end
82
+ end
83
+
84
+ def tokenish?(s)
85
+ s =~ /\d+/ && s =~ /[a-zA-Z]+/ && s.length >= 10
86
+ end
87
+
88
+ def relevant?(uri)
89
+ (uri.to_s =~ @pattern).present?
90
+ end
91
+ end
92
+ end
data/lib/crapidocs.rb CHANGED
@@ -16,7 +16,7 @@ module Rack
16
16
  end
17
17
 
18
18
  module CrapiDocs
19
- VERSION = [0, 1]
19
+ VERSION = [0, 1, 1]
20
20
  TEMPLATE_DIR = File.expand_path('../..', __FILE__) + '/templates'
21
21
 
22
22
  class << self
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crapidocs
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Holly
@@ -17,6 +17,8 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/crapidocs.rb
20
+ - lib/crapidocs/formatter.rb
21
+ - lib/crapidocs/session.rb
20
22
  homepage: https://rubygems.org/gems/crapidocs
21
23
  licenses:
22
24
  - MIT