tessa 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b10fb71b4a6667e71ce7be6c39e6a497a5f6399
4
- data.tar.gz: 340818b12c567e206a9a13b3cf3da455b5a927d8
3
+ metadata.gz: f249bb180317b0210a3ccb8ac00990203ef3b958
4
+ data.tar.gz: d1977678af1fd84081e3ecc11b67469461c9d0ac
5
5
  SHA512:
6
- metadata.gz: 07fa141947b98f509ed36f32df7b8d17cb2bf7b416661f1bad7a5da5069b5a32e959ae23ea1b06723bb5d96b951390cded79a49765b483c56011c3ec89654274
7
- data.tar.gz: c51f1e24de102066b4d6565fd29ed9f505fc452f50be596987f4f5ef1c6e07d7ef90f259eb34b03f8c3b03851d0d9df32e954db6473eaa2be3ea413e4ac2495d
6
+ metadata.gz: 6cfbfe4952713a3d56f5f35fe440135f1da9bc41d44e43e2a0db496729a599bf78916b587ea9f4c897765410630755f7ac32442591e8dc365c04f1da96a04caa
7
+ data.tar.gz: 590c79b194d50cb6e0d5db7399ab8e65ed4b6e47d4bd3396cc3eca03be8b0068e787b6d5185bf1a7ddf29bd159a2e76d5710fb1ff7f70332911c12e57161fd76
data/bin/rspec ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rspec-core", "rspec")
data/lib/tessa/asset.rb CHANGED
@@ -29,6 +29,10 @@ module Tessa
29
29
  new_from_response connection.get("/assets/#{ids.join(",")}")
30
30
  end
31
31
 
32
+ def self.create(file:, **options)
33
+ Upload.create(options).upload_file(file)
34
+ end
35
+
32
36
  def failure?
33
37
  false
34
38
  end
data/lib/tessa/upload.rb CHANGED
@@ -7,10 +7,25 @@ module Tessa
7
7
  attribute :upload_url, String
8
8
  attribute :upload_method, String
9
9
 
10
+ def upload_file(file)
11
+ if UploadsFile.new(upload: self).(file)
12
+ asset.complete!
13
+ else
14
+ asset.cancel!
15
+ false
16
+ end
17
+ end
18
+
10
19
  def self.create(connection: Tessa.config.connection,
11
20
  strategy: Tessa.config.strategy,
12
21
  **options)
13
22
  new_from_response connection.post('/uploads', options.merge(strategy: strategy))
14
23
  end
24
+
25
+ private def asset
26
+ Asset.new(id: asset_id)
27
+ end
15
28
  end
16
29
  end
30
+
31
+ require 'tessa/upload/uploads_file'
@@ -0,0 +1,23 @@
1
+ class Tessa::Upload::UploadsFile
2
+ attr_reader :upload, :connection
3
+
4
+ def initialize(upload:, connection: self.class.connection_factory)
5
+ @upload = upload
6
+ @connection = connection
7
+ end
8
+
9
+ def call(file)
10
+ params = { file: Faraday::UploadIO.new(file, "application/octet-stream") }
11
+ connection
12
+ .public_send(upload.upload_method, upload.upload_url, params)
13
+ .success?
14
+ end
15
+
16
+ def self.connection_factory
17
+ Faraday.new do |conn|
18
+ conn.request :multipart
19
+ conn.request :url_encoded
20
+ conn.adapter Faraday.default_adapter
21
+ end
22
+ end
23
+ end
data/lib/tessa/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Tessa
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -143,4 +143,28 @@ RSpec.describe Tessa::Asset do
143
143
  end
144
144
  end
145
145
 
