Skip to content

Commit

Permalink
Merge with main and add changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulnsward committed Mar 9, 2024
1 parent 2c6a314 commit 931d8fa
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 153 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ jobs:
strategy:
matrix:
ruby-version:
- 2.7
- 3.0
- 3.1
- 3.2
- 3.3

steps:
- uses: actions/checkout@v3
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler: 2.4
bundler: latest
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- name: Run tests
Expand All @@ -41,8 +41,7 @@ jobs:
strategy:
matrix:
ruby-version:
- 2.7
- 3.2
- 3.3
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
Expand Down
6 changes: 1 addition & 5 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AllCops:
NewCops: enable
TargetRubyVersion: 2.7
TargetRubyVersion: 3.0

Layout/LineLength:
Max: 120
Expand All @@ -14,10 +14,6 @@ Style/HashTransformKeys:
- 'lib/uploadcare/client/conversion/video_conversion_client.rb'
- 'lib/uploadcare/entity/file.rb'

Gemspec/RequiredRubyVersion:
Exclude:
- 'uploadcare-ruby.gemspec'

Metrics/BlockLength:
Exclude:
- 'bin/'
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 4.4.0 — 2024-03-09

### Breaking

* Drop support of unmaintainable Ruby versions < 3.x

### Fixed

