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, apib_path, drafter_yaml_path) ⇒ Yaml

Returns a new instance of Yaml.



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

def initialize(prefix, apib_path, drafter_yaml_path)
  @prefix = prefix
  @documentation = if apib_path
                     YAML.safe_load(`drafter #{apib_path}`)
                   elsif drafter_yaml_path
                     YAML.safe_load(File.read(drafter_yaml_path))
                   end
end

Instance Method Details

#action?(content) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 87

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

#action_hash(related_actions) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 99

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

#actionsObject



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

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)


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

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



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

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)


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

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

#resource_path(resource) ⇒ Object



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

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

#resourcesObject



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

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

#single_resource?(group) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#to_resourcesObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 116

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



110
111
112
113
114
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 110

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

#transition?(transition) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 59

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

#transition_hash(transition, resource) ⇒ Object



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

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



71
72
73
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 71

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

#transitionsObject



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

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

#without_group_actionsObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tomograph/api_blueprint/drafter_4/yaml.rb', line 75

def without_group_actions
  transitions.inject([]) do |result_transition, transition|
    result_transition.push(transition['transition']['content'].each_with_object([]) do |content, result_contents|
      result_contents.push(Tomograph::ApiBlueprint::Drafter4::Yaml::Action.new(
                             content['content'],
                             transition['transition_path'],
                             transition['resource']
      )) if action?(content)
    end)
  end
end