Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

[WIP] Isolate Board Object #48

Open
wants to merge 37 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a272b07
Moved all classes into wetransfer module and extracted client methods…
Oct 29, 2018
cd8b9e7
added manual create/upload/complete actions to boards
Nov 7, 2018
80744e9
Group items in file/link array and send file size instead of whole io
Nov 8, 2018
ac52001
Refactor board behaviour
Nov 8, 2018
75ac14f
rearranging methods and ensure respons for select file method
Nov 12, 2018
f6d070a
Remove raise if no file is found
Nov 12, 2018
329660d
remove unused methods
Nov 12, 2018
251df6d
Show 400..499 error message in response
Nov 13, 2018
77d4a90
Items is public method
Nov 13, 2018
04c8d49
added space in argument
Nov 13, 2018
4d58dbf
names are strings, size are integers, changed error class and also re…
Nov 13, 2018
d855942
Urls and title should always be strings and return with a remote_link…
Nov 13, 2018
4d4b620
Omit the success key for remote boards
Nov 13, 2018
11e72d6
added authorise call to all calls
Nov 13, 2018
bffa121
Rubocops
Nov 13, 2018
23d0df6
skip feature tests
Nov 13, 2018
014c8ae
Updated tests to latest changes
Nov 13, 2018
a97c47c
updated files to test all methods
Nov 14, 2018
c655ebd
Swapped files names
Nov 15, 2018
504d919
initialize objects with client and parent_objects
Nov 15, 2018
2b5fe60
There, I rigged the tests
Nov 15, 2018
ad2cf4e
singular thing
Nov 16, 2018
c6c499d
Board in singular
Nov 16, 2018
aa86183
Added states to files and links
Nov 21, 2018
168c7fe
Changed the to singular transfers
Nov 21, 2018
0264f2e
removed functionality
Nov 21, 2018
da3c8bc
description to empty string in initialiser
Nov 21, 2018
4fb405b
Remove passthrough of client in methods
Nov 21, 2018
9b068f9
removed trailing space in block method
Nov 21, 2018
a04ed09
commented out tests
Nov 21, 2018
5ca896e
Added space inside block
Nov 21, 2018
0276e6d
removed empty space before let definition
Nov 21, 2018
4ef2eff
Set failing test to pending
Nov 21, 2018
5abbc5f
indentation
Nov 21, 2018
469e0a5
Add integration specs to guard watch
Nov 21, 2018
6ee3d9a
change of website in readme
Nov 21, 2018
0340f62
Typo fix
Nov 21, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
group :red_green_refactor, halt_on_fail: true do
guard :rspec, cmd: 'bundle exec rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^spec/.+.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
end

