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

Commit

Permalink
Make Communication class safer by not storing state inside some singl…
Browse files Browse the repository at this point in the history
…eton

Move specs around
  • Loading branch information
arnoFleming committed Mar 6, 2019
1 parent f272590 commit 6d1f239
Show file tree
Hide file tree
Showing 14 changed files with 979 additions and 572 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Great! Now you can go to your project file and use the client.
A transfer is a collection of files that can be created once, and downloaded until it expires. Once a transfer is ready for sharing, it is closed for modifications.

```ruby
# In your proje ct file:
# In your project file:
require 'we_transfer'

client = WeTransfer::Client.new(api_key: ENV.fetch('WT_API_KEY'))
Expand All @@ -75,21 +75,28 @@ client = WeTransfer::Client.new(api_key: ENV.fetch('WT_API_KEY'))
Now that you've got the client set up you can use `create_transfer_and_upload_files` to, well, create a transfer, and upload all files!

```ruby
client.create_transfer_and_upload_files(message: 'All the Things') do |transfer|
transfer = client.create_transfer_and_upload_files(message: 'All the Things') do |transfer|
# Add a file using File.open. If you do it like this, :name and :io params are optional
transfer.add_file(io: File.open('Gemfile'))

# Add a file with File.open, but give it a different name inside the transfer
transfer.add_file(
name: 'hello_world.rb',
io: File.open('path/to/different_name.rb')
)

# Using :name, :size and :io params.
# Specifying the size is not very useful in this case, but feel free to explicitly
# communicate the size of the coming io.
#
# The :name param is compulsory if it cannot be derived from the IO.
transfer.add_file(
name: 'README.txt',
size: 31,
io: StringIO.new("You should read All the Things!")
)
end

transfer = client.transfer

# To get a link to your transfer, call `url` on your transfer object:
transfer.url => "https://we.tl/t-1232346"

Expand Down
28 changes: 26 additions & 2 deletions lib/we_transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,32 @@
require 'json'
require 'ks'

%w[communication client transfer mini_io we_transfer_file remote_file version].each do |file|
%w[
logging
communication
client
transfer
mini_io
we_transfer_file
remote_file version
].each do |file|
require_relative "we_transfer/#{file}"
end

module WeTransfer; end
module WeTransfer
NULL_LOGGER = Logger.new(nil)

def self.logger
@logger || NULL_LOGGER
end

# Set the logger to your preferred logger
#
# @params new_logger [Logger] the logger that WeTransfer SDK should use
#
# example:
# WeTransfer.logger = Rails.logger
def self.logger=(new_logger)
@logger = new_logger
end
end
29 changes: 8 additions & 21 deletions lib/we_transfer/client.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,30 @@
module WeTransfer
class Client
class Error < StandardError; end
include Communication

NullLogger = Logger.new(nil)

attr_reader :transfer

# Initialize a WeTransfer::Client
#
# @param api_key [String] The API key you want to authenticate with
# @param logger [Logger] (NullLogger) your custom logger
#
# @return [WeTransfer::Client]
def initialize(api_key:, logger: NullLogger)
Communication.reset_authentication!
Communication.api_key = api_key
Communication.logger = logger
def initialize(api_key:)
@communicator = Communication.new(api_key)
end

def create_transfer(**args, &block)
transfer = WeTransfer::Transfer.new(args)
transfer = WeTransfer::Transfer.new(args.merge(communicator: @communicator))
transfer.persist(&block)
@transfer = transfer

self
end

def create_transfer_and_upload_files(**args, &block)
create_transfer(args, &block)
@transfer.upload_files
@transfer.complete_files
@transfer.finalize

self
transfer = create_transfer(args, &block)
transfer.upload_files
transfer.complete_files
transfer.finalize
end

def find_transfer(transfer_id)
@transfer = WeTransfer::Transfer.find(transfer_id)
@communicator.find_transfer(transfer_id)
end
end
end
Loading

0 comments on commit 6d1f239

Please sign in to comment.