* Update locations where Dry::Monads structure has changed.
* Sign URL uploads if configured (#139)
* Start returning proper error message when raising RequestError in poll_upload_response, to hint to users what is going on. Fixes #141
* When polling, raise if an error is returned (#142)
* Fix documentation about original file url on simple file upload.

### Changed
* Support params in Rest client and in file info method, to allow passing custom params like "include=appdata" in `Uploadcare::File.file` calls. Closes #132


## 4.3.6 — 2023-11-18

### Fixed
Expand Down
12 changes: 6 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

source 'https://rubygems.org'

gem 'byebug', '~> 11.1'
gem 'rake', '~> 13.0'
gem 'rspec', '~> 3.0'
gem 'rubocop', '~> 1.48'
gem 'vcr', '~> 6.1'
gem 'webmock', '~> 3.18'
gem 'byebug'
gem 'rake'
gem 'rspec'
gem 'rubocop'
gem 'vcr'
gem 'webmock'

# Specify your gem's dependencies in uploadcare-ruby.gemspec
gemspec
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ wrapping Upload and REST APIs.
* [Useful links](#useful-links)

## Requirements
* ruby 2.7+
* ruby 3.0+

## Compatibility

Expand Down Expand Up @@ -101,8 +101,8 @@ Using Uploadcare is simple, and here are the basics of handling files.
# => "dc99200d-9bd6-4b43-bfa9-aa7bfaefca40"

# URL for the file, can be used with your website or app right away
@uc_file.url
# => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/"
@uc_file.original_file_url
# => "https://ucarecdn.com/dc99200d-9bd6-4b43-bfa9-aa7bfaefca40/your-file.png"
```

Your might then want to store or delete the uploaded file. Storing files could
Expand Down Expand Up @@ -134,7 +134,7 @@ Uploadcare::Uploader.upload("https://placekitten.com/96/139")
There are explicit ways to select upload type:

```ruby
files = [File.open("1.jpg"), File.open("1.jpg"]
files = [File.open("1.jpg"), File.open("1.jpg")]
Uploadcare::Uploader.upload_files(files)

Uploadcare::Uploader.upload_from_url("https://placekitten.com/96/139")
Expand Down Expand Up @@ -208,10 +208,10 @@ Entities are representations of objects in Uploadcare cloud.

#### File

File entity contains its metadata.
File entity contains its metadata. It also supports `include` param to include additional fields to the file object, such as: "appdata".

```ruby
@file = Uploadcare::File.file("FILE_ID_IN_YOUR_PROJECT")
@file = Uploadcare::File.file("FILE_ID_IN_YOUR_PROJECT", include: "appdata")
{
"datetime_removed"=>nil,
"datetime_stored"=>"2018-11-26T12:49:10.477888Z",
Expand Down
12 changes: 0 additions & 12 deletions lib/uploadcare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@
module Uploadcare
extend Dry::Configurable

# NOTE: The dry-configurable gem has introduced the `default` keyword argument
# and deprecated the positional default argument in v0.13.0, which requires
# Ruby >= 2.6.0. In order to provide backwards compatibility and not disable
# deprecation warnings, we override the dry-configurable's `setting` DSL method.
def self.setting(name, default:, **options, &block)
if RUBY_VERSION < '2.6'
super name, default, &block
else
super
end
end

setting :public_key, default: ENV.fetch('UPLOADCARE_PUBLIC_KEY', '')
setting :secret_key, default: ENV.fetch('UPLOADCARE_SECRET_KEY', '')
setting :auth_type, default: 'Uploadcare'
Expand Down
4 changes: 2 additions & 2 deletions lib/uploadcare/client/file_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def index

# Acquire file info
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#operation/fileInfo
def info(uuid)
get(uri: "/files/#{uuid}/")
def info(uuid, params = {})
get(uri: "/files/#{uuid}/", params: params)
end
alias file info

Expand Down
7 changes: 5 additions & 2 deletions lib/uploadcare/client/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ def request(uri:, method: 'GET', **options)
request_headers = Param::AuthenticationHeader.call(method: method.upcase, uri: uri,
content_type: headers[:'Content-Type'], **options)
handle_throttling do
send("api_struct_#{method.downcase}", path: remove_trailing_slash(uri),
headers: request_headers, body: options[:content])
send("api_struct_#{method.downcase}",
path: remove_trailing_slash(uri),
headers: request_headers,
body: options[:content],
params: options[:params])
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/uploadcare/client/uploader_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def handle_polling_response(response)
when 'error'
raise RequestError, response.success[:error]
when 'progress', 'waiting', 'unknown'
raise RetryError, response.success[:error]
raise RetryError, response.success[:error] || 'Upload is taking longer than expected. Try increasing the max_request_tries config if you know your file uploads will take more time.' # rubocop:disable Layout/LineLength
end

response
Expand Down
40 changes: 27 additions & 13 deletions lib/uploadcare/param/secure_auth_header.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# frozen_string_literal: true

require 'digest/md5'
require 'addressable/uri'

module Uploadcare
module Param
# This object returns headers needed for authentication
# This authentication method is more secure, but more tedious
class SecureAuthHeader
# @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
def self.call(options = {})
@method = options[:method]
@body = options[:content] || ''
@content_type = options[:content_type]
@uri = options[:uri]
@date_for_header = timestamp
{
Date: @date_for_header,
Authorization: "Uploadcare #{Uploadcare.config.public_key}:#{signature}"
}
end

class << self
# @see https://uploadcare.com/docs/api_reference/rest/requests_auth/#auth-uploadcare
def call(options = {})
@method = options[:method]
@body = options[:content] || ''
@content_type = options[:content_type]
@uri = make_uri(options)

@date_for_header = timestamp
{
Date: @date_for_header,
Authorization: "Uploadcare #{Uploadcare.config.public_key}:#{signature}"
}
end

def signature
content_md5 = Digest::MD5.hexdigest(@body)
sign_string = [@method, content_md5, @content_type, @date_for_header, @uri].join("\n")
Expand All @@ -31,6 +33,18 @@ def signature
def timestamp
Time.now.gmtime.strftime('%a, %d %b %Y %H:%M:%S GMT')
end

private

def make_uri(options)
if options[:params] && !options[:params].empty?
uri = Addressable::URI.parse options[:uri]
uri.query_values = uri.query_values(Array).to_a.concat(options[:params].to_a)
uri.to_s
else
options[:uri]
end
end
end
end
end
Expand Down
58 changes: 57 additions & 1 deletion spec/fixtures/vcr_cassettes/rest_file_info.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'byebug'
require 'webmock/rspec'
require 'uploadcare'
Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].sort.each { |f| require f }
Dir[File.expand_path(File.join(File.dirname(__FILE__), 'support', '**', '*.rb'))].each { |f| require f }

RSpec.configure do |config|
include Uploadcare::Exception
Expand Down
9 changes: 9 additions & 0 deletions spec/uploadcare/client/file_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ module Client
end
end

it 'supports extra params like include' do
VCR.use_cassette('rest_file_info') do
uuid = '640fe4b7-7352-42ca-8d87-0e4387957157'
file = subject.info(uuid, { include: 'appdata' })
expect(file.value![:uuid]).to eq(uuid)
expect(file.value![:appdata]).not_to be_empty
end
end

it 'shows nothing on invalid file' do
VCR.use_cassette('rest_file_info_fail') do
uuid = 'nonexistent'
Expand Down
Loading

0 comments on commit 931d8fa

Please sign in to comment.