Class: Spaceship::ConnectAPI::AppPreview

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
spaceship/lib/spaceship/connect_api/models/app_preview.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.



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

def asset_delivery_state
  @asset_delivery_state
end

#file_nameObject

Returns the value of attribute file_name.



13
14
15
# File 'spaceship/lib/spaceship/connect_api/models/app_preview.rb', line 13

def file_name
  @file_name
end

#file_sizeObject

Returns the value of attribute file_size.



12
13
14
# File 'spaceship/lib/spaceship/connect_api/models/app_preview.rb', line 12

def file_size
  @file_size
end

#mime_typeObject

Returns the value of attribute mime_type.



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

def mime_type
  @mime_type
end

#preview_frame_time_codeObject

Returns the value of attribute preview_frame_time_code.



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

def preview_frame_time_code
  @preview_frame_time_code
end

#preview_imageObject

Returns the value of attribute preview_image.



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

def preview_image
  @preview_image
end

#source_file_checksumObject

Returns the value of attribute source_file_checksum.



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

def source_file_checksum
  @source_file_checksum
end

#uploadObject

Returns the value of attribute upload.



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

def upload
  @upload
end

#upload_operationsObject

Returns the value of attribute upload_operations.



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

def upload_operations
  @upload_operations
end

#video_urlObject

Returns the value of attribute video_url.



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

def video_url
  @video_url
end

Class Method Details

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

Creates an AppPreview in an AppPreviewSet Setting the optional frame_time_code will force polling until video is done processing

Parameters:

  • app_preview_set_id (defaults to: nil)

    The AppPreviewSet id

  • path (defaults to: nil)

    The path of the file

  • frame_time_code (defaults to: nil)

    The time code for the preview still frame (ex: “00:00:07:01”)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'spaceship/lib/spaceship/connect_api/models/app_preview.rb', line 58

def self.create(client: nil, app_preview_set_id: nil, path: nil, wait_for_processing: true, frame_time_code: nil)
  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
  preview = client.post_app_preview(
    app_preview_set_id: app_preview_set_id,
    attributes: post_attributes
  ).first

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

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

  begin
    preview = client.patch_app_preview(
      app_preview_id: preview.id,
      attributes: patch_attributes
    ).first
  rescue => error
    puts("Failed to patch app preview. Update may have gone through so verifying") if Spaceship::Globals.verbose?

    preview = Spaceship::ConnectAPI::AppPreview.get(client: client, app_preview_id: preview.id)
    raise error unless preview.complete?
  end

  # Poll for video processing completion to set still frame time
  wait_for_processing = true unless frame_time_code.nil?
  if wait_for_processing
    loop do
      unless preview.video_url.nil?
        puts("Preview processing complete!") if Spaceship::Globals.verbose?
        preview = preview.update(attributes: {
          previewFrameTimeCode: frame_time_code
        })
        puts("Updated preview frame time code!") if Spaceship::Globals.verbose?
        break
      end

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

      preview = Spaceship::ConnectAPI::AppPreview.get(client: client, app_preview_id: preview.id)
    end
  end

  preview
end

.get(client: nil, app_preview_id: nil) ⇒ Object

API



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

def self.get(client: nil, app_preview_id: nil)
  client ||= Spaceship::ConnectAPI
  client.get_app_preview(app_preview_id: app_preview_id).first
end

.typeObject



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

def self.type
  return "appPreviews"
end

Instance Method Details

#complete?Boolean

Returns:

  • (Boolean)


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

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

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



129
130
131
132
# File 'spaceship/lib/spaceship/connect_api/models/app_preview.rb', line 129

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

#update(client: nil, attributes: nil) ⇒ Object



123
124
125
126
127
# File 'spaceship/lib/spaceship/connect_api/models/app_preview.rb', line 123

def update(client: nil, attributes: nil)
  client ||= Spaceship::ConnectAPI
  attributes = reverse_attr_mapping(attributes)
  client.patch_app_preview(app_preview_id: id, attributes: attributes).first
end