Class: Rpdoc::PostmanCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/rpdoc/postman_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration: nil, data: nil) ⇒ PostmanCollection

Returns a new instance of PostmanCollection.



11
12
13
14
15
16
17
18
# File 'lib/rpdoc/postman_collection.rb', line 11

def initialize(configuration: nil, data: nil)
  @configuration = configuration || Rpdoc.configuration
  @requester = JsonRequester.new(@configuration.postman_host)
  @data = data&.deep_symbolize_keys || generated_collection_data

  self.clean_empty_folders!
  self.reordering!
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/rpdoc/postman_collection.rb', line 9

def data
  @data
end

Instance Method Details

#clean_empty_folders!Object



59
60
61
# File 'lib/rpdoc/postman_collection.rb', line 59

def clean_empty_folders!
  clean_empty_folders_from(@data[:collection][:item])
end

#clean_generated_responses!Object



63
64
65
# File 'lib/rpdoc/postman_collection.rb', line 63

def clean_generated_responses!
  clean_generated_responses_from(@data[:collection][:item])
end

#merge!(other_collection) ⇒ Object



52
53
54
55
56
57
# File 'lib/rpdoc/postman_collection.rb', line 52

def merge!(other_collection)
  @data[:collection][:info][:name] = other_collection.data[:collection][:info][:name]
  @data[:collection][:info][:description] = other_collection.data[:collection][:info][:description]
  insert_generated_responses_into(@data[:collection][:item], from_collection_items: other_collection.data[:collection][:item].to_a)
  sort_folders_from(@data[:collection][:item])
end

#pushObject



20
21
22
# File 'lib/rpdoc/postman_collection.rb', line 20

def push
  send(@configuration.rpdoc_auto_push_strategy)
end

#push_and_createObject



24
25
26
27
28
29
30
# File 'lib/rpdoc/postman_collection.rb', line 24

def push_and_create
  path = "#{@configuration.postman_collection_path}?workspace=#{@configuration.collection_workspace}"
  headers = {
    'X-Api-Key': @configuration.postman_apikey
  }
  @requester.http_send(:post, path, @data, headers)
end

#push_and_updateObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rpdoc/postman_collection.rb', line 32

def push_and_update
  path = "#{@configuration.postman_collection_path}/#{@configuration.collection_uid}"
  headers = {
    'X-Api-Key': @configuration.postman_apikey
  }
  remote_collection_data = @requester.http_send(:get, path, {}, headers)
  remote_collection_data = remote_collection_data['status'] == 200 ? remote_collection_data.deep_symbolize_keys.slice(:collection) : nil
  remote_collection = PostmanCollection.new(data: remote_collection_data)
  remote_collection.clean_generated_responses!
  remote_collection.merge!(self)
  @requester.http_send(:put, path, remote_collection.data, headers, content_type_charset: nil)
end

#reordering!Object



67
68
69
# File 'lib/rpdoc/postman_collection.rb', line 67

def reordering!
  sort_folders_from(@data[:collection][:item])
end

#save(path: nil) ⇒ Object



45
46
47
48
49
50
# File 'lib/rpdoc/postman_collection.rb', line 45

def save(path: nil)
  path ||= "#{@configuration.rpdoc_root}/#{@configuration.rpdoc_collection_filename}"
  File.open(path, 'w+') do |f|
    f.write(JSON.pretty_generate(@data))
  end
end