microslop_one_drive 2.1.0 → 3.0.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.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +67 -7
  3. data/lib/microslop_one_drive/batch/batch_response.rb +15 -0
  4. data/lib/microslop_one_drive/batch/response.rb +18 -0
  5. data/lib/microslop_one_drive/client.rb +98 -42
  6. data/lib/microslop_one_drive/deserializers/drive_deserializer.rb +57 -0
  7. data/lib/microslop_one_drive/deserializers/drive_item_deserializer.rb +67 -0
  8. data/lib/microslop_one_drive/deserializers/parent_reference_deserializer.rb +20 -0
  9. data/lib/microslop_one_drive/deserializers/quota_deserializer.rb +24 -0
  10. data/lib/microslop_one_drive/deserializers/shared_with_me_item_deserializer.rb +48 -0
  11. data/lib/microslop_one_drive/deserializers/user_deserializer.rb +29 -0
  12. data/lib/microslop_one_drive/drive.rb +40 -9
  13. data/lib/microslop_one_drive/drive_item.rb +61 -63
  14. data/lib/microslop_one_drive/file.rb +13 -0
  15. data/lib/microslop_one_drive/folder.rb +13 -0
  16. data/lib/microslop_one_drive/list_responses/drive_item_list.rb +21 -0
  17. data/lib/microslop_one_drive/list_responses/drive_list.rb +15 -0
  18. data/lib/microslop_one_drive/list_responses/list_response.rb +21 -0
  19. data/lib/microslop_one_drive/list_responses/permission_list.rb +27 -0
  20. data/lib/microslop_one_drive/list_responses/shared_with_me_list.rb +15 -0
  21. data/lib/microslop_one_drive/parent_reference.rb +14 -0
  22. data/lib/microslop_one_drive/permission_set.rb +13 -15
  23. data/lib/microslop_one_drive/quota.rb +14 -0
  24. data/lib/microslop_one_drive/root_folder.rb +13 -0
  25. data/lib/microslop_one_drive/shared_with_me_item.rb +44 -0
  26. data/lib/microslop_one_drive/user.rb +33 -5
  27. data/lib/microslop_one_drive/utils.rb +10 -0
  28. data/lib/microslop_one_drive/version.rb +1 -1
  29. data/lib/microslop_one_drive.rb +33 -8
  30. metadata +25 -67
  31. data/lib/microslop_one_drive/batch_response.rb +0 -13
  32. data/lib/microslop_one_drive/drive_item_list.rb +0 -32
  33. data/lib/microslop_one_drive/drive_list.rb +0 -13
  34. data/lib/microslop_one_drive/list_response.rb +0 -21
  35. data/lib/microslop_one_drive/permission_list.rb +0 -29
  36. data/lib/microslop_one_drive/response.rb +0 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8dad115ceda6d13e87dfa8ce0e5d9b1d9e451a5000da6fa4feea51a66cf0a5f
4
- data.tar.gz: 32162a0c944a59cfcdf3a80497d599e899d995fde9286555f11b4d09223b8cfc
3
+ metadata.gz: 62f6bca1662f2c07a1b60c1c4913cb6bfd38bf91ba1aa5b1967c6fd6d934449e
4
+ data.tar.gz: 4c2c0331fb82cffaee9e577e199c9080d65e21b0bab21c4bb0e453b6bf3f96fb
5
5
  SHA512:
6
- metadata.gz: fe6c984e15650dc7a03c4299674122518bb55dabe9aac1943d03d0792003a0580a231a121cb7a93dd2dadaafd587992f2a747599d0eb1faf03f79bc272661db1
7
- data.tar.gz: 5e3d86ad896bd39c942d014e2b33cc58a0b80db859b36c856049396dee10d99b3abff74af702a5a9abe1d05376746bf0c85403be7853d80f10c05c0d88f07fca
6
+ metadata.gz: e94c8e9605382067d47f07795f6804b77e57a12e2e40a42a83f51451732acbe188377caa7e3c25dc1de4340a6ff32d5418282afeafd28946a6737224fda78cad
7
+ data.tar.gz: b244b1834e892720324401b7e6f688b4791bb7c406acd76fa8d2c326d8c7b53667aa714f62ec6fc09f7829a513782db27c74a85d773b1a0db915b7450cd5bb23
data/README.md CHANGED
@@ -16,8 +16,7 @@ gem "microslop_one_drive"
16
16
 