146
+ describe ".create" do
147
+ let(:upload) { instance_double(Tessa::Upload) }
148
+
149
+ before :each do
150
+ allow(Tessa::Upload).to receive(:create).and_return(upload)
151
+ allow(upload).to receive(:upload_file)
152
+ end
153
+
154
+ it "requires a file param" do
155
+ expect { described_class.create }.to raise_error(ArgumentError)
156
+ expect { described_class.create(file: "abc") }.to_not raise_error
157
+ end
158
+
159
+ it "creates a new upload" do
160
+ expect(Tessa::Upload).to receive(:create).with(foo: :bar, baz: :boom).and_return(upload)
161
+ described_class.create(file: "abc", foo: :bar, baz: :boom)
162
+ end
163
+
164
+ it "uploads the passed file to the upload's URL" do
165
+ expect(Tessa::Upload).to receive(:create).and_return(upload)
166
+ expect(upload).to receive(:upload_file).with(:file)
167
+ described_class.create(file: :file)
168
+ end
169
+ end
146
170
  end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Tessa::Upload::UploadsFile do
4
+ describe "#initialize" do
5
+ it "requires an upload and sets it to attribute" do
6
+ expect { described_class.new }.to raise_error(ArgumentError)
7
+ obj = described_class.new(upload: :upload)
8
+ expect(obj.upload).to eq(:upload)
9
+ end
10
+
11
+ it "optionally takes a connection" do
12
+ obj = described_class.new(upload: :upload, connection: :conn)
13
+ expect(obj.connection).to eq(:conn)
14
+ end
15
+
16
+ it "defaults connection to empty Faraday connection" do
17
+ expect(described_class).to receive(:connection_factory).and_return(:foo)
18
+ obj = described_class.new(upload: :upload)
19
+ expect(obj.connection).to eq(:foo)
20
+ end
21
+ end
22
+
23
+ describe "#call" do
24
+ let(:upload) {
25
+ instance_double(
26
+ Tessa::Upload,
27
+ upload_url: "http://upload/path?arg=1",
28
+ upload_method: "post"
29
+ )
30
+ }
31
+ let(:connection) {
32
+ Faraday.new { |f| f.adapter :test, stubs }
33
+ }
34
+ subject(:task) { described_class.new(upload: upload, connection: connection) }
35
+
36
+ context "uploads file successfully" do
37
+ let(:stubs) {
38
+ Faraday::Adapter::Test::Stubs.new do |stub|
39
+ stub.post("http://upload/path?arg=1") { |env|
40
+ [200, {}, '']
41
+ }
42
+ end
43
+ }
44
+
45
+ it "calls the upload_url with upload_method HTTP method" do
46
+ file = __FILE__
47
+ expect(connection).to receive(:post).with("http://upload/path?arg=1", hash_including(:file)).and_call_original
48
+ expect(task.call(file)).to be_truthy
49
+ end
50
+ end
51
+
52
+ context "uploads file successfully" do
53
+ let(:stubs) {
54
+ Faraday::Adapter::Test::Stubs.new do |stub|
55
+ stub.post("http://upload/path?arg=1") { |env|
56
+ [500, {}, '']
57
+ }
58
+ end
59
+ }
60
+
61
+ it "calls the upload_url with upload_method HTTP method" do
62
+ file = __FILE__
63
+ expect(connection).to receive(:post).with("http://upload/path?arg=1", hash_including(:file)).and_call_original
64
+ expect(task.call(file)).to be_falsey
65
+ end
66
+ end
67
+ end
68
+
69
+ describe ".connection_factory" do
70
+ it "returns a new Faraday::Connection with the default adapter and url_encoded and multipart" do
71
+ obj = described_class.connection_factory
72
+ expect(obj).to be_a(Faraday::Connection)
73
+ expect(obj.builder.handlers)
74
+ .to eq([Faraday::Request::Multipart, Faraday::Request::UrlEncoded, Faraday::Adapter::NetHttp])
75
+ end
76
+ end
77
+
78
+ end
@@ -26,7 +26,52 @@ RSpec.describe Tessa::Upload do
26
26
  end
27
27
  end
28
28
 
