ubiquity-wiredrive 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ccd5f3318034434414e3b07000317126d2e6d1f4bf30f2cdfc5f8680911f011
4
- data.tar.gz: cc92e7e91a7cedaea4e2f4aa48477c1f9b0fa12b8557cd8033eb05f8811b829b
3
+ metadata.gz: ea7fb1e3e3969008101e9128dc102dfeea1fce1b6d2e9eeafa46d6c2225d7ba6
4
+ data.tar.gz: 47d5922424490ccbf839f32782354fcdeb93e79a85ad1a3ec285a80a4a52c1da
5
5
  SHA512:
6
- metadata.gz: 99bfad05b74cfd9194b19a3b6966fc7565aa98bd103b81d45300726b928536a868a4624bf95897dfe1d195b0c063bce5e10cbd471eb6e25a43fd6b983e3be309
7
- data.tar.gz: 1291cb4b05760c751152837b1556257c84e2d94a20808d390c5878e8bdf2a6acd6deb82ad61f2dcada727ca5daf5fef16b96986f3a930176505f5b3dc534aab2
6
+ metadata.gz: e81c8126843e593ee4b48bbfe1a9490accb53af107f1fef871dc0acc45ed4f4c3384cca87b6d5b59f33ca73ef948376d0563e405965915b9287503b507495c55
7
+ data.tar.gz: 7a2b5521063f885c5cbd0fe0bd480124bf63fa84072fbc0ce82d76e7290ba253661cc4cb3516c21a44174517c2ce3928d00ea4cb0378ac14298687a70351c0a4
@@ -0,0 +1,113 @@
1
+ require 'cgi'
2
+ require 'logger'
3
+ require 'uri'
4
+
5
+ require 'ubiquity/wiredrive/api/v3/client'
6
+ module Ubiquity::Wiredrive
7
+
8
+ class PresentationUtility
9
+
10
+ attr_accessor :initial_args, :logger, :api_client
11
+
12
+ def initialize(args = { })
13
+ @initial_args = args
14
+
15
+ initialize_logger(args)
16
+ initialize_api_client(args)
17
+ end
18
+
19
+ def initialize_logger(args = { })
20
+ @logger = args[:logger] || Logger.new(STDOUT)
21
+ end
22
+
23
+ def initialize_api_client(args = { })
24
+ client_args = { }
25
+ @api_client = Ubiquity::Wiredrive::API::V3::Client.new(client_args)
26
+ end
27
+
28
+ def presentation_assets_download(args = { }, options = { })
29
+ assets = args[:assets] || begin
30
+ invitation_token = args[:presentation_invitation_token] || args[:invitation_token]
31
+ invitation_password = args[:presentation_password] || args[:password]
32
+ presentation_assets_get_using_token(invitation_token, invitation_password)
33
+ end
34
+ destination_path = args[:destination_path]
35
+ assets.map do |a|
36
+ r = presentation_asset_download(a, destination_path, options).merge(asset: a)
37
+ yield r if block_given?
38
+ r
39
+ end
40
+ end
41
+
42
+ def presentation_asset_download(asset, destination_path, options = { })
43
+ destination_dir = destination_path
44
+ overwrite = options.fetch(:overwrite, false)
45
+
46
+ media_elements = asset['media']
47
+ # media_elements.concat asset['thumbnails']
48
+ media_elements_download_responses = media_elements.map do |media|
49
+ url = media['downloadUrl'] || media['url']
50
+ uri = URI(url)
51
+ file_name = CGI.unescape(File.basename(uri.path))
52
+ destination_file_path = File.join(destination_dir, file_name)
53
+ download_file(url, destination_file_path, overwrite).merge(media: media, asset: asset)
54
+ end
55
+
56
+ thumbnails = asset['thumbnails']
57
+ thumbnails_download_responses = thumbnails.map do |media|
58
+ url = media['downloadUrl'] || media['url']
59
+ uri = URI(url)
60
+ file_name = CGI.unescape(File.basename(uri.path)) + "_thumbnail_#{media['category']}.#{media['extension']}"
61
+ destination_file_path = File.join(destination_dir, file_name)
62
+ download_file(url, destination_file_path, overwrite).merge(media: media, asset: asset)
63
+ end
64
+
65
+ { media_element_download_responses: media_elements_download_responses, thumbnail_download_responses: thumbnails_download_responses }
66
+ end
67
+
68
+ # Downloads a file from a URI or file location and saves it to a local path
69
+ #
70
+ # @param [String] download_file_path The source path of the file being downloaded
71
+ # @param [String] destination_file_path The destination path for the file being downloaded
72
+ # @param [Boolean] overwrite Determines if the destination file will be overwritten if it is found to exist
73
+ #
74
+ # @return [Hash]
75
+ # * :download_file_path [String] The source path of the file being downloaded
76
+ # * :overwrite [Boolean] The value of the overwrite parameter when the method was called
77
+ # * :file_downloaded [Boolean] Indicates if the file was downloaded, will be false if overwrite was true and the file existed
78
+ # * :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
79
+ # * :destination_file_path [String] The destination path for the file being downloaded
80
+ def download_file(download_file_path, destination_file_path, overwrite = false)
81
+ logger.debug { "Downloading '#{download_file_path}' -> '#{destination_file_path}' Overwrite: #{overwrite}" }
82
+ file_existed = 'unknown'
83
+ if overwrite or not (file_existed = File.exists?(destination_file_path))
84
+ File.open(destination_file_path, 'wb') { |tf|
85
+ open(download_file_path) { |sf| tf.write sf.read }
86
+ }
87
+ file_downloaded = true
88
+ else
89
+ file_downloaded = false
90
+ end
91
+ { :download_file_path => download_file_path, :overwrite => overwrite, :file_downloaded => file_downloaded, :destination_file_existed => file_existed, :destination_file_path => destination_file_path }
92
+ end
93
+
94
+
95
+ def presentation_assets_get_using_token(presentation_invitation_token, presentation_password = nil)
96
+ r = presentation_get_using_token(presentation_invitation_token, presentation_password)
97
+ r['assets']
98
+ end
99
+
100
+ # @param [Object] presentation_invitation_token
101
+ # @param [Object] presentation_password
102
+ # @return [Object]
103
+ def presentation_get_using_token(presentation_invitation_token, presentation_password = nil)
104
+ auth_token = api_client.presentation_authorize_get(token: presentation_invitation_token, password: presentation_password)
105
+ presentation_id = api_client.response['presentation']['id']
106
+ api_client.auth_token = auth_token
107
+ presentation_elements = api_client.presentation_get(:id => presentation_id)
108
+ end
109
+
110
+
111
+ end
112
+
113
+ end
@@ -1,5 +1,5 @@
1
1
  module Ubiquity
2
2
  module Wiredrive
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ubiquity-wiredrive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Whitson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-23 00:00:00.000000000 Z
11
+ date: 2018-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -87,6 +87,7 @@ files:
87
87
  - lib/ubiquity/wiredrive/api/v3/client/requests/base_request.rb
88
88
  - lib/ubiquity/wiredrive/api/v3/http_client.rb
89
89
  - lib/ubiquity/wiredrive/api/v3/utilities.rb
90
+ - lib/ubiquity/wiredrive/presentation_utility.rb
90
91
  - lib/ubiquity/wiredrive/version.rb
91
92
  - ubiquity-wiredrive.gemspec
92
93
  homepage: https://www.github.com/XPlatform-Consulting/ubiquity-wiredrive