Skip to content

Commit

Permalink
Implementing the BrowseEverything::Resource Class and extracting para…
Browse files Browse the repository at this point in the history
…meter parsing Controller functionality into BrowseEverything::Parameters
  • Loading branch information
jrgriffiniii committed Jul 16, 2019
1 parent 70119c9 commit 2cffe03
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 37 deletions.
25 changes: 2 additions & 23 deletions app/controllers/browse_everything_controller.rb
Expand Up @@ -5,6 +5,7 @@
class BrowseEverythingController < ActionController::Base
layout 'browse_everything'
helper BrowseEverythingHelper
include BrowseEverything::Parameters

protect_from_forgery with: :exception

Expand Down Expand Up @@ -97,15 +98,7 @@ def resolve
last_provider_key = provider_key
end

selected_links.each_index do |index|
payload[index] = selected_files[index]
end

payload[:browse_everything] = {
selected_files: selected_links,
selected_directories: selected_directories
}
payload[:browse_everything][:provider] = last_provider_key unless selected_links.empty?
payload = selected_links

respond_to do |format|
format.html { render layout: false }
Expand Down Expand Up @@ -189,20 +182,6 @@ def provider
@provider ||= build_provider
end

# Retrieve the file and directory entries selected using the POST request
# @return [Array<String>]
def browse_everything_params
return unless params[:browse_everything]

params[:browse_everything].fetch(:selected_files, []) + params[:browse_everything].fetch(:selected_directories, [])
end

# Retrieve the file entries selected using the legacy POST request parameter
# @return [Array<String>]
def selected_params
params[:selected_files]
end

helper_method :auth_link
helper_method :browser
helper_method :browse_path
Expand Down
48 changes: 48 additions & 0 deletions app/controllers/concerns/browse_everything/parameters.rb
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require File.expand_path('../../../../lib/browse_everything/resource', __dir__)

module BrowseEverything
module Parameters
extend ActiveSupport::Concern

included do
# Retrieve the file and directory entries selected using the POST request
# @return [Array<String>]
def browse_everything_params
return unless params[:browse_everything]

file_values = params[:browse_everything].fetch(:selected_files, [])
dir_values = params[:browse_everything].fetch(:selected_directories, [])
# Ensure that these are empty Arrays if ActionController::Parameters are
# empty
file_values = [] if file_values.empty?
dir_values = [] if dir_values.empty?

file_values + dir_values
end

# Retrieve the file entries selected using the legacy POST request parameter
# @return [Array<String>]
def selected_params
params[:selected_files]
end

# Retrieve the files selected from browse-everything
# @return [BrowseEverything::Resource]
def selected_files
return [] unless selected_params

values = selected_params.values.uniq
values.map { |value| BrowseEverything::Resource.new(value) }
end

# Determine whether or not cloud service files are being uploaded
# @return [Boolean]
def selected_cloud_files?
values = selected_files.map(&:cloud_file?)
values.reduce(:|)
end
end
end
end
1 change: 1 addition & 0 deletions lib/browse_everything.rb
Expand Up @@ -8,6 +8,7 @@
module BrowseEverything
autoload :Browser, 'browse_everything/browser'
autoload :FileEntry, 'browse_everything/file_entry'
autoload :Resource, 'browse_everything/resource'

module Driver
module Paginator
Expand Down
14 changes: 7 additions & 7 deletions lib/browse_everything/driver/box.rb
Expand Up @@ -37,13 +37,13 @@ def validate_config
# @param [String] id of the file or folder in Box
# @return [Array<RubyBox::File>]
def contents(id = '', _page_index = 0)
if id.empty?
folder = box_client.root_folder
@entries = []
else
folder = box_client.folder_by_id(id)
@entries = [parent_directory(folder)]
end
@entries = []

folder = if id.empty?
box_client.root_folder
else
box_client.folder_by_id(id)
end

folder.items(ITEM_LIMIT, 0, %w[name size created_at]).collect do |f|
values << directory_entry(f)
Expand Down
32 changes: 32 additions & 0 deletions lib/browse_everything/resource.rb
@@ -0,0 +1,32 @@
# frozen_string_literal: true

# Class modeling resources selected for upload using browse-everything
module BrowseEverything
class Resource < ActiveSupport::HashWithIndifferentAccess
# Retrieve the path for the resource
# @return [String]
def path
return if url.nil?

_provider_key, uri = url.split(/:\/\//)
uri
end

# Determine whether or not this file is a cloud resource
# @return [Boolean]
def cloud_file?
return false if url.nil?

m = /^https?\:/.match(url)
!m.nil?
end

private

# Retrieve the URL for the resource
# @return [String]
def url
fetch("url", nil)
end
end
end
7 changes: 4 additions & 3 deletions spec/controllers/browse_everything_controller_spec.rb
Expand Up @@ -141,9 +141,10 @@
json_response = JSON.parse(response.body)
expect(json_response).not_to be_empty
resolved = json_response.first
expect(resolved).to include 'url' => 'file:///my/test/file.txt'
expect(resolved).to include 'file_name' => 'file.txt'
expect(resolved).to include 'file_size' => 0
expect(resolved).to include "directory" => false
expect(resolved).to include "file_name" => "file.txt"
expect(resolved).to include "file_size" => 0
expect(resolved).to include "url" => "file:///my/test/file.txt"
end
end
end
Binary file added spec/fixtures/Getting Started.pdf
Binary file not shown.
6 changes: 5 additions & 1 deletion spec/lib/browse_everything/driver/file_system_spec.rb
Expand Up @@ -95,6 +95,10 @@
describe "#link_for('/path/to/file')" do
subject { provider.link_for('/path/to/file') }

it { is_expected.to eq(['file:///path/to/file', { file_name: 'file', file_size: 0 }]) }
it {
is_expected.to eq([
['file:///path/to/file', { directory: false, file_name: 'file', file_size: 0 }]
])
}
end
end
7 changes: 5 additions & 2 deletions spec/lib/browse_everything/driver/google_drive_spec.rb
Expand Up @@ -194,7 +194,7 @@
end

describe '#link_for' do
subject(:link) { driver.link_for('asset-id2') }
subject(:links) { driver.link_for('asset-id2') }
let(:file_response_body) do
'{
"id": "asset-id2",
Expand All @@ -205,7 +205,7 @@

before do
stub_request(
:get, "https://www.googleapis.com/drive/v3/files/asset-id2?fields=id,%20name,%20size"
:get, "https://www.googleapis.com/drive/v3/files/asset-id2?fields=id,%20name,%20size,%20mimeType"
).to_return(
body: file_response_body,
status: 200,
Expand All @@ -216,6 +216,9 @@
end

it 'generates the link for a Google Drive asset' do
expect(links).to be_an Array
expect(links.length).to eq 1
link = links.first
expect(link).to be_an Array
expect(link.first).to eq 'https://www.googleapis.com/drive/v3/files/asset-id2?alt=media'
expect(link.last).to be_a Hash
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -80,7 +80,8 @@ def stub_configuration
's3' => {
client_id: 'S3AppKey',
client_secret: 'S3AppSecret',
bucket: 's3.bucket'
bucket: 's3.bucket',
region: 'us-east-1'
})
end

Expand Down

0 comments on commit 2cffe03

Please sign in to comment.