Class: RswagSchemaExport::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/rswag_schema_export/schema_import.rb

Instance Method Summary collapse

Instance Method Details

#runObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rswag_schema_export/schema_import.rb', line 5

def run
  abort("Set up RswagSchemaExport.config.schemas") unless RswagSchemaExport.config.schemas

  begin
    client = ::RswagSchemaExport::Client.new(ENV["STAGE"])
    RswagSchemaExport.config.schemas.map do |schema|
      # Copy previous version
      FileUtils.cp(schema, "#{schema}.previous") if File.exist?(schema)

      schema_id = schema.gsub(/[^a-zA-Z0-9\-]/, "_")

      # Copy latest version to root
      versions = client.fetch_versions(schema_id)
      last_schema_key = versions.max

      client.copy_latest_version_to_root(last_schema_key, schema_id)
      # Download schema.json
      client.download_file(schema_id, schema)
      # Clean versions folder
      client.clean(versions)

      puts("Schema has been successfully imported. Stage: #{client.stage} | Key: #{last_schema_key}")
      if ENV["SLACK_WEBHOOK_URL"]
        begin
          changes = Differ.call("#{schema}.previous", schema)

          unless changes.nil?
            Slack::Notifier.new(ENV["SLACK_WEBHOOK_URL"], username: 'Swagger API Differ').post(
              text: '', attachments: [{ color: 'good', text: "_Swagger schema has been updated:_ \n #{changes}"}])
          end
        rescue StandardError => e
          puts("Slack notification error: #{e}")
        end
      end
    end
    yield if block_given?
    puts("Import finished")
  rescue StandardError => e
    abort(e.message)
  end
end