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

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

Defined Under Namespace

Classes: Action

Instance Method Summary collapse

Constructor Details

#initialize(prefix, drafter_yaml_path) ⇒ Yaml

Returns a new instance of Yaml.



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

def initialize(prefix, drafter_yaml_path)
  @prefix = prefix
  @documentation = YAML.safe_load(File.read(drafter_yaml_path))
end

Instance Method Details

#action?(content) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 92

def action?(content)
  content['element'] == 'httpTransaction'
end

#action_hash(related_actions) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 104

def action_hash(related_actions)
  {
    path: "#{@prefix}#{related_actions.first.path}",
    method: related_actions.first.method,
    content_type: related_actions.first.content_type,
    requests: related_actions.map(&:request).flatten.uniq,
    responses: related_actions.map(&:responses).flatten.uniq,
    resource: related_actions.first.resource
  }
end

#actionsObject



96
97
98
99
100
101
102
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 96

def actions
  @actions ||= without_group_actions
               .flatten
               .group_by { |action| "#{action.method} #{action.path}" }.map do |_key, related_actions|
    action_hash(related_actions)
  end.flatten
end

#group?(group) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 25

def group?(group)
  return false if group['element'] == 'resource'

  group['element'] != 'copy' && # Element is a human readable text
    group['meta']['classes']['content'][0]['content'] == 'resourceGroup' # skip Data Structures
end

#groupsObject



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

def groups
  @groups ||= @documentation['content'][0]['content'].each_with_object([]) do |group, result_groups|
    result_groups.push(group) if group?(group)
    result_groups.push('content' => [group]) if single_resource?(group)
  end
end

#resource?(resource) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 42

def resource?(resource)
  resource['element'] != 'copy' # Element is a human readable text
end

#resource_path(resource) ⇒ Object



46
47
48
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 46

def resource_path(resource)
  resource['attributes'] && resource['attributes']['href']['content']
end

#resourcesObject



32
33
34
35
36
37
38
39
40
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 32

def resources
  @resources ||= groups.inject([]) do |result_groups, group|
    result_groups.push(group['content'].each_with_object([]) do |resource, result_resources|
      if resource?(resource)
        result_resources.push('resource' => resource, 'resource_path' => resource_path(resource))
      end
    end)
  end.flatten
end

#single_resource?(group) ⇒ Boolean

Returns:

  • (Boolean)


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

def single_resource?(group)
  group['element'] == 'resource'
end

#to_resourcesObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 121

def to_resources
  return @to_resources if @to_resources

  @to_resources = actions.group_by { |action| action[:resource] }
  @to_resources = @to_resources.inject({}) do |res, related_actions|
    requests = related_actions[1].map do |action|
      "#{action[:method]} #{action[:path]}"
    end
    res.merge(related_actions[1].first[:resource] => requests)
  end
end

#to_tomogramObject



115
116
117
118
119
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 115

def to_tomogram
  @tomogram ||= actions.inject([]) do |result, action|
    result.push(Tomograph::Tomogram::Action.new(**action))
  end
end

#transition?(transition) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 61

def transition?(transition)
  transition['element'] == 'transition'
end

#transition_hash(transition, resource) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 65

def transition_hash(transition, resource)
  {
    'transition' => transition,
    'transition_path' => transition_path(transition, resource['resource_path']),
    'resource' => resource['resource_path']
  }
end

#transition_path(transition, resource_path) ⇒ Object



73
74
75
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 73

def transition_path(transition, resource_path)
  transition['attributes'] && transition['attributes']['href']['content'] || resource_path
end

#transitionsObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 50

def transitions
  @transitions ||= resources.inject([]) do |result_resources, resource|
    result_resources.push(resource['resource']['content']
                    .each_with_object([]) do |transition, result_transitions|
                            if transition?(transition)
                              result_transitions.push(transition_hash(transition, resource))
                            end
                          end)
  end.flatten
end

#without_group_actionsObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 77

def without_group_actions
  transitions.inject([]) do |result_transition, transition|
    result_transition.push(transition['transition']['content']
                     .each_with_object([]) do |content, result_contents|
                             next unless action?(content)

                             result_contents.push(Tomograph::ApiBlueprint::Drafter4::Yaml::Action.new(
                                                    content['content'],
                                                    transition['transition_path'],
                                                    transition['resource']
                                                  ))
                           end)
  end
end