diff --git a/lib/sharepoint/client.rb b/lib/sharepoint/client.rb index 207d015..3ec2c6a 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.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" 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 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..12f4a9d 100644 --- a/spec/lib/sharepoint/client_methods_spec.rb +++ b/spec/lib/sharepoint/client_methods_spec.rb @@ -282,20 +282,38 @@ 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