Class: Tomograph::ApiBlueprint::Drafter4::Yaml::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/tomograph/api_blueprint/drafter_4/yaml/action.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, path, resource) ⇒ Action

Returns a new instance of Action.



8
9
10
11
12
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 8

def initialize(content, path, resource)
  @content = content
  @path = path
  @resource = resource
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 14

def path
  @path
end

#resourceObject (readonly)

Returns the value of attribute resource.



14
15
16
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 14

def resource
  @resource
end

Instance Method Details

#content_typeObject



20
21
22
23
24
25
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 20

def content_type
  if @content.first['attributes'].key?('headers') &&
     @content.first['attributes']['headers']['content'][0]['content']['key']['content'] == 'Content-Type'
    @content.first['attributes']['headers']['content'][0]['content']['value']['content']
  end
end

#json_schema(actions) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 34

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

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

#methodObject



16
17
18
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 16

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

#requestObject



27
28
29
30
31
32
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 27

def request
  return @request if @request

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

#responsesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tomograph/api_blueprint/drafter_4/yaml/action.rb', line 47

def responses
  return @responses if @responses

  @responses = @content.select do |response|
    response['element'] == 'httpResponse' && response['attributes']
  end
  @responses = @responses.map do |response|
    content_type = if response['attributes'].key?('headers')
                     response['attributes']['headers']['content'][0]['content']['value']['content']
                   end

    {
      'status' => response['attributes']['statusCode']['content'].to_s,
      'body' => json_schema(response['content']),
      'content-type' => content_type
    }
  end
end