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(items) ⇒ Object
-
#initialize(json_collection) ⇒ Postman
constructor
A new instance of Postman.
- #max_common_url(url1, url2) ⇒ Object
- #name ⇒ Object
- #step(item) ⇒ Object
- #step_body(body) ⇒ Object
- #step_header(header) ⇒ Object
- #step_uri(uri) ⇒ Object
- #steps(items) ⇒ 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(items) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/api_mini_tester/import/postman.rb', line 47 def baseurl(items) base = '' items.each do |item| if item['item'] item_url = baseurl(item['item']) elsif item['request'] item_url = item['request']['url']['raw'] end base = max_common_url(base, item_url) end base end |
#max_common_url(url1, url2) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/api_mini_tester/import/postman.rb', line 39 def max_common_url(url1, url2) return "" unless url1 && url2 index = 0 index += 1 while url1[index] && url2[index] && url1[index] == url2[index] return "" if index.zero? url1[0..(index - 1)][0..(url1.rindex('/') - 1)] end |
#name ⇒ Object
35 36 37 |
# File 'lib/api_mini_tester/import/postman.rb', line 35 def name collection['info']['name'] end |
#step(item) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/api_mini_tester/import/postman.rb', line 90 def step(item) res = { 'step' => item['name'], '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
84 85 86 87 88 |
# File 'lib/api_mini_tester/import/postman.rb', line 84 def step_body(body) body ? JSON.parse(body) : {} rescue JSON::ParserError {} end |
#step_header(header) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/api_mini_tester/import/postman.rb', line 76 def step_header(header) res = {} header.each do |h| res[h['key']] = h['value'] end res end |
#step_uri(uri) ⇒ Object
72 73 74 |
# File 'lib/api_mini_tester/import/postman.rb', line 72 def step_uri(uri) uri.gsub("#{@base}/", "") end |
#steps(items) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/api_mini_tester/import/postman.rb', line 60 def steps(items) res = [] items.each do |item| if item['item'] res << steps(item['item']) elsif item['request'] res << step(item) end 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(collection['item']) }, '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(collection['item']) suite.to_yaml end |