Module: Updoc::Report

Included in:
RackReport, RailsReport
Defined in:
lib/updoc/report.rb,
lib/updoc/report/rack_report.rb,
lib/updoc/report/rails_report.rb

Defined Under Namespace

Classes: GrapeApp, RackReport, RailsReport

Constant Summary collapse

SORT =
[:name, :definition_uri, :consumes, :produces, :updoc_version]

Instance Method Summary collapse

Instance Method Details

#base_reportObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/updoc/report/rails_report.rb', line 41

def base_report
  {
    name: Updoc.application_name,
    updoc_version: Updoc::VERSION
  }.tap do |report|
    if Updoc.git?
      git_uri = `git config --get remote.origin.url`.strip
      updoc_file = `git ls-files -z updoc.yml`.split("\x0").first
      report[:definition_uri] = "git:#{git_uri}/#{updoc_file}"
    end
  end
end

#map_grape_endpoints(grape_apps) ⇒ Object



9
10
11
12
13
14
15
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
41
42
43
44
# File 'lib/updoc/report.rb', line 9

def map_grape_endpoints(grape_apps)
  updoc_supported_routes = grape_apps.flat_map do |grape_app|
    app_feature = grape_app.app.updoc.feature_name if grape_app.app < Updoc::Config

    grape_app.app.endpoints.flat_map do |endpoint|
      endpoint.routes.map do |r|
        detail = {
          description: r.route_description,
          method: r.route_method.downcase,
          path: r.route_path,
          feature: app_feature
        }

        # XXX: Having to manually adding the path prefix may have changed
        # from older Grape versions
        #detail[:path] = File.join(*[grape_app.path_prefix.to_s, detail[:path]].compact) if grape_app.path_prefix

        if endpoint.options[:app]
          detail[:feature] = endpoint.options[:app].updoc.feature_name if endpoint.options[:app] < Updoc::Producer
          detail[:content_types] = endpoint.options[:app].content_types
        end

        detail if detail[:feature]
      end
    end
  end.compact.sort_by { |r| [r[:feature], r[:method], r[:path]] }

  updoc_supported_routes.group_by { |r| r[:feature] }.each_with_object({}) do |(feature, routes), memo|
    memo[feature] = {
      service_type: 'REST',
      services: routes.each_with_object({}) do |route, memo|
        memo["rest:#{route[:method]}:#{route[:path]}"] = route.slice(:description, :content_types)
      end
    }
  end
end

#map_updoc_consumersObject



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

def map_updoc_consumers
  dependents = ObjectSpace.each_object(::Class).select { |x| x < Updoc::DependsOn }
  dependents.each_with_object({}) do |dependent, memo|
    consumers = {}

    depends_on = dependent.updoc.config.depends_on
    depends_on.consumers.each do |name, services|
      raise "Referenced consumer #{name} has not be defined" unless Updoc::Consumers[name]

      consumers[name] = {
        service_type: Updoc::Consumers[name].service_type,
        definition_uri: Updoc::Consumers[name].definition_uri,
        services: services
      }
    end

    memo[dependent.updoc.feature_name] = consumers
  end
end

#write_updoc_report(file_path, report) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/updoc/report.rb', line 46

def write_updoc_report(file_path, report)
  report = (SORT | report.keys).each_with_object({}) do |key, hash|
    hash[key] = report[key]
  end

  report.delete_if { |_, v| v.nil? || v.empty? }

  IO.write(file_path, report.to_yaml)

  file_path
end