Class: Ubiquity::Wiredrive::PresentationUtility

Inherits:
Object
  • Object
show all
Defined in:
lib/ubiquity/wiredrive/presentation_utility.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = { }) ⇒ PresentationUtility

Returns a new instance of PresentationUtility.



12
13
14
15
16
17
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 12

def initialize(args = { })
  @initial_args = args

  initialize_logger(args)
  initialize_api_client(args)
end

Instance Attribute Details

#api_clientObject

Returns the value of attribute api_client.



10
11
12
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 10

def api_client
  @api_client
end

#initial_argsObject

Returns the value of attribute initial_args.



10
11
12
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 10

def initial_args
  @initial_args
end

#loggerObject

Returns the value of attribute logger.



10
11
12
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 10

def logger
  @logger
end

Instance Method Details

#download_file(download_file_path, destination_file_path, overwrite = false) ⇒ Hash

Downloads a file from a URI or file location and saves it to a local path

Parameters:

  • download_file_path (String)

    The source path of the file being downloaded

  • destination_file_path (String)

    The destination path for the file being downloaded

  • overwrite (Boolean) (defaults to: false)

    Determines if the destination file will be overwritten if it is found to exist

Returns:

  • (Hash)
    • :download_file_path [String] The source path of the file being downloaded

    • :overwrite [Boolean] The value of the overwrite parameter when the method was called

    • :file_downloaded [Boolean] Indicates if the file was downloaded, will be false if overwrite was true and the file existed

    • :destination_file_existed [String|Boolean] The value will be ‘unknown’ if overwrite is true because the file exist check will not have been run inside of the method

    • :destination_file_path [String] The destination path for the file being downloaded



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 80

def download_file(download_file_path, destination_file_path, overwrite = false)
  logger.debug { "Downloading '#{download_file_path}' -> '#{destination_file_path}' Overwrite: #{overwrite}" }
  file_existed = 'unknown'
  if overwrite or not (file_existed = File.exists?(destination_file_path))
    File.open(destination_file_path, 'wb') { |tf|
      open(download_file_path) { |sf| tf.write sf.read }
    }
    file_downloaded = true
  else
    file_downloaded = false
  end
  { :download_file_path => download_file_path, :overwrite => overwrite, :file_downloaded => file_downloaded, :destination_file_existed => file_existed, :destination_file_path => destination_file_path }
end

#initialize_api_client(args = { }) ⇒ Object



23
24
25
26
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 23

def initialize_api_client(args = { })
  client_args = {  }
  @api_client = Ubiquity::Wiredrive::API::V3::Client.new(client_args)
end

#initialize_logger(args = { }) ⇒ Object



19
20
21
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 19

def initialize_logger(args = { })
  @logger = args[:logger] || Logger.new(STDOUT)
end

#presentation_asset_download(asset, destination_path, options = { }) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 42

def presentation_asset_download(asset, destination_path, options = { })
  destination_dir = destination_path
  overwrite = options.fetch(:overwrite, false)

  media_elements = asset['media']
  # media_elements.concat asset['thumbnails']
  media_elements_download_responses = media_elements.map do |media|
    url = media['downloadUrl'] || media['url']
    uri = URI(url)
    file_name = CGI.unescape(File.basename(uri.path))
    destination_file_path = File.join(destination_dir, file_name)
    download_file(url, destination_file_path, overwrite).merge(media: media, asset: asset)
  end

  thumbnails = asset['thumbnails']
  thumbnails_download_responses = thumbnails.map do |media|
    url = media['downloadUrl'] || media['url']
    uri = URI(url)
    file_name = CGI.unescape(File.basename(uri.path)) + "_thumbnail_#{media['category']}.#{media['extension']}"
    destination_file_path = File.join(destination_dir, file_name)
    download_file(url, destination_file_path, overwrite).merge(media: media, asset: asset)
  end

  { media_element_download_responses: media_elements_download_responses, thumbnail_download_responses: thumbnails_download_responses }
end

#presentation_assets_download(args = { }, options = { }) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 28

def presentation_assets_download(args = { }, options = { })
  assets = args[:assets] || begin
    invitation_token = args[:presentation_invitation_token] || args[:invitation_token]
    invitation_password = args[:presentation_password] || args[:password]
    presentation_assets_get_using_token(invitation_token, invitation_password)
  end
  destination_path = args[:destination_path]
  assets.map do |a|
    r = presentation_asset_download(a, destination_path, options).merge(asset: a)
    yield r if block_given?
    r
  end
end

#presentation_assets_get_using_token(presentation_invitation_token, presentation_password = nil) ⇒ Object



95
96
97
98
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 95

def presentation_assets_get_using_token(presentation_invitation_token, presentation_password = nil)
  r = presentation_get_using_token(presentation_invitation_token, presentation_password)
  r['assets']
end

#presentation_get_using_token(presentation_invitation_token, presentation_password = nil) ⇒ Object

Parameters:

  • presentation_invitation_token (Object)
  • presentation_password (Object) (defaults to: nil)

Returns:

  • (Object)


103
104
105
106
107
108
# File 'lib/ubiquity/wiredrive/presentation_utility.rb', line 103

def presentation_get_using_token(presentation_invitation_token, presentation_password = nil)
  auth_token = api_client.presentation_authorize_get(token: presentation_invitation_token, password: presentation_password)
  presentation_id = api_client.response['presentation']['id']
  api_client.auth_token = auth_token
  presentation_elements = api_client.presentation_get(:id => presentation_id)
end