Sha256: f8cfbbef451639a8dec448e54165290c86049679261361de852e7862d20d95bd

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

require 'tomograph/tomogram/action'

module Tomograph
  module ApiBlueprint
    class JsonSchema
      def initialize(prefix, json_schema_path)
        @prefix = prefix
        @documentation = JSON.parse(File.read(json_schema_path))
      end

      def to_tomogram
        @tomogram ||= @documentation.inject([]) do |result, action|
          result.push(Tomograph::Tomogram::Action.new(
                        path: "#{@prefix}#{action['path']}",
                        method:  action['method'],
                        content_type: action['content-type'],
                        requests: action['requests'],
                        responses: action['responses'],
                        resource: action['resource']))
        end
      end

      def to_resources
        return @to_resources if @to_resources

        @to_resources = @documentation.group_by { |action| action['resource'] }
        @to_resources = @to_resources.each_with_object({}) do |(resource, actions), resource_map|
          requests = actions.map do |action|
            "#{action['method']} #{@prefix}#{action['path']}"
          end
          resource_map[resource] = requests
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tomograph-3.1.2 lib/tomograph/api_blueprint/json_schema.rb
tomograph-3.1.1 lib/tomograph/api_blueprint/json_schema.rb
tomograph-3.1.0 lib/tomograph/api_blueprint/json_schema.rb
tomograph-3.0.1 lib/tomograph/api_blueprint/json_schema.rb