Sha256: d0512b026f6817f8497d6a67cf5a1d6672f1dc9ad5d6c6217e8410b7b8acb1a6

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'tomograph/tomogram/action'

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

          attr_reader :path, :resource

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

          def content_type
            @content_type ||= @content.first['attributes'].has_key?('headers') ?
              @content.first['attributes']['headers']['content'][0]['content']['value']['content'] : nil
          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']),
                'content-type' => response['attributes'].has_key?('headers') ?
                  response['attributes']['headers']['content'][0]['content']['value']['content'] : nil
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tomograph-2.5.0 lib/tomograph/api_blueprint/drafter_3/yaml/action.rb