Class: RswagSchemaExport::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/rswag_schema_export/client.rb', line 8

def initialize(stage)
  @app_name = ENV["APP_NAME"] || "app"
  @stage = stage || "develop"
end

Instance Attribute Details

#app_nameObject (readonly)

Returns the value of attribute app_name.



6
7
8
# File 'lib/rswag_schema_export/client.rb', line 6

def app_name
  @app_name
end

#stageObject (readonly)

Returns the value of attribute stage.



6
7
8
# File 'lib/rswag_schema_export/client.rb', line 6

def stage
  @stage
end

Instance Method Details

#clean(versions) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/rswag_schema_export/client.rb', line 73

def clean(versions)
  old_versions = versions - versions.sort.last(5)
  if aws_client?
    old_versions.each { |key| bucket.object(key).delete }
  else
    old_versions.each { |key| client.delete_blob(ENV["RSWAG_AZURE_CONTAINER"], key) }
  end
end

#clientObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rswag_schema_export/client.rb', line 13

def client # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity:
  if aws_client?
    abort("RSWAG_AWS_ACCESS_KEY_ID is not defined") unless ENV["RSWAG_AWS_ACCESS_KEY_ID"]
    abort("RSWAG_AWS_SECRET_ACCESS_KEY is not defined") unless ENV["RSWAG_AWS_SECRET_ACCESS_KEY"]
    abort("RSWAG_AWS_REGION is not defined") unless ENV["RSWAG_AWS_REGION"]
    abort("RSWAG_AWS_BUCKET is not defined") unless ENV["RSWAG_AWS_BUCKET"]

    @client ||= Aws::S3::Resource.new(access_key_id: ENV["RSWAG_AWS_ACCESS_KEY_ID"],
                                      secret_access_key: ENV["RSWAG_AWS_SECRET_ACCESS_KEY"],
                                      region: ENV["RSWAG_AWS_REGION"])
  else
    abort("RSWAG_AZURE_STORAGE_ACCOUNT_NAME is not defined") unless ENV["RSWAG_AZURE_STORAGE_ACCOUNT_NAME"]
    abort("RSWAG_AZURE_STORAGE_ACCESS_KEY is not defined") unless ENV["RSWAG_AZURE_STORAGE_ACCESS_KEY"]
    abort("RSWAG_AZURE_CONTAINER is not defined") unless ENV["RSWAG_AZURE_CONTAINER"]

    @client = Azure::Storage::Blob::BlobService.create(
      storage_account_name: ENV["RSWAG_AZURE_STORAGE_ACCOUNT_NAME"],
      storage_access_key: ENV["RSWAG_AZURE_STORAGE_ACCESS_KEY"]
    )
  end
end

#copy_latest_version_to_root(last_schema_key, schema_id) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/rswag_schema_export/client.rb', line 52

def copy_latest_version_to_root(last_schema_key, schema_id)
  if aws_client?
    bucket.object(last_schema_key)
          .copy_to("#{ENV['RSWAG_AWS_BUCKET']}/schemas/#{app_name}/#{stage}_#{schema_id}/schema.json")
  else
    client.copy_blob(ENV["RSWAG_AZURE_CONTAINER"], "schemas/#{app_name}/#{stage}_#{schema_id}/schema.json",
                     ENV["RSWAG_AZURE_CONTAINER"], last_schema_key)
  end
end

#download_file(schema_id, path) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rswag_schema_export/client.rb', line 62

def download_file(schema_id, path)
  FileUtils.mkdir_p(path.split("/")[0...-1].join("/"))
  if aws_client?
    bucket.object("schemas/#{app_name}/#{stage}_#{schema_id}/schema.json").download_file(path)
  else
    _blob, content = client.get_blob(ENV["RSWAG_AZURE_CONTAINER"],
                                     "schemas/#{app_name}/#{stage}_#{schema_id}/schema.json")
    ::File.open(path, "wb") { |f| f.write(content) }
  end
end

#fetch_versions(schema_id) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/rswag_schema_export/client.rb', line 43

def fetch_versions(schema_id)
  if aws_client?
    bucket.objects(prefix: "schemas/#{app_name}/#{stage}_#{schema_id}/versions").collect(&:key)
  else
    prefix = "schemas/#{app_name}/#{stage}_#{schema_id}/versions"
    client.list_blobs(ENV["RSWAG_AZURE_CONTAINER"], prefix: prefix).collect(&:name)
  end
end

#upload_file(key, file) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rswag_schema_export/client.rb', line 35

def upload_file(key, file)
  if aws_client?
    client.bucket(ENV["RSWAG_AWS_BUCKET"]).object(key).upload_file(file)
  else
    client.create_block_blob(ENV["RSWAG_AZURE_CONTAINER"], key, ::File.open(file, &:read))
  end
end