Sha256: 208a2ce1b5f3909df69a35279f715b54d6d1b88475714ba400c880eaa7bb31c8

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'tomograph/tomogram/action'

module Tomograph
  module ApiBlueprint
    class Yaml
      class Action
        def initialize(content, path)
          @content = content
          @path = path
        end

        attr_reader :path

        def method
          @method ||= @content.first['attributes']['method']
        end

        def request
          return @request if @request

          request_action = @content.find { |el| el['element'] == 'httpRequest' }
          @request = json_schema(request_action['content'])
        end

        def json_schema(actions)
          schema_node = actions.find do |action|
            action && action['element'] == 'asset' && action['attributes']['contentType'] == 'application/schema+json'
          end
          return {} unless schema_node

          MultiJson.load(schema_node['content'])
        rescue MultiJson::ParseError => e
          puts "[Tomograph] Error while parsing #{e}. skipping..."
          {}
        end

        def responses
          return @responses if @responses

          @responses = @content.select do |response|
            response['element'] == 'httpResponse' && response['attributes']
          end
          @responses = @responses.map do |response|
            {
              'status' => response['attributes']['statusCode'],
              'body' => json_schema(response['content'])
            }
          end
        end

        def to_tomogram
          Tomograph::Tomogram::Action.new(
            path: path, method: method, request: request, responses: responses
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tomograph-1.0.0 lib/tomograph/api_blueprint/yaml/action.rb