Class: ApiMiniTester::Import::Postman

Inherits:
Object
  • Object
show all
Defined in:
lib/api_mini_tester/import/postman.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_collection) ⇒ Postman

Returns a new instance of Postman.



9
10
11
# File 'lib/api_mini_tester/import/postman.rb', line 9

def initialize(json_collection)
  @collection = JSON.parse json_collection
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



7
8
9
# File 'lib/api_mini_tester/import/postman.rb', line 7

def collection
  @collection
end

Instance Method Details

#baseurlObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/api_mini_tester/import/postman.rb', line 39

def baseurl
  @base ||= begin
    base = collection['item'][0]['request']['url']['raw']
    collection['item'].each do |item|
      item_url = item['request']['url']['raw']
      index = 0
      index += 1 while base[index] && item_url[index] && base[index] == item_url[index]
      base = base[0..(index - 1)][0..(base.rindex('/') - 1)]
    end
    base
  end
  @base
end

#nameObject



35
36
37
# File 'lib/api_mini_tester/import/postman.rb', line 35

def name
  collection['info']['name']
end

#step(item, index) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/api_mini_tester/import/postman.rb', line 79

def step(item, index)
  res = {
    'step' => index,
    'name' => item['name'],
    'method' => item['request']['method'],
    'uri' => step_uri(item['request']['url']['raw']),
    'input' => {
      'header' => step_header(item['request']['header']),
      'body' => step_body(item['request']['body']['raw'])
    },
    'output' => {
      'header' => {},
      'body' => {}
    }
  }
  res
end

#step_body(body) ⇒ Object



75
76
77
# File 'lib/api_mini_tester/import/postman.rb', line 75

def step_body(body)
  body ? JSON.parse(body) : {}
end

#step_header(header) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/api_mini_tester/import/postman.rb', line 67

def step_header(header)
  res = {}
  header.each do |h|
    res[h['key']] = h['value']
  end
  res
end

#step_uri(uri) ⇒ Object



63
64
65
# File 'lib/api_mini_tester/import/postman.rb', line 63

def step_uri(uri)
  uri.gsub("#{@base}/", "")
end

#stepsObject



53
54
55
56
57
58
59
60
61
# File 'lib/api_mini_tester/import/postman.rb', line 53

def steps
  res = []
  index = 0
  collection['item'].each do |item|
    res << step(item, index)
    index += 1
  end
  res
end

#suite_baseObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/api_mini_tester/import/postman.rb', line 19

def suite_base
  @suite_base ||= {
    'name' => name,
    'desc' => "Imported from postman collection: #{name}",
    'settings' => {
      'baseurl' => baseurl
    },
    'tests' => [
      {
        'name' => "Test scenario based on postman collection #{name}",
        'steps' => []
      }
    ]
  }
end

#to_yamlObject



13
14
15
16
17
# File 'lib/api_mini_tester/import/postman.rb', line 13

def to_yaml
  suite = suite_base
  suite['tests'][0]['steps'] = steps
  suite.to_yaml
end