29
- describe "::create" do
29
+ describe "#upload_file" do
30
+ subject(:upload) { described_class.new }
31
+ let(:task) { instance_double(described_class::UploadsFile) }
32
+
33
+ before :each do
34
+ allow(described_class::UploadsFile).to receive(:new).and_return(task)
35
+ allow(task).to receive(:call)
36
+ allow(Tessa::Asset).to receive(:new).and_return(instance_spy(Tessa::Asset))
37
+ end
38
+
39
+ it "initializes UploadsFile with self and passes argument to call" do
40
+ expect(task).to receive(:call).with(:file)
41
+ expect(described_class::UploadsFile).to receive(:new).with(upload: upload).and_return(task)
42
+ upload.upload_file(:file)
43
+ end
44
+
45
+ context "with a successful upload" do
46
+ before :each do
47
+ allow(task).to receive(:call).and_return(true)
48
+ end
49
+
50
+ it "updates the asset to complete state and returns asset" do
51
+ upload.asset_id = 123
52
+ asset = Tessa::Asset.new(id: 123)
53
+ expect(Tessa::Asset).to receive(:new).with(id: 123).and_return(asset)
54
+ expect(asset).to receive(:complete!).and_return(:asset)
55
+ expect(upload.upload_file(:file)).to eq(:asset)
56
+ end
57
+ end
58
+
59
+ context "with a failed upload" do
60
+ before :each do
61
+ allow(task).to receive(:call).and_return(false)
62
+ end
63
+
64
+ it "updates the asset to canceled state and returns false" do
65
+ upload.asset_id = 123
66
+ asset = Tessa::Asset.new(id: 123)
67
+ expect(Tessa::Asset).to receive(:new).with(id: 123).and_return(asset)
68
+ expect(asset).to receive(:cancel!).and_return(:asset)
69
+ expect(upload.upload_file(:file)).to eq(false)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe ".create" do
30
75
  subject(:call) { described_class.create(call_args) }
31
76
 
32
77
  include_examples "remote call macro", :post, "/uploads", Tessa::Upload
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tessa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Powell
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-21 00:00:00.000000000 Z
12
+ date: 2017-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -85,7 +85,8 @@ description: Manage your assets.
85
85
  email:
86
86
  - jpowell@watermark.org
87
87
  - tpetticrew@watermark.org
88
- executables: []
88
+ executables:
89
+ - rspec
89
90
  extensions: []
90
91
  extra_rdoc_files: []
91
92
  files:
@@ -97,6 +98,7 @@ files:
97
98
  - LICENSE.txt
98
99
  - README.md
99
100
  - Rakefile
101
+ - bin/rspec
100
102
  - circle.yml
101
103
  - lib/tessa.rb
102
104
  - lib/tessa/asset.rb
@@ -110,6 +112,7 @@ files:
110
112
  - lib/tessa/rack_upload_proxy.rb
111
113
  - lib/tessa/response_factory.rb
112
114
  - lib/tessa/upload.rb
115
+ - lib/tessa/upload/uploads_file.rb
113
116
  - lib/tessa/version.rb
114
117
  - lib/tessa/view_helpers.rb
115
118
  - spec/spec_helper.rb
@@ -123,6 +126,7 @@ files:
123
126
  - spec/tessa/model_field_spec.rb
124
127
  - spec/tessa/model_spec.rb
125
128
  - spec/tessa/rack_upload_proxy_spec.rb
129
+ - spec/tessa/upload/uploads_file_spec.rb
126
130
  - spec/tessa/upload_spec.rb
127
131
  - tessa.gemspec
128
132
  homepage: https://github.com/watermarkchurch/tessa-client
@@ -161,4 +165,5 @@ test_files:
161
165
  - spec/tessa/model_field_spec.rb
162
166
  - spec/tessa/model_spec.rb
163
167
  - spec/tessa/rack_upload_proxy_spec.rb
168
+ - spec/tessa/upload/uploads_file_spec.rb
164
169
  - spec/tessa/upload_spec.rb