Module: ApipiePostman

Defined in:
lib/apipie-postman.rb

Defined Under Namespace

Classes: Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



8
9
10
# File 'lib/apipie-postman.rb', line 8

def configuration
  @configuration
end

Class Method Details

.check_collection_uid_by_name(headers) ⇒ Object



72
73
74
75
76
77
# File 'lib/apipie-postman.rb', line 72

def self.check_collection_uid_by_name(headers)
  response = Faraday.public_send(:get, 'https://api.getpostman.com/collections/', {}, headers)
  JSON.parse(response.body)['collections'].select do |col|
    col['name'] == self.configuration.postman_collection_name
  end.last
end

.configure {|configuration| ... } ⇒ Object

Yields:



11
12
13
14
# File 'lib/apipie-postman.rb', line 11

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.create_endpoint_hash(endpoint, req_body) ⇒ Object



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

def self.create_endpoint_hash(endpoint, req_body)
  {
    name: "#{endpoint['verb']} #{endpoint['path']}",
    request: {
      url: "#{self.configuration.base_url}#{endpoint['path']}",
      method: endpoint['verb'],
      header: [],
      body: {
        mode: 'raw',
        raw: req_body.to_json
      },
      description: endpoint['title']
    },
    response: []
  }
end

.create_folder(key, item_hashes) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/apipie-postman.rb', line 96

def self.create_folder(key, item_hashes)
  {
    name: key,
    description: key,
    item: item_hashes
  }
end

.generate_docsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/apipie-postman.rb', line 29

def self.generate_docs
  file = File.read('doc/apipie_examples.json')
  @docs = JSON.parse(file)
  folder_hashes = []

  @docs = @docs.each_with_object({}) do |array, result|
    key = array[0].to_s.split('#')[0]
    (result[key] ||= []).concat(array[1])
  end.transform_values(&:flatten)

  @docs.each_key do |key|
    key_endpoint_hashes = []
    @docs[key].each do |endpoint|
      key_endpoint_hashes << create_endpoint_hash(endpoint, endpoint['request_data'] || {})
    end
    folder_hashes << create_folder(key, key_endpoint_hashes)
  end

  body = {
    collection: {
      info: {
        name: self.configuration.postman_collection_name,
        description: 'Test description',
        schema: 'https://schema.getpostman.com/json/collection/v2.1.0/collection.json'
      },
      item: folder_hashes
    }
  }.to_json

  headers = {
    'X-Api-Key': self.configuration.postman_api_key,
    'Content-Type': 'application/json'
  }

  collection_uid = check_collection_uid_by_name(headers)

  if collection_uid.nil?
    Faraday.public_send(:post, 'https://api.getpostman.com/collections/', body, headers)
  else
    Faraday.public_send(:put, "https://api.getpostman.com/collections/#{collection_uid['uid']}", body, headers)
  end
end