Expand Down
2 changes: 1 addition & 1 deletion V2_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ board = client.create_board(name: 'Dog Collection', description: 'A collection o
item.add_file(name: 'dalmatian.jpg', io: File.open('path/to/dalmatian.jpg', 'r'))
item.add_file(name: 'beagle.jpg', io: File.open('path/to/beagle.jpg', 'r'))
item.add_file(name: 'great_dane.jpg', io: File.open('path/to/great_dane.jpg', 'r'))
item.add_web_url(url: 'http://www.wetransfer.com', title: 'WeTransfer Website')
item.add_web_url(url: 'https://www.developers.wetransfer.com', title: 'WeTransfer Dev Portal')
end
```

Expand Down
43 changes: 5 additions & 38 deletions lib/we_transfer_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
require_relative 'we_transfer_client/remote_board'
require_relative 'we_transfer_client/remote_link'
require_relative 'we_transfer_client/remote_file'
require_relative 'we_transfer_client/transfers'
require_relative 'we_transfer_client/boards'
require_relative 'we_transfer_client/transfer'
require_relative 'we_transfer_client/board'

module WeTransfer
class TransferIOError < StandardError; end
class Client
include WeTransfer::Client::Transfers
include WeTransfer::Client::Boards

class Error < StandardError
end
class Error < StandardError; end

NULL_LOGGER = Logger.new(nil)

Expand All @@ -33,36 +30,6 @@ def initialize(api_key:, logger: NULL_LOGGER)
@logger = logger
end

def upload_file(object:, file:, io:)
put_io_in_parts(object: object, file: file, io: io)
end

def complete_file!(object:, file:)
object.prepare_file_completion(client: self, file: file)
end

def check_for_file_duplicates(files, new_file)
if files.select { |file| file.name == new_file.name }.size != 1
raise ArgumentError, 'Duplicate file entry'
end
end

def put_io_in_parts(object:, file:, io:)
(1..file.multipart.part_numbers).each do |part_n_one_based|
upload_url, chunk_size = object.prepare_file_upload(client: self, file: file, part_number: part_n_one_based)
part_io = StringIO.new(io.read(chunk_size))
part_io.rewind
response = faraday.put(
upload_url,
part_io,
'Content-Type' => 'binary/octet-stream',
'Content-Length' => part_io.size.to_s
)
ensure_ok_status!(response)
end
{success: true, message: 'File Uploaded'}
end

def faraday
Faraday.new(@api_url_base) do |c|
c.response :logger, @logger
Expand Down Expand Up @@ -95,7 +62,7 @@ def ensure_ok_status!(response)
true
when 400..499
@logger.error response
raise Error, "Response had a #{response.status} code, the server will not accept this request even if retried"
raise Error, JSON.parse(response.body, symbolize_names: true)[:message]
when 500..504
@logger.error response
raise Error, "Response had a #{response.status} code, we could retry"
Expand Down
51 changes: 51 additions & 0 deletions lib/we_transfer_client/board.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module WeTransfer
class Board
attr_reader :remote_board

def initialize(client:, name:, description: nil)
@client = client
@remote_board = create_remote_board(name: name, description: description)
@builder = WeTransfer::BoardBuilder.new(client: @client)
end

def add_items
yield(@builder)
add_items_to_remote_board(future_items: @builder.items)
rescue LocalJumpError
raise ArgumentError, 'No items where added to the board'
end

def upload_file!(io:, name: File.basename(io.to_path))
local_file = @builder.select_file_on_name(name: name)
local_file.upload_file(io: io)
end

def complete_file!(name:)
local_file = @builder.select_file_on_name(name: name)
local_file.complete_file
end

private

def create_remote_board(name:, description:, future_board_class: FutureBoard)
future_board = future_board_class.new(name: name, description: description)
@client.authorize_if_no_bearer_token!
response = @client.faraday.post(
'/v2/boards',
JSON.pretty_generate(future_board.to_initial_request_params),
@client.auth_headers.merge('Content-Type' => 'application/json')
)
@client.ensure_ok_status!(response)
WeTransfer::RemoteBoard.new(JSON.parse(response.body, symbolize_names: true))
end

def add_items_to_remote_board(future_items:)
future_items.group_by(&:class).each do |_group, grouped_items|
grouped_items.each do |item|
item.add_to_board(remote_board: @remote_board)
end
end
@remote_board
end
end
end
50 changes: 25 additions & 25 deletions lib/we_transfer_client/board_builder.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
class BoardBuilder
attr_reader :items
class TransferIOError < StandardError; end
module WeTransfer
class BoardBuilder
attr_reader :files, :links

def initialize
@items = []
end
def initialize(client:)
@client = client
@files = []
@links = []
end

def add_file(name:, io:)
ensure_io_compliant!(io)
@items << FutureFile.new(name: name, io: io)
end
def items
(@files + @links).flatten
end

def add_file_at(path:)
add_file(name: File.basename(path), io: File.open(path, 'rb'))
end
def add_file(name:, size:)
@files << FutureFile.new(name: name, size: size, client: @client)
end

def add_web_url(url:, title: url)
@items << FutureLink.new(url: url, title: title)
end
def add_file_at(path:)
add_file(name: File.basename(path), size: File.size(path))
end

private
def add_web_url(url:, title: url)
@links << FutureLink.new(url: url, title: title, client: @client)
end

def ensure_io_compliant!(io)
io.seek(0)
io.read(1) # Will cause things like Errno::EACCESS to happen early, before the upload begins
io.seek(0) # Also rewinds the IO for later uploading action
size = io.size # Will cause a NoMethodError
raise TransferIOError, "#{File.basename(io)}, given to add_file has a size of 0" if size <= 0
rescue NoMethodError
raise TransferIOError, "#{File.basename(io)}, given to add_file must respond to seek(), read() and size(), but #{io.inspect} did not"
def select_file_on_name(name:)
file = files.select { |f| f.name == name }.first
return file if file
raise WeTransfer::TransferIOError, 'File not found'
end
end
end
74 changes: 0 additions & 74 deletions lib/we_transfer_client/boards.rb

This file was deleted.

41 changes: 13 additions & 28 deletions lib/we_transfer_client/future_board.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
class FutureBoard
attr_reader :name, :description, :items
module WeTransfer
class FutureBoard
attr_reader :name, :description

def initialize(name:, description: nil, items: [])
@name = name
@description = description
@items = items
end

def files
@items.select { |item| item.class == FutureFile }
end

def links
@items.select { |item| item.class == FutureLink }
end

def to_initial_request_params
{
name: name,
description: description,
}
end
def initialize(name:, description: '')
@name = name.to_s
@description = description.to_s
end

def to_request_params
{
name: name,
description: description,
items: items.map(&:to_request_params),
}
def to_initial_request_params
{
name: name,
description: description,
}
end
end
end
Loading