Class: Spaceship::ConnectAPI::AppClipHeaderImage

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb

Instance Attribute Summary collapse

Attributes included from Model

#id, #reverse_attr_map

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#attr_mapping, included, #initialize, #reverse_attr_mapping, #to_json, #update_attributes

Instance Attribute Details

#asset_delivery_stateObject

Returns the value of attribute asset_delivery_state.



21
22
23
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 21

def asset_delivery_state
  @asset_delivery_state
end

#asset_tokenObject

Returns the value of attribute asset_token.



18
19
20
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 18

def asset_token
  @asset_token
end

#asset_typeObject

Returns the value of attribute asset_type.



19
20
21
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 19

def asset_type
  @asset_type
end

#file_nameObject

Returns the value of attribute file_name.



15
16
17
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 15

def file_name
  @file_name
end

#file_sizeObject

Much of the functionality below is modified from ‘spaceship/connect_api/models/app_screenshot.rb`



14
15
16
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 14

def file_size
  @file_size
end

#image_assetObject

Returns the value of attribute image_asset.



17
18
19
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 17

def image_asset
  @image_asset
end

#source_file_checksumObject

Returns the value of attribute source_file_checksum.



16
17
18
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 16

def source_file_checksum
  @source_file_checksum
end

#upload_operationsObject

Returns the value of attribute upload_operations.



20
21
22
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 20

def upload_operations
  @upload_operations
end

#uploadedObject

Returns the value of attribute uploaded.



22
23
24
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 22

def uploaded
  @uploaded
end

Class Method Details

.create(client: nil, app_clip_default_experience_localization_id: nil, path: nil, wait_for_processing: true) ⇒ Object

API



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 82

def self.create(client: nil, app_clip_default_experience_localization_id: nil, path: nil, wait_for_processing: true)
  client ||= Spaceship::ConnectAPI
  require 'faraday'

  filename = File.basename(path)
  filesize = File.size(path)
  bytes = File.binread(path)

  post_attributes = {
    fileSize: filesize,
    fileName: filename
  }

  # Create placeholder to upload the app clip header image
  begin
    app_clip_header_image = client.post_app_clip_header_image(
      app_clip_default_experience_localization_id: app_clip_default_experience_localization_id,
      attributes: post_attributes
    ).first
  rescue => error
    raise "Unable to create app clip header image reservation: #{error}"
  end

  # Upload the file
  upload_operations = app_clip_header_image.upload_operations
  Spaceship::ConnectAPI::FileUploader.upload(upload_operations, bytes)

  # Update file uploading complete
  patch_attributes = {
    uploaded: true,
    sourceFileChecksum: Digest::MD5.hexdigest(bytes)
  }

  # Patch app clip header image that file upload is complete
  # Catch error if patch retries due to 504. Original patch
  # may go through by return response as 504.
  begin
    app_clip_header_image = Spaceship::ConnectAPI.patch_app_clip_header_image(
      app_clip_header_image_id: app_clip_header_image.id,
      attributes: patch_attributes
    ).first
  rescue => error
    puts("Failed to patch app clip header image. Update may have gone through so verifying") if Spaceship::Globals.verbose?

    app_clip_header_image = client.get_app_clip_header_image(app_clip_header_image_id: app_clip_header_image.id).first
    raise error unless app_clip_header_image.complete?
  end

  # Wait for processing
  if wait_for_processing
    loop do
      if app_clip_header_image.complete?
        puts("App clip header image processing complete!") if Spaceship::Globals.verbose?
        break
      elsif app_clip_header_image.error?
        messages = ["Error processing app clip header image '#{app_clip_header_image.file_name}'"] + app_clip_header_image.error_messages
        raise messages.join(". ")
      end

      # Poll every 2 seconds
      sleep_time = 2
      puts("Waiting #{sleep_time} seconds before checking status of processing...") if Spaceship::Globals.verbose?
      sleep(sleep_time)

      app_clip_header_image = client.get_app_clip_header_image(app_clip_header_image_id: app_clip_header_image.id).first
    end
  end

  return app_clip_header_image
end

.typeObject



36
37
38
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 36

def self.type
  'appClipHeaderImages'
end

Instance Method Details

#awaiting_upload?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 40

def awaiting_upload?
  (asset_delivery_state || {})["state"] == "AWAITING_UPLOAD"
end

#complete?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 44

def complete?
  (asset_delivery_state || {})["state"] == "COMPLETE"
end

#delete!(client: nil, filter: {}, includes: nil, limit: nil, sort: nil) ⇒ Object



153
154
155
156
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 153

def delete!(client: nil, filter: {}, includes: nil, limit: nil, sort: nil)
  client ||= Spaceship::ConnectAPI
  client.delete_app_clip_header_image(app_clip_header_image_id: id)
end

#error?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 48

def error?
  (asset_delivery_state || {})["state"] == "FAILED"
end

#error_messagesObject



52
53
54
55
56
57
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 52

def error_messages
  errors = (asset_delivery_state || {})["errors"]
  (errors || []).map do |error|
    [error["code"], error["description"]].compact.join(" - ")
  end
end

#image_asset_url(width: nil, height: nil, type: "png") ⇒ Object

This does not download the source image (exact image that was uploaded) This downloads a modified version. This image won’t have the same checksums as source_file_checksum.

There is an open radar for allowing downloading of source file. openradar.appspot.com/radar?id=4980344105205760



65
66
67
68
69
70
71
72
73
74
75
76
# File 'spaceship/lib/spaceship/connect_api/models/app_clip_header_image.rb', line 65

def image_asset_url(width: nil, height: nil, type: "png")
  return nil if image_asset.nil?

  template_url = image_asset["templateUrl"]
  width ||= image_asset["width"]
  height ||= image_asset["height"]

  return template_url
         .gsub("{w}", width.to_s)
         .gsub("{h}", height.to_s)
         .gsub("{f}", type)
end