Skip to content

Commit abdd832

Browse files
committed
Integration specs
1 parent 5bbdf0b commit abdd832

25 files changed

Lines changed: 432 additions & 205 deletions

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ You can use these URLs to download output files:
7070
```rb
7171
exported_url_task_id = "84e872fc-d823-4363-baab-eade2e05ee54"
7272
task = cloudconvert.tasks.wait(exported_url_task_id) # Wait for job completion
73-
file = task.result.files[0]
73+
file = task.result.files.first
7474
export = cloudconvert.download(file.url)
7575
```
7676

@@ -102,7 +102,7 @@ job = cloudconvert.jobs.create({
102102
After you've created a `import/upload` task, you can upload a file:
103103

104104
```rb
105-
upload_task = job.tasks[0]
105+
upload_task = job.tasks.where(operation: "import/upload").first
106106

107107
response = cloudconvert.tasks.upload("/path/to/sample.pdf", upload_task)
108108

cloudconvert.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
2121
spec.add_dependency "json"
2222
spec.add_dependency "memoizable", "~> 0.4.0"
2323
spec.add_dependency "mimemagic", "~> 0.3.5"
24+
spec.add_dependency "multi_xml"
2425
spec.add_dependency "openssl"
2526
spec.add_dependency "ostruct"
2627
spec.add_dependency "schemacop", "~> 2.4"

lib/cloudconvert.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
require "cloudconvert/resources/tasks"
2626
require "cloudconvert/resources/users"
2727
require "cloudconvert/task"
28+
require "cloudconvert/upload"
2829
require "cloudconvert/user"
2930
require "cloudconvert/version"
3031
require "cloudconvert/webhook"

lib/cloudconvert/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def define_collection_method(key, klass)
9999
define_method(key) do
100100
collection = @attrs[key] || []
101101
entity = CloudConvert.const_get(klass)
102-
collection.map { |item| entity.new(item) }
102+
Collection.new collection.map { |item| entity.new(item) }
103103
end
104104
memoize(key)
105105
end

lib/cloudconvert/client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def connection
9292
f.request :json
9393
f.request :multipart
9494
f.use CloudConvert::Middleware::ParseJson, content_type: /\bjson$/
95+
f.use FaradayMiddleware::ParseXml, content_type: /\bxml$/
9596
end
9697
end
9798

lib/cloudconvert/entity.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module CloudConvert
22
class Entity < Base
3+
attr_reader :id
4+
35
include Equalizer.new(:id)
46

57
class << self

lib/cloudconvert/resources/tasks.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def wait(id)
5656

5757
# @param file [File, String, IO] Either a String filename to a local file or an open IO object.
5858
# @param task [Task] The "import/upload" Task to upload the file to.
59-
# @return [Response]
59+
# @return [Upload]
6060
def upload(file, task)
6161
unless task.operation == "import/upload"
6262
raise ArgumentError.new("The task operation is not import/upload")
@@ -68,10 +68,12 @@ def upload(file, task)
6868

6969
file = File.new(file) unless file.is_a? File
7070

71-
client.post(task.result.form.url, task.result.form.parameters.to_h.merge(file: file)) do |request|
71+
response = client.post(task.result.form.url, task.result.form.parameters.to_h.merge(file: file)) do |request|
7272
request.headers.delete("Authorization")
7373
request.headers["Content-Type"] = "multipart/form-data"
7474
end
75+
76+
Upload.new Hash[*response["PostResponse"].flat_map { |k, v| [k.downcase.to_sym, v] }]
7577
end
7678
end
7779
end

lib/cloudconvert/upload.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module CloudConvert
2+
class Upload < Base
3+
# @return [String]
4+
attr_reader :bucket, :etag, :key, :location
5+
end
6+
end

spec/client_spec.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
API_KEY_ATTR = "api key from attrs"
33
API_KEY_ENV = "api key from env"
44

5-
subject do
5+
let(:cloudconvert) do
66
CloudConvert::Client.new(api_key: API_KEY_ATTR)
77
end
88

99
describe ".new" do
1010
it "reads the api key out of attrs" do
11-
expect(subject.api_key).to eq API_KEY_ATTR
11+
expect(cloudconvert.api_key).to eq API_KEY_ATTR
1212
end
1313

1414
it "reads the api key out of the env variable" do
@@ -19,7 +19,7 @@
1919

2020
it "prefers the api key out of attrs" do
2121
with_env CLOUDCONVERT_API_KEY: API_KEY_ENV do
22-
expect(subject.api_key).to eq API_KEY_ATTR
22+
expect(cloudconvert.api_key).to eq API_KEY_ATTR
2323
end
2424
end
2525

@@ -33,11 +33,11 @@
3333

3434
it "reads the sandbox bool out of the env variable" do
3535
with_env CLOUDCONVERT_SANDBOX: "true" do
36-
expect(subject.sandbox).to eq true
36+
expect(cloudconvert.sandbox).to eq true
3737
end
3838

3939
with_env CLOUDCONVERT_SANDBOX: "TRUE" do
40-
expect(subject.sandbox).to eq true
40+
expect(cloudconvert.sandbox).to eq true
4141
end
4242
end
4343

@@ -52,32 +52,32 @@
5252
end
5353

5454
it "defaults to false when no sandbox bool is supplied" do
55-
expect(subject.sandbox).to be false
55+
expect(cloudconvert.sandbox).to be false
5656
end
5757
end
5858

5959
describe "#download" do
6060
it "downloads the file and returns a tempfile" do
6161
stub_request(:get, "https://storage.cloudconvert.com/file.mp4").to_return(body: "video content")
62-
expect(subject.download("https://storage.cloudconvert.com/file.mp4")).to be_a Tempfile
62+
expect(cloudconvert.download("https://storage.cloudconvert.com/file.mp4")).to be_a Tempfile
6363
end
6464
end
6565

6666
describe "#jobs" do
6767
it "returns jobs resource" do
68-
expect(subject.jobs).to be_a CloudConvert::Resources::Jobs
68+
expect(cloudconvert.jobs).to be_a CloudConvert::Resources::Jobs
6969
end
7070
end
7171

7272
describe "#tasks" do
7373
it "returns tasks resource" do
74-
expect(subject.tasks).to be_a CloudConvert::Resources::Tasks
74+
expect(cloudconvert.tasks).to be_a CloudConvert::Resources::Tasks
7575
end
7676
end
7777

7878
describe "#users" do
7979
it "returns users resource" do
80-
expect(subject.users).to be_a CloudConvert::Resources::Users
80+
expect(cloudconvert.users).to be_a CloudConvert::Resources::Users
8181
end
8282
end
8383
end

spec/collection_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
describe CloudConvert::Collection do
2+
let(:collection) do
3+
CloudConvert::Collection.new [import_task, export_task]
4+
end
5+
6+
let(:import_task) do
7+
CloudConvert::Task.new(name: "import/url", status: :finished)
8+
end
9+
10+
let(:export_task) do
11+
CloudConvert::Task.new(name: "export/url", status: :finished)
12+
end
13+
14+
describe "#where" do
15+
it "should filter results" do
16+
expect(collection.where(name: "import/url").first).to be import_task
17+
expect(collection.where(name: "import/url").count).to be 1
18+
expect(collection.where(name: "export/url").first).to be export_task
19+
expect(collection.where(name: "export/url").count).to be 1
20+
expect(collection.where(name: "import/url", status: :finished).first).to be import_task
21+
expect(collection.where(name: "import/url", status: :finished).count).to be 1
22+
expect(collection.where(name: "export/url", status: :finished).first).to be export_task
23+
expect(collection.where(name: "export/url", status: :finished).count).to be 1
24+
expect(collection.where(name: "import/url", status: :waiting).first).to be nil
25+
expect(collection.where(name: "import/url", status: :waiting).count).to be 0
26+
expect(collection.where(name: "export/url", status: :waiting).first).to be nil
27+
expect(collection.where(name: "export/url", status: :waiting).count).to be 0
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)