17
17
  ## Usage
18
18
 
19
- Here's a quickstart showing listing drives, getting some files in a drive, and getting permissions for some files in
20
- a batched manner:
19
+ Quickstart: current user, drives, drive items, shared-with-me, delta (changes), and permissions (single or batched).
21
20
 
22
21
  ### Creating a client
23
22
 
@@ -25,6 +24,17 @@ a batched manner:
25
24
  access_token = "..." # Get an access token via OAuth 2.0
26
25
 
27
26
  client = MicroslopOneDrive::Client.new(access_token)
27
+
28
+ # Optional: pass a logger to log all API requests and responses (e.g. Rails.logger)
29
+ client = MicroslopOneDrive::Client.new(access_token, logger: Rails.logger)
30
+ ```
31
+
32
+ ### Get current user
33
+
34
+ ```rb
35
+ user = client.me
36
+ user.display_name # => "Jane Doe"
37
+ user.email_address # => "jane@example.com"
28
38
  ```
29
39
 
30
40
  ### Listing Drives
@@ -34,7 +44,7 @@ Note: Microsoft will return all Drives a user has access to. Some of these seem
34
44
  `drive_exists?()` method to check if it's a real drive you can interact with.
35
45
 
36
46
  ```rb
37
- drive_list = client.drives
47
+ drive_list = client.all_drives
38
48
  drive_list.drives.size # => 2
39
49
 
40
50
  drive = drive_list.drives[1]
@@ -44,6 +54,29 @@ drive.id # => "0f0**********42"
44
54
  client.drive_exists?(drive.id) # => true (it's a real Drive)
45
55
  ```
46
56
 
57
+ ### Get a single drive
58
+
59
+ ```rb
60
+ # Default (current user's) drive
61
+ drive = client.drive
62
+
63
+ # Or a specific drive by ID
64
+ drive = client.drive(drive_id: "0f0**********42")
65
+ drive.name # => "OneDrive"
66
+ ```
67
+
68
+ ### Get a single drive item
69
+
70
+ ```rb
71
+ item = client.drive_item(item_id: "01ABC123...", drive_id: drive.id)
72
+ item.name # => "My Document.docx"
73
+ item.file? # => true
74
+ item.folder? # => false
75
+
76
+ # Check if an item exists without raising
77
+ client.drive_item_exists?(item_id: "01ABC123...", drive_id: drive.id) # => true
78
+ ```
79
+
47
80
  ### Listing Drive Items (Folders and Files)
48
81
 
49
82
  ```rb
@@ -59,6 +92,34 @@ delta_token = page2.delta_token # Save this somewhere and use as "token" in the
59
92
  # only get new changes, and don't list the whole drive from the beginning again.
60
93
  ```
61
94
 
95
+ ### Shared with me
96
+
97
+ Get drive items (files and folders) that others have shared with the current user.
98
+
99
+ ```rb
100
+ shared_list = client.shared_with_me
101
+ shared_list.shared_with_me_items.size # => 5
102
+
103
+ item = shared_list.shared_with_me_items.first
104
+ item.name # => "Spreadsheet One.xlsx"
105
+ item.id # => "64E5DD3210FD6004!s1d8ad87a1d6e4d4e..."
106
+ item.web_url # => "https://onedrive.live.com?cid=..."
107
+ item.size # => 6183
108
+ item.created_at # => 2026-02-17 14:27:26 UTC
109
+ item.updated_at # => 2026-02-17 14:27:27 UTC
110
+
111
+ # Who shared it
112
+ item.created_by.display_name # => "Someone Else"
113
+ item.created_by.email_address # => "outlook_64E5DD3210FD6004@outlook.com"
114
+ item.last_modified_by.display_name # => "Someone Else"
115
+
116
+ # The underlying drive item (with file?, folder?, mime_type, shared?, etc.)
117
+ item.remote_item.file? # => true
118
+ item.remote_item.shared? # => true
119
+ ```
120
+
121
+ Use `shared_list.next_page?` and `shared_list.next_token` to paginate when there are many items.
122
+
62
123
  ### Get Permissions for a single Drive Item
63
124
 
64
125
  ```rb
