From 753e7d129289490a2a52d1d21ab63f0c363c2d76 Mon Sep 17 00:00:00 2001 From: Danilo Grieco Date: Tue, 2 Jul 2024 18:54:52 +0200 Subject: [PATCH 1/4] Handle also path in the response for a get --- lib/sharepoint/client.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/sharepoint/client.rb b/lib/sharepoint/client.rb index 207d015..1616d0e 100644 --- a/lib/sharepoint/client.rb +++ b/lib/sharepoint/client.rb @@ -280,12 +280,13 @@ def list_documents_page(url) # - `:link_url` [String] if the requested file is a link, this returns the destination file url def download(file_path: nil, site_path: nil, link_credentials: {}) meta = get_document(file_path, site_path) - if meta.url.nil? + meta_path = meta.url || meta.path + if meta_path url = computed_web_api_url(site_path) server_relative_url = odata_escape_single_quote "#{site_path}#{file_path}" download_url "#{url}GetFileByServerRelativeUrl('#{server_relative_url}')/$value" else # requested file is a link - paths = extract_paths(meta.url) + paths = extract_paths(meta_path) link_config = { uri: paths[:root] } if link_credentials.empty? link_config = config.to_h.merge(link_config) @@ -293,7 +294,7 @@ def download(file_path: nil, site_path: nil, link_credentials: {}) link_config.merge!(link_credentials) end link_client = self.class.new(link_config) - result = link_client.download_url meta.url + result = link_client.download_url meta_path result[:link_url] = meta.url if result[:link_url].nil? result end From c98ae3dd9c463fc5c50b18b0ac6859958404b88b Mon Sep 17 00:00:00 2001 From: Danilo Grieco Date: Tue, 2 Jul 2024 19:14:40 +0200 Subject: [PATCH 2/4] Fixed check --- lib/sharepoint/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sharepoint/client.rb b/lib/sharepoint/client.rb index 1616d0e..3ec2c6a 100644 --- a/lib/sharepoint/client.rb +++ b/lib/sharepoint/client.rb @@ -281,7 +281,7 @@ def list_documents_page(url) def download(file_path: nil, site_path: nil, link_credentials: {}) meta = get_document(file_path, site_path) meta_path = meta.url || meta.path - if meta_path + if meta_path.nil? url = computed_web_api_url(site_path) server_relative_url = odata_escape_single_quote "#{site_path}#{file_path}" download_url "#{url}GetFileByServerRelativeUrl('#{server_relative_url}')/$value" From c0d988683864b0819a090fc8b9d47c8d7f745a63 Mon Sep 17 00:00:00 2001 From: Danilo Grieco Date: Tue, 2 Jul 2024 19:34:44 +0200 Subject: [PATCH 3/4] Added specs on path --- .../responses/get_document_having_path.json | 83 +++++++++++++++++++ spec/lib/sharepoint/client_methods_spec.rb | 35 ++++++-- 2 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 spec/fixtures/responses/get_document_having_path.json diff --git a/spec/fixtures/responses/get_document_having_path.json b/spec/fixtures/responses/get_document_having_path.json new file mode 100644 index 0000000..115fe65 --- /dev/null +++ b/spec/fixtures/responses/get_document_having_path.json @@ -0,0 +1,83 @@ +{ + "d": { + "__metadata": { + "id": "Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)", + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)", + "etag": "\"4\"", + "type": "SP.Data.DocumentsItem" + }, + "FirstUniqueAncestorSecurableObject": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FirstUniqueAncestorSecurableObject" + } + }, + "RoleAssignments": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/RoleAssignments" + } + }, + "AttachmentFiles": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/AttachmentFiles" + } + }, + "ContentType": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/ContentType" + } + }, + "FieldValuesAsHtml": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FieldValuesAsHtml" + } + }, + "FieldValuesAsText": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FieldValuesAsText" + } + }, + "FieldValuesForEdit": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FieldValuesForEdit" + } + }, + "File": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/File" + } + }, + "Folder": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/Folder" + } + }, + "ParentList": { + "__deferred": { + "uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/ParentList" + } + }, + "FileSystemObjectType": 0, + "Id": 6, + "ContentTypeId": "0x0101002E97C997BC5CAC4785AA3F57D34080E3", + "Title": "Bari-Rome boarding-pass", + "OData__dlc_DocId": "QHWH36P2KKT2-1-6", + "OData__dlc_DocIdUrl": { + "__metadata": { + "type": "SP.FieldUrlValue" + }, + "Description": "QHWH36P2KKT2-1-6", + "Path": "http://win-jamsdpuujgc/_layouts/15/DocIdRedir.aspx?ID=QHWH36P2KKT2-1-6" + }, + "URL": null, + "DocumentSetDescription": null, + "EditorId": 1, + "ID": 6, + "Created": "2016-07-17T08:26:35", + "AuthorId": 1, + "Modified": "2016-07-25T05:45:18", + "OData__CopySource": null, + "CheckoutUserId": null, + "OData__UIVersionString": "0.2", + "GUID": "e35ac090-3f54-46aa-8ffd-fb14a0aa1610" + } +} diff --git a/spec/lib/sharepoint/client_methods_spec.rb b/spec/lib/sharepoint/client_methods_spec.rb index 451b156..14a7200 100644 --- a/spec/lib/sharepoint/client_methods_spec.rb +++ b/spec/lib/sharepoint/client_methods_spec.rb @@ -282,20 +282,37 @@ describe '#download' do subject(:download) { client.download file_path: file_path } - let(:document_json) { File.read('spec/fixtures/responses/get_document.json') } - let(:document_meta) { client.send :parse_get_document_response, document_json, [] } let(:file_path) { '/Documents/document.docx' } let(:expected_content) { File.read('spec/fixtures/responses/document.docx') } + let(:document_meta) { client.send :parse_get_document_response, document_json, [] } - before do - allow(client).to receive(:get_document).and_return(document_meta) - mock_responses('document.docx') + context "when meta contains URL" do + let(:document_json) { File.read('spec/fixtures/responses/get_document.json') } + + before do + allow(client).to receive(:get_document).and_return(document_meta) + mock_responses('document.docx') + end + + it 'returns expected hash' do + expect(download).to have_key :file_contents + expect(download).to have_key :link_url + expect(download[:file_contents]).to eq expected_content + end end - it 'returns expected hash' do - expect(download).to have_key :file_contents - expect(download).to have_key :link_url - expect(download[:file_contents]).to eq expected_content + context "when meta contains Path" do + let(:document_json) { File.read('spec/fixtures/responses/get_document_having_path.json') } + before do + allow(client).to receive(:get_document).and_return(document_meta) + mock_responses('document.docx') + end + + it 'returns expected hash' do + expect(download).to have_key :file_contents + expect(download).to have_key :link_url + expect(download[:file_contents]).to eq expected_content + end end end From ed4fa7d841d3d1daba83f7d017fd77016284fb39 Mon Sep 17 00:00:00 2001 From: Danilo Grieco Date: Wed, 3 Jul 2024 14:50:25 +0200 Subject: [PATCH 4/4] Rubocop --- spec/lib/sharepoint/client_methods_spec.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/lib/sharepoint/client_methods_spec.rb b/spec/lib/sharepoint/client_methods_spec.rb index 14a7200..12f4a9d 100644 --- a/spec/lib/sharepoint/client_methods_spec.rb +++ b/spec/lib/sharepoint/client_methods_spec.rb @@ -286,7 +286,7 @@ let(:expected_content) { File.read('spec/fixtures/responses/document.docx') } let(:document_meta) { client.send :parse_get_document_response, document_json, [] } - context "when meta contains URL" do + context 'when meta contains URL' do let(:document_json) { File.read('spec/fixtures/responses/get_document.json') } before do @@ -301,8 +301,9 @@ end end - context "when meta contains Path" do + context 'when meta contains Path' do let(:document_json) { File.read('spec/fixtures/responses/get_document_having_path.json') } + before do allow(client).to receive(:get_document).and_return(document_meta) mock_responses('document.docx')