Class: ApiMiniTester::Import::Postman
- Inherits:
-
Object
- Object
- ApiMiniTester::Import::Postman
- Defined in:
- lib/api_mini_tester/import/postman.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Instance Method Summary collapse
- #baseurl ⇒ Object
-
#initialize(json_collection) ⇒ Postman
constructor
A new instance of Postman.
- #name ⇒ Object
- #step(item, index) ⇒ Object
- #step_body(body) ⇒ Object
- #step_header(header) ⇒ Object
- #step_uri(uri) ⇒ Object
- #steps ⇒ Object
- #suite_base ⇒ Object
- #to_yaml ⇒ Object
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
#collection ⇒ Object (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
#baseurl ⇒ Object
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 |
#name ⇒ Object
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 |
#steps ⇒ Object
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_base ⇒ Object
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_yaml ⇒ Object
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 |