apipie-postman 0.0.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/apipie-postman.rb +49 -39
  3. metadata +24 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b51cef8c7fc976fc2ccc65adea1a7a310e78a88f13012da523513e569daa6d0
4
- data.tar.gz: 270436badcf392d477d9a1ac1f734a5443a6ef2d6a34b1866fe230c0357c2075
3
+ metadata.gz: cb15fdaecb71ee97ad64d367620f122fc5ba847e682dfe5692c61b05d2fd8a98
4
+ data.tar.gz: 104174bf8f7dec3283fa1202e616c5d992d6496b8c6d99e3bb111e0293a0308f
5
5
  SHA512:
6
- metadata.gz: 7bd3da97802ae8f63790df791660075b51f165f9a941dbf29402a5d6f4dea6d3c89439e5460eb2356635bd1ca42432d378cc712eb22e07f936a48e6d944b566a
7
- data.tar.gz: 43627d5c8b2c6cebbe592592e2fc181e2fc8bf57ffdf694af7dcbfe944458521350e9cad7328a37e03a167f88b4a8489e2e118761d9c41df18595db016d04a49
6
+ metadata.gz: e6c6ddb7271df9f1ff84f4f939cc86c7cd1365fc71b8ec505d183841b2d6ca7be309ebf25e93c26cc4e843b09fb50c0102422459bc41e1869e46e7bc8d7e12d7
7
+ data.tar.gz: 6e31ad3dd6ccea47efb91c80fb566a84434ea173a06bcc397e3914555b2b4536497274da284924e8598a2b1b7a5c6e5cd49791747ab2b7b91ed50e25faaff925
@@ -3,7 +3,6 @@
3
3
  require 'json'
4
4
  require 'faraday'
5
5
 
6
- # General module for gem
7
6
  module ApipiePostman
8
7
  class << self
9
8
  attr_accessor :configuration
@@ -30,36 +29,19 @@ module ApipiePostman
30
29
  def self.generate_docs
31
30
  file = File.read('doc/apipie_examples.json')
32
31
  @docs = JSON.parse(file)
33
- docs_hashes = []
34
- endpoints_hashes = []
32
+ folder_hashes = []
35
33
 
36
- @docs.each_key do |key|
37
- docs_hashes << @docs[key]
38
- end
34
+ @docs = @docs.each_with_object({}) do |array, result|
35
+ key = array[0].to_s.split('#')[0]
36
+ (result[key] ||= []).concat(array[1])
37
+ end.transform_values(&:flatten)
39
38
 
40
- docs_hashes.each do |doc_hash|
41
- doc_hash.each do |endpoint|
42
- req_body = if endpoint['request_data'].nil?
43
- {}
44
- else
45
- endpoint['request_data']
46
- end
47
-
48
- endpoints_hashes << {
49
- name: endpoint['title'],
50
- request: {
51
- url: "#{self.configuration.base_url}#{endpoint['path']}",
52
- method: endpoint['verb'],
53
- header: [],
54
- body: {
55
- mode: 'raw',
56
- raw: req_body.to_json
57
- },
58
- description: endpoint['title']
59
- },
60
- response: []
61
- }
39
+ @docs.each_key do |key|
40
+ key_endpoint_hashes = []
41
+ @docs[key].each do |endpoint|
42
+ key_endpoint_hashes << create_endpoint_hash(endpoint, endpoint['request_data'] || {})
62
43
  end
44
+ folder_hashes << create_folder(key, key_endpoint_hashes)
63
45
  end
64
46
 
65
47
  body = {
@@ -69,25 +51,53 @@ module ApipiePostman
69
51
  description: 'Test description',
70
52
  schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
71
53
  },
72
- item: endpoints_hashes
54
+ item: folder_hashes
73
55
  }
74
56
  }.to_json
57
+
75
58
  headers = {
76
59
  'X-Api-Key': self.configuration.postman_api_key,
77
60
  'Content-Type': 'application/json'
78
61
  }
79
62
 
80
- Faraday.public_send(:post, 'https://api.getpostman.com/collections/', body, headers)
63
+ collection_uid = check_collection_uid_by_name(headers)
64
+
65
+ if collection_uid.nil?
66
+ Faraday.public_send(:post, 'https://api.getpostman.com/collections/', body, headers)
67
+ else
68
+ Faraday.public_send(:put, "https://api.getpostman.com/collections/#{collection_uid['uid']}", body, headers)
69
+ end
70
+ end
71
+
72
+ def self.check_collection_uid_by_name(headers)
73
+ response = Faraday.public_send(:get, 'https://api.getpostman.com/collections/', {}, headers)
74
+ JSON.parse(response.body)['collections'].select do |col|
75
+ col['name'] == self.configuration.postman_collection_name
76
+ end.last
77
+ end
81
78
 
82
- # if Object.const_defined?('POSTMAN_COLLECTION_UID') && Object.const_defined?('POSTMAN_COLLECTION_ID')
83
- # Faraday.public_send(:put, "https://api.getpostman.com/collections/#{Object.const_get('POSTMAN_COLLECTION_UID')}", body, headers)
84
- # else
85
- # response = Faraday.public_send(:post, 'https://api.getpostman.com/collections/', body, headers)
79
+ def self.create_endpoint_hash(endpoint, req_body)
80
+ {
81
+ name: endpoint['title'] == 'Default' ? "#{endpoint['verb']} #{endpoint['path']}" : endpoint['title'],
82
+ request: {
83
+ url: "#{self.configuration.base_url}#{endpoint['path']}",
84
+ method: endpoint['verb'],
85
+ header: [],
86
+ body: {
87
+ mode: 'raw',
88
+ raw: req_body.to_json
89
+ },
90
+ description: endpoint['title']
91
+ },
92
+ response: []
93
+ }
94
+ end
86
95
 
87
- # if response.status == 200
88
- # Object.const_set('POSTMAN_COLLECTION_UID', JSON.parse(response.body)['collection']['uid'])
89
- # Object.const_set('POSTMAN_COLLECTION_ID', JSON.parse(response.body)['collection']['id'])
90
- # end
91
- # end
96
+ def self.create_folder(key, item_hashes)
97
+ {
98
+ name: key,
99
+ description: key,
100
+ item: item_hashes
101
+ }
92
102
  end
93
103
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apipie-postman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Motogna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-25 00:00:00.000000000 Z
11
+ date: 2021-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -58,8 +58,28 @@ dependencies:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '3.10'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.22'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.22.1
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.22'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 1.22.1
61
81
  description: Use 'bundle exec apipie-postman' and 'rake apipie_postman' to generate
62
- the docs! More details on https://github.com/VictorMotogna/apipie-postman
82
+ the docs!
63
83
  email: vmotogna@gmail.com
64
84
  executables:
65
85
  - apipie-postman
@@ -80,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
100
  requirements:
81
101
  - - "~>"
82
102
  - !ruby/object:Gem::Version
83
- version: '2.4'
103
+ version: '2.5'
84
104
  required_rubygems_version: !ruby/object:Gem::Requirement
85
105
  requirements:
86
106
  - - ">="