Skip to content

Commit 8e564c9

Browse files
bartostefan14113
authored andcommitted
feat: Upgrade to latest version and upgrade rest client (#2)
1 parent d5bfbaf commit 8e564c9

File tree

5 files changed

+105
-10
lines changed

5 files changed

+105
-10
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ gemspec # load filepicker_client.gemspec as a gem
55
gem 'activesupport'
66
gem 'rake'
77
gem 'yard'
8+
gem 'minitest'

filepicker_client.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Gem::Specification.new do |s|
99
s.files = ["lib/filepicker_client.rb"]
1010
s.homepage = "https://github.com/infowrap/filepicker_client"
1111

12-
s.add_dependency "rest-client", "~> 1.6"
12+
s.add_dependency "rest-client", "~> 2.0"
1313
end

lib/filepicker_client.rb

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def sign(options={})
4747
if options[:handle]
4848
policy['handle'] = options[:handle]
4949
elsif options[:path]
50-
policy['path'] = (options[:path] + '/').gsub /\/+/, '/' # ensure path has a single, trailing '/'
50+
policy['path'] = (options[:path] + '/').gsub(/\/+/, '/') # ensure path has a single, trailing '/'
5151
end
5252

5353
if options[:min_size]
@@ -101,13 +101,15 @@ def file_read_uri_and_expiry(handle, expiry=DEFAULT_POLICY_EXPIRY)
101101

102102
return convert_hash(
103103
uri: uri,
104-
expiry: signage[:policy]['expiry'].to_i
104+
expiry: signage[:policy]['expiry'].to_i,
105+
signature: signage[:signature],
106+
policy: signage[:encoded_policy]
105107
)
106108
end
107109

108110
# Store the given file at the given storage path through Filepicker.
109-
# @param path [String] Path the file should be organized under in the destination storage
110111
# @param file [File] File to upload
112+
# @param path [String] Path the file should be organized under in the destination storage
111113
# @param options [Hash] optional additional filepicker.io request params
112114
# @return [FilepickerClientFile] Object representing the uploaded file in Filepicker
113115
def store(file, path=nil, options = {})
@@ -138,6 +140,26 @@ def store(file, path=nil, options = {})
138140
end
139141
end
140142

143+
# Store provided string in a file with the provided file name under the target storage path through Filepicker.
144+
# @param content [String] Contents of the file which should created in in the destination storage
145+
# @param file_name [String] Name that should be given to the file in Filepicker
146+
# @param path [String] Path the file should be organized under in the destination storage
147+
# @param options [Hash] optional additional filepicker.io request params
148+
# @return [FilepickerClientFile] Object representing the uploaded file in Filepicker
149+
def store_content(content, file_name, path=nil, options = {})
150+
store_file = Tempfile.new(file_name)
151+
152+
begin
153+
store_file.write content
154+
store_file.rewind
155+
156+
return store(store_file, path, options)
157+
ensure
158+
store_file.close
159+
store_file.unlink
160+
end
161+
end
162+
141163
# Store the file located at the given URL under the target storage path through Filepicker.
142164
# @param path [String] Path the file should be organized under in the destination storage
143165
# @param file_url [String] URL to get the file to upload from
@@ -365,7 +387,7 @@ def remove(handle)
365387
private
366388

367389
def get_fp_resource(uri)
368-
resource = RestClient::Resource.new(
390+
RestClient::Resource.new(
369391
uri.to_s,
370392
verify_ssl: (@filepicker_cert ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE),
371393
ssl_client_cert: @filepicker_cert

test/image.jpeg

9.08 KB
Loading

test/test_filepicker_client.rb

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
require 'test/unit'
1+
require 'minitest/autorun'
22
require 'tempfile'
33
require 'filepicker_client'
44

5-
class TestFilepickerClient < Test::Unit::TestCase
5+
class TestFilepickerClient < Minitest::Test
66
def setup
77
@api_key = ENV['FPAPIKEY']
88
@api_secret = ENV['FPAPISECRET']
@@ -16,7 +16,7 @@ def test_file_uri
1616

1717
def test_client_file_client_required
1818
begin
19-
client_file = FilepickerClientFile.new({}, nil)
19+
FilepickerClientFile.new({}, nil)
2020
rescue FilepickerClientError => e
2121
assert_equal "FilepickerClientFile client required", e.message
2222
error_fired = true
@@ -25,6 +25,78 @@ def test_client_file_client_required
2525
assert error_fired, "FilepickerClientFile did not require a client as it should"
2626
end
2727

28+
def test_store_string_text_file
29+
assert @api_key, "Must set FPAPIKEY for this test"
30+
assert @api_secret, "Must set FPAPISECRET for this test"
31+
32+
client = FilepickerClient.new @api_key, @api_secret
33+
content = "test file content\n" * 10
34+
35+
begin
36+
# store
37+
file = client.store_content(content, 'filename.txt', 'test')
38+
39+
# file attributes
40+
assert_match(/^filename\.txt/, file.filename)
41+
42+
#file_uri
43+
file_uri = file.file_uri
44+
assert_equal "https://www.filepicker.io/api/file/#{file.handle}", file_uri.to_s
45+
46+
# stat
47+
stats = file.stat
48+
assert_equal content.length, stats[:size]
49+
assert_equal 'text/plain', stats[:mimetype]
50+
51+
# read
52+
downloaded_content = file.read
53+
assert_equal content, downloaded_content
54+
55+
#remove test file
56+
assert file.remove
57+
rescue Exception => e
58+
raise e # reraise to have error reported
59+
end
60+
end
61+
62+
def test_store_string_image_file
63+
assert @api_key, "Must set FPAPIKEY for this test"
64+
assert @api_secret, "Must set FPAPISECRET for this test"
65+
66+
client = FilepickerClient.new @api_key, @api_secret
67+
68+
image_file = File.open('test/image.jpeg')
69+
content = image_file.read
70+
71+
begin
72+
# store
73+
file = client.store_content(content, 'filename.jpeg', 'test')
74+
75+
# file attributes
76+
assert_match(/^filename\.jpeg/, file.filename)
77+
78+
#file_uri
79+
file_uri = file.file_uri
80+
assert_equal "https://www.filepicker.io/api/file/#{file.handle}", file_uri.to_s
81+
82+
# stat
83+
stats = file.stat
84+
assert_equal image_file.size, stats[:size]
85+
assert_equal 'text/plain', stats[:mimetype]
86+
87+
# read
88+
downloaded_content = file.read
89+
assert_equal content, downloaded_content
90+
91+
#remove test file
92+
assert file.remove
93+
rescue Exception => e
94+
raise e # reraise to have error reported
95+
end
96+
end
97+
98+
99+
28100
def test_file
29101
assert @api_key, "Must set FPAPIKEY for this test"
30102
assert @api_secret, "Must set FPAPISECRET for this test"
@@ -48,7 +120,7 @@ def test_file
48120
file = client.store(store_file, 'test')
49121

50122
# file attributes
51-
assert_match /^test\.txt/, file.filename
123+
assert_match(/^test\.txt/, file.filename)
52124

53125
#file_uri
54126
file_uri = file.file_uri
@@ -65,7 +137,7 @@ def test_file
65137

66138
# store_url
67139
second_file = client.store_url file.file_read_uri_and_expiry[:uri], 'test'
68-
assert_not_equal second_file.handle, file.handle
140+
assert(second_file.handle != file.handle)
69141
assert_equal second_file.read, content
70142

71143
# write

0 commit comments

Comments
 (0)