@@ -86,10 +147,9 @@ Microsoft Graph API [batch](https://learn.microsoft.com/en-us/graph/json-batchin
86
147
  drive_item_list = client.delta(drive_id: drive.id)
87
148
  shared_items = drive_item_list.items.select(&:shared?)
88
149
 
89
- permission_batch = client.batch_permissions(item_ids: shared_items.map(&:id))
90
-
91
- # Under the hood, this will batch the shared_items into batches of 20 (the max Microsoft allows on their batch endpoint)
92
- # and returns an aggregated result.
150
+ permissions = client.batch_permissions(item_ids: shared_items.map(&:id))
151
+ # Returns a flat array of MicroslopOneDrive::Permission (batches of 20 are made under the hood).
152
+ permissions.each { |p| puts "#{p.audience.display_name}: #{p.role}" }
93
153
  ```
94
154
 
95
155
  ## Contributing
@@ -0,0 +1,15 @@
1
+ module MicroslopOneDrive
2
+ module Batch
3
+ class BatchResponse
4
+ attr_reader :responses
5
+
6
+ def initialize
7
+ @responses = []
8
+ end
9
+
10
+ def add_response(response)
11
+ @responses << response
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ module MicroslopOneDrive
2
+ module Batch
3
+ class Response
4
+ attr_reader :id, :status, :headers, :body
5
+
6
+ def initialize(response_hash)
7
+ @id = response_hash.fetch("id")
8
+ @status = response_hash.fetch("status")
9
+ @headers = response_hash.fetch("headers")
10
+ @body = response_hash.fetch("body")
11
+ end
12
+
13
+ def success?
14
+ @status.between?(200, 299)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -3,7 +3,7 @@ require "json"
3
3
  module MicroslopOneDrive
4
4
  class Client
5
5
  BASE_URL = "https://graph.microsoft.com/v1.0".freeze
6
- BATCH_REQUEST_LIMIT = 20.freeze
6
+ BATCH_REQUEST_LIMIT = 20 # This is set by Microsoft
7
7
 
8
8
  # @param access_token [String] OAuth access token for Microsoft Graph.
9
9
  # @param logger [Object, nil] Optional logger (e.g. Rails.logger) that responds to +#info+, +#debug+, +#warn+, +#error+.
@@ -25,30 +25,41 @@ module MicroslopOneDrive
25
25
  def me
26
26
  response = get(path: "me", query: {})
27
27
  handle_error(response) unless response.success?
28
- MicroslopOneDrive::User.new(response.parsed_response)
28
+ MicroslopOneDrive::Deserializers::UserDeserializer.create_from_hash(response.parsed_response)
29
29
  end
30
30
 
31
- # Gets all Drives the current user has access to.
31
+ # Gets a Drive.
32
32
  #
33
- # NOTE: This will include some internal Microsoft drives that aren't real drives, such as AI, Face scans, and other
34
- # shitty things.
33
+ # If no drive_id is provided, the current User's default Drive will be returned, else, the specific Drive identified
34
+ # by the drive_id will be returned.
35
35
  #
36
- # @return [MicroslopOneDrive::DriveList]
37
- def drives
38
- response = get(path: "me/drives", query: {})
36
+ # From the docs:
37
+ #
38
+ # > Most users will only have a single Drive resource.
39
+ # > Groups and Sites may have multiple Drive resources available.
40
+ #
41
+ # @param drive_id [String, nil] The ID of the Drive to get. If not provided, the current User's default Drive will
42
+ # be returned.
43
+ #
44
+ # @return [MicroslopOneDrive::Drive]
45
+ def drive(drive_id: nil)
46
+ url = drive_id.nil? ? "me/drive" : "me/drives/#{drive_id}"
47
+ response = get(path: url, query: {})
48
+
39
49
  handle_error(response) unless response.success?
40
- MicroslopOneDrive::DriveList.new(response.parsed_response)
50
+ MicroslopOneDrive::Deserializers::DriveDeserializer.create_from_hash(response.parsed_response)
41
51
  end
42
52
 
43
- # Gets a specific Drive by its ID.
53
+ # Gets ALL Drives the current user has access to.
44
54
  #
45
- # @param drive_id [String] The ID of the Drive to get.
55
+ # NOTE: This will include some internal Microsoft drives that aren't real drives, such as AI, Face scans, and other
56
+ # shitty things.
46
57
  #
47
- # @return [MicroslopOneDrive::Drive]
48
- def drive(drive_id:)
49
- response = get(path: "me/drives/#{drive_id}", query: {})
58
+ # @return [MicroslopOneDrive::DriveList]
59
+ def all_drives
60
+ response = get(path: "me/drives", query: {})
50
61
  handle_error(response) unless response.success?
51
- MicroslopOneDrive::Drive.new(response.parsed_response)
62
+ MicroslopOneDrive::ListResponses::DriveList.new(response.parsed_response)
52
63
  end
53
64
 
54
65
  # Asks if a Drive exists by its ID.
@@ -61,24 +72,31 @@ module MicroslopOneDrive
61
72
  response.success?
62
73
  end
63
74
 
64
- # Gets a specific DriveItem (folder or file) by its ID.
75
+ # Gets a specific DriveItem (folder or file)
65
76
  #
77
+ # @param drive_id [String, nil] The ID of the Drive to get the Drive Item from. If not provided, the current User's
78
+ # default Drive will be used.
66
79
  # @param item_id [String] The ID of the Drive Item to get.
67
80
  #
68
81
  # @return [MicroslopOneDrive::DriveItem]
69
- def drive_item(item_id:)
70
- response = get(path: "me/drive/items/#{item_id}", query: {})
82
+ def drive_item(item_id:, drive_id: nil)
83
+ url = drive_id.nil? ? "me/drive/items/#{item_id}" : "me/drives/#{drive_id}/items/#{item_id}"
84
+ response = get(path: url, query: {})
85
+
71
86
  handle_error(response) unless response.success?
72
- MicroslopOneDrive::DriveItem.new(response.parsed_response)
87
+ MicroslopOneDrive::Deserializers::DriveItemDeserializer.create_from_hash(response.parsed_response)
73
88
  end
74
89
 
75
90
  # Asks if a DriveItem (folder or file) exists by its ID.
76
91
  #
92
+ # @param drive_id [String, nil] The ID of the Drive to check the Drive Item in. If not provided, the current User's
93
+ # default Drive will be used.
77
94
  # @param item_id [String] The ID of the Drive Item to check.
78
95
  #
79
96
  # @return [Boolean]
80
- def item_exists?(item_id:)
81
- response = get(path: "me/drive/items/#{item_id}", query: {})
97
+ def drive_item_exists?(item_id:, drive_id: nil)
98
+ url = drive_id.nil? ? "me/drive/items/#{item_id}" : "me/drives/#{drive_id}/items/#{item_id}"
99
+ response = get(path: url, query: {})
82
100
 
83
101
  return false if response.code == 404
84
102
  return true if response.success?
@@ -86,33 +104,58 @@ module MicroslopOneDrive
86
104
  handle_error(response)
87
105
  end
88
106
 
89
- # Gets a delta of changes to a Drive.
107
+ # Gets a delta of changes in a Drive.
90
108
  #
91
- # @param drive_id [String] The ID of the Drive to get the delta of.
92
- # @param token [String] The token to use for the delta. If not provided, the initial delta will be returned.
109
+ # @param drive_id [String, nil] The ID of the Drive to get the delta of. If not provided, the current User's default
110
+ # Drive will be used.
111
+ # @param token [String, nil] The token to use for the delta. If not provided, the initial delta will be returned.
93
112
  #
94
113
  # @return [MicroslopOneDrive::DriveItemList]
95
- def delta(drive_id:, token: nil)
96
- response = get(path: "me/drives/#{drive_id}/root/delta", query: {token: token})
114
+ def delta(drive_id: nil, token: nil)
115
+ url = drive_id.nil? ? "me/drive/root/delta" : "me/drives/#{drive_id}/root/delta"
116
+ response = get(path: url, query: {token: token})
117
+ handle_error(response) unless response.success?
118
+ MicroslopOneDrive::ListResponses::DriveItemList.new(response.parsed_response)
119
+ end
120
+
121
+ # Gets the Drive Items shared with the current user.
122
+ #
123
+ # @return [MicroslopOneDrive::SharedWithMeList]
124
+ def shared_with_me
125
+ response = get(path: "me/drive/sharedWithMe")
97
126
  handle_error(response) unless response.success?
98
- MicroslopOneDrive::DriveItemList.new(response.parsed_response)
127
+ MicroslopOneDrive::ListResponses::SharedWithMeList.new(response.parsed_response)
99
128
  end
100
129
 
101
- # Gets the permissions for a DriveItem (folder or file).
130
+ # Gets the permissions for a DriveItem (folder or file) in a Drive.
102
131
  #
132
+ # @param drive_id [String, nil] The ID of the Drive to get the permissions of. If not provided, the current User's
133
+ # default Drive will be used.
103
134
  # @param item_id [String] The ID of the Drive Item to get the permissions of.
104
135
  #
105
136
  # @return [MicroslopOneDrive::PermissionList]
106
- def permissions(item_id:)
107
- response = get(path: "me/drive/items/#{item_id}/permissions", query: {})
137
+ def permissions(item_id:, drive_id: nil)
138
+ url = if drive_id.nil?
139
+ "me/drive/items/#{item_id}/permissions"
140
+ else
141
+ "me/drives/#{drive_id}/items/#{item_id}/permissions"
142
+ end
143
+
144
+ response = get(path: url, query: {})
108
145
 
109
146
  if response.code == 404
110
- return MicroslopOneDrive::PermissionList.new(drive_item_id: item_id, parsed_response: { "value" => [] })
147
+ return MicroslopOneDrive::ListResponses::PermissionList.new(
148
+ drive_item_id: item_id,
149
+ parsed_response: {"value" => []}
150
+ )
111
151
  end
112
152
 
113
153
  handle_error(response) unless response.success?
114
154
 
115
- MicroslopOneDrive::PermissionList.new(drive_item_id: item_id, parsed_response: response.parsed_response)
155
+ MicroslopOneDrive::ListResponses::PermissionList.new(
156
+ drive_item_id: item_id,
157
+ parsed_response: response.parsed_response
158
+ )
116
159
  end
117
160
 
118
161
  # Gets the permissions for multiple Drive Items.
@@ -122,16 +165,21 @@ module MicroslopOneDrive
122
165
  #
123
166
  # See: https://learn.microsoft.com/en-us/graph/json-batching
124
167
  #
168
+ # @param drive_id [String, nil] The ID of the Drive to get the permissions of. If not provided, the current User's
169
+ # default Drive will be used.
125
170
  # @param item_ids [Array<String>] The IDs of the Drive Items to get the permissions of.
126
171
  #
127
172
  # @return [Array<MicroslopOneDrive::Permission>]
128
- def batch_permissions(item_ids:)
129
- requests = item_ids.map { |item_id| { id: item_id, method: "GET", url: "/me/drive/items/#{item_id}/permissions" } }
173
+ def batch_permissions(item_ids:, drive_id: nil)
174
+ requests = build_batch_permissions_requests(item_ids: item_ids, drive_id: drive_id)
130
175
  batch_response = batch(requests: requests)
131
176
  successful_responses = batch_response.responses.select(&:success?)
132
177
 
133
- permission_lists = successful_responses.map do |response|
134
- MicroslopOneDrive::PermissionList.new(drive_item_id: response.id, parsed_response: response.body)
178
+ permission_lists = successful_responses.map do
179
+ MicroslopOneDrive::ListResponses::PermissionList.new(
180
+ drive_item_id: it.id,
181
+ parsed_response: it.body
182
+ )
135
183
  end
136
184
 
137
185
  permission_lists.flat_map(&:permissions)
@@ -149,18 +197,18 @@ module MicroslopOneDrive
149
197
  #
150
198
  # @return [MicroslopOneDrive::BatchResponse]
151
199
  def batch(requests:)
152
- batch_response = MicroslopOneDrive::BatchResponse.new
200
+ batch_response = MicroslopOneDrive::Batch::BatchResponse.new
153
201
 
154
202
  # No requests, so simply return an empty batch response:
155
203
  return batch_response if requests.empty?
156
204
 
157
205
  batches = requests.each_slice(BATCH_REQUEST_LIMIT).to_a
158
- batches.each do |batch|
159
- response = post(path: "$batch", body: { requests: batch }.to_json)
206
+ batches.each do
207
+ response = post(path: "$batch", body: {requests: it}.to_json)
160
208
  handle_error(response) unless response.success?
161
209
  new_responses = response.parsed_response.fetch("responses", [])
162
- new_responses.each do |new_response_hash|
163
- batch_response.add_response(MicroslopOneDrive::Response.new(new_response_hash))
210
+ new_responses.each do
211
+ batch_response.add_response(MicroslopOneDrive::Batch::Response.new(it))
164
212
  end
165
213
  end
166
214
 
@@ -169,6 +217,14 @@ module MicroslopOneDrive
169
217
 
170
218
  private
171
219
 
220
+ def build_batch_permissions_requests(item_ids:, drive_id: nil)
221
+ if drive_id.nil?
222
+ item_ids.map { {id: it, method: "GET", url: "/me/drive/items/#{it}/permissions"} }
223
+ else
224
+ item_ids.map { {id: it, method: "GET", url: "/me/drives/#{drive_id}/items/#{it}/permissions"} }
225
+ end
226
+ end
227
+
172
228
  def get(path:, query: {})
173
229
  url = "#{BASE_URL}/#{path}"
174
230
 
@@ -200,7 +256,7 @@ module MicroslopOneDrive
200
256
  @logger.info "==================== START MicroslopOneDrive #{method} #{url} ===================="
201
257
  @logger.info "Request method: #{method}"
202
258
  @logger.info "Request url: #{url}"
203
- @logger.info "Request query: #{query.inspect}" if query && query.any?
259
+ @logger.info "Request query: #{query.inspect}" if query&.any?
204
260
  @logger.info "Request body: #{body.inspect}" if body
205
261
  end
206
262
 
@@ -0,0 +1,57 @@
1
+ require "microslop_one_drive/utils"
2
+
3
+ module MicroslopOneDrive
4
+ module Deserializers
5
+ class DriveDeserializer
6
+ # Creates a new Drive object from a hash.
7
+ #
8
+ # @param drive_hash [Hash] The hash to create the Drive object from.
9
+ # @return [MicroslopOneDrive::Drive] The created Drive object.
10
+ def self.create_from_hash(drive_hash)
11
+ drive_hash = Utils.deep_symbolize_keys(drive_hash)
12
+
13
+ created_by = build_created_by(drive_hash)
14
+ last_modified_by = build_last_modified_by(drive_hash)
15
+ owner = build_owner(drive_hash)
16
+
17
+ Drive.new(
18
+ id: drive_hash.fetch(:id, nil),
19
+ name: drive_hash.fetch(:name, nil),
20
+ description: drive_hash.fetch(:description, nil),
21
+ web_url: drive_hash.fetch(:webUrl, nil),
22
+ drive_type: drive_hash.fetch(:driveType, nil),
23
+ created_date_time: Utils.safe_parse_time(drive_hash.fetch(:createdDateTime, nil)),
24
+ last_modified_date_time: Utils.safe_parse_time(drive_hash.fetch(:lastModifiedDateTime, nil)),
25
+ created_by: created_by,
26
+ last_modified_by: last_modified_by,
27
+ owner: owner,
28
+ quota: QuotaDeserializer.create_from_hash(drive_hash.fetch(:quota, {}))
29
+ )
30
+ end
31
+
32
+ def self.build_created_by(drive_hash)
33
+ created_by_hash = drive_hash.dig(:createdBy, :user)
34
+
35
+ return unless created_by_hash
36
+
37
+ UserDeserializer.create_from_hash(created_by_hash)
38
+ end
39
+
40
+ def self.build_last_modified_by(drive_hash)
41
+ last_modified_by_hash = drive_hash.dig(:lastModifiedBy, :user)
42
+
43
+ return unless last_modified_by_hash
44
+
45
+ UserDeserializer.create_from_hash(last_modified_by_hash)
46
+ end
47
+
48
+ def self.build_owner(drive_hash)
49
+ owner_hash = drive_hash.dig(:owner, :user)
50
+
51
+ return unless owner_hash
52
+
53
+ UserDeserializer.create_from_hash(owner_hash)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,67 @@
1
+ require "microslop_one_drive/utils"
2
+
3
+ module MicroslopOneDrive
4
+ module Deserializers
5
+ class DriveItemDeserializer
6
+ # Creates a new DriveItem object from a hash.
7
+ #
8
+ # @param drive_item_hash [Hash] The hash to create the DriveItem object from.
9
+ # @return [MicroslopOneDrive::DriveItem] The created DriveItem object.
10
+ def self.create_from_hash(drive_item_hash)
11
+ drive_item_hash = Utils.deep_symbolize_keys(drive_item_hash)
12
+
13
+ parameters = build_parameters(drive_item_hash)
14
+
15
+ root = drive_item_hash.key?(:root) && parameters[:file_or_folder] == :folder
16
+
17
+ if root
18
+ RootFolder.new(**parameters)
19
+ elsif parameters[:file_or_folder] == :file
20
+ File.new(**parameters)
21
+ elsif parameters[:file_or_folder] == :folder
22
+ Folder.new(**parameters)
23
+ else
24
+ DriveItem.new(**parameters)
25
+ end
26
+ end
27
+
28
+ def self.build_parameters(drive_item_hash)
29
+ file_or_folder = drive_item_hash.key?(:file) ? :file : :folder
30
+ mime_type = build_mime_type(file_or_folder, drive_item_hash)
31
+ parent_reference = build_parent_reference(drive_item_hash)
32
+
33
+ {
34
+ id: drive_item_hash.fetch(:id, nil),
35
+ name: drive_item_hash.fetch(:name, nil),
36
+ download_url: drive_item_hash.fetch(:"@microsoft.graph.downloadUrl", nil),
37
+ web_url: drive_item_hash.fetch(:webUrl, nil),
38
+ size: drive_item_hash.fetch(:size, nil),
39
+ created_date_time: Utils.safe_parse_time(drive_item_hash.fetch(:createdDateTime, nil)),
40
+ last_modified_date_time: Utils.safe_parse_time(drive_item_hash.fetch(:lastModifiedDateTime, nil)),
41
+ e_tag: drive_item_hash.fetch(:eTag, nil),
42
+ c_tag: drive_item_hash.fetch(:cTag, nil),
43
+ file_or_folder: file_or_folder,
44
+ mime_type: mime_type,
45
+ is_deleted: drive_item_hash.dig(:deleted, :state) == "deleted",
46
+ is_shared: drive_item_hash.key?(:shared),
47
+ parent_reference: parent_reference
48
+ }
49
+ end
50
+
51
+ def self.build_mime_type(file_or_folder, drive_item_hash)
52
+ if file_or_folder == :file
53
+ drive_item_hash.dig(:file, :mimeType)
54
+ else
55
+ MicroslopOneDrive::DriveItem::DIRECTORY_MIME_TYPE
56
+ end
57
+ end
58
+
59
+ def self.build_parent_reference(drive_item_hash)
60
+ parent_reference_hash = drive_item_hash.fetch(:parentReference, nil)
61
+ return unless parent_reference_hash
62
+
63
+ ParentReferenceDeserializer.create_from_hash(parent_reference_hash)
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,20 @@
1
+ require "microslop_one_drive/utils"
2
+
3
+ module MicroslopOneDrive
4
+ module Deserializers
5
+ class ParentReferenceDeserializer
6
+ def self.create_from_hash(parent_reference_hash)
7
+ parent_reference_hash = Utils.deep_symbolize_keys(parent_reference_hash)
8
+
9
+ ParentReference.new(
10
+ drive_type: parent_reference_hash.fetch(:driveType, nil),
11
+ drive_id: parent_reference_hash.fetch(:driveId, nil),
12
+ id: parent_reference_hash.fetch(:id, nil),
13
+ name: parent_reference_hash.fetch(:name, nil),
14
+ path: parent_reference_hash.fetch(:path, nil),
15
+ site_id: parent_reference_hash.fetch(:siteId, nil)
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ require "microslop_one_drive/utils"
2
+
3
+ module MicroslopOneDrive
4
+ module Deserializers
5
+ class QuotaDeserializer
6
+ # Creates a new Quota object from a hash.
7
+ #
8
+ # @param quota_hash [Hash] The hash to create the Quota object from.
9
+ # @return [MicroslopOneDrive::Quota] The created Quota object.
10
+ def self.create_from_hash(quota_hash)
11
+ quota_hash = Utils.deep_symbolize_keys(quota_hash)
12
+
13
+ Quota.new(
14
+ deleted: quota_hash.fetch(:deleted, nil),
15
+ remaining: quota_hash.fetch(:remaining, nil),
16
+ state: quota_hash.fetch(:state, nil),
17
+ total: quota_hash.fetch(:total, nil),
18
+ used: quota_hash.fetch(:used, nil),
19
+ upgrade_available: quota_hash.dig(:storagePlanInformation, :upgradeAvailable)
20
+ )
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,48 @@
1
+ require "microslop_one_drive/utils"
2
+
3
+ module MicroslopOneDrive
4
+ module Deserializers
5
+ class SharedWithMeItemDeserializer
6
+ def self.create_from_hash(shared_with_me_item_hash)
7
+ shared_with_me_item_hash = Utils.deep_symbolize_keys(shared_with_me_item_hash)
8
+
9
+ created_by = build_created_by(shared_with_me_item_hash)
10
+ last_modified_by = build_last_modified_by(shared_with_me_item_hash)
11
+ remote_item = build_remote_item(shared_with_me_item_hash)
12
+
13
+ SharedWithMeItem.new(
14
+ id: shared_with_me_item_hash.fetch(:id, nil),
15
+ name: shared_with_me_item_hash.fetch(:name, nil),
16
+ web_url: shared_with_me_item_hash.fetch(:webUrl, nil),
17
+ size: shared_with_me_item_hash.fetch(:size, nil),
18
+ last_modified_date_time: Utils.safe_parse_time(shared_with_me_item_hash.fetch(:lastModifiedDateTime, nil)),
19
+ created_date_time: Utils.safe_parse_time(shared_with_me_item_hash.fetch(:createdDateTime, nil)),
20
+ created_by: created_by,
21
+ last_modified_by: last_modified_by,
22
+ remote_item: remote_item
23
+ )
24
+ end
25
+
26
+ def self.build_created_by(shared_with_me_item_hash)
27
+ created_by_hash = shared_with_me_item_hash.dig(:createdBy, :user)
28
+ return unless created_by_hash
29
+
30
+ UserDeserializer.create_from_hash(created_by_hash)
31
+ end
32
+
33
+ def self.build_last_modified_by(shared_with_me_item_hash)
34
+ last_modified_by_hash = shared_with_me_item_hash.dig(:lastModifiedBy, :user)
35
+ return unless last_modified_by_hash
36
+
37
+ UserDeserializer.create_from_hash(last_modified_by_hash)
38
+ end
39
+
40
+ def self.build_remote_item(shared_with_me_item_hash)
41
+ remote_item_hash = shared_with_me_item_hash.fetch(:remoteItem, {})
42
+ return unless remote_item_hash
43
+
44
+ DriveItemDeserializer.create_from_hash(remote_item_hash)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,29 @@
1
+ require "microslop_one_drive/utils"
2
+
3
+ module MicroslopOneDrive
4
+ module Deserializers
5
+ class UserDeserializer
6
+ # Creates a new User object from a hash.
7
+ #
8
+ # @param user_hash [Hash] The hash to create the User object from.
9
+ # @return [MicroslopOneDrive::User] The created User object.
10
+ def self.create_from_hash(user_hash)
11
+ user_hash = Utils.deep_symbolize_keys(user_hash)
12
+
13
+ User.new(
14
+ id: user_hash.fetch(:id, nil),
15
+ principal_name: user_hash.fetch(:userPrincipalName, nil),
16
+ display_name: user_hash.fetch(:displayName, nil),
17
+ surname: user_hash.fetch(:surname, nil),
18
+ given_name: user_hash.fetch(:givenName, nil),
19
+ preferred_language: user_hash.fetch(:preferredLanguage, nil),
20
+ mail: user_hash.fetch(:mail, nil) || user_hash.fetch(:email, nil),
21
+ mobile_phone: user_hash.fetch(:mobilePhone, nil),
22
+ job_title: user_hash.fetch(:jobTitle, nil),
23
+ office_location: user_hash.fetch(:officeLocation, nil),
24
+ business_phones: user_hash.fetch(:businessPhones, [])
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end