Skip to content

Commit

Permalink
Lots of style fixe for Rubopcop
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulnsward committed Jan 13, 2025
1 parent fdfa4e4 commit ccf41eb
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/uploadcare/clients/file_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def delete(uuid)
# @param uuid [String] The UUID of the file
# @return [Hash] The response body containing the file details
# @see https://uploadcare.com/api-refs/rest-api/v0.7.0/#tag/File/operation/fileInfo
def info(uuid, params= {})
def info(uuid, params = {})
get("/files/#{uuid}/", params)
end

Expand Down
18 changes: 15 additions & 3 deletions lib/uploadcare/clients/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,23 @@ def del(path, params = {}, headers = {})

def prepare_request(req, method, path, params, headers)
upcase_method_name = method.to_s.upcase
uri = params.is_a?(Hash) ? build_uri(path, params) : path
req.headers.merge!(authenticator.headers(upcase_method_name, uri))
uri = build_request_uri(path, params)

prepare_headers(req, upcase_method_name, uri, headers)
prepare_body_or_params(req, upcase_method_name, params)
end

def build_request_uri(path, params)
params.is_a?(Hash) ? build_uri(path, params) : path
end

def prepare_headers(req, method, uri, headers)
req.headers.merge!(authenticator.headers(method, uri))
req.headers.merge!(headers)
end

if upcase_method_name == 'GET'
def prepare_body_or_params(req, method, params)
if method == 'GET'
req.params.update(params) unless params.empty?
else
req.body = params.to_json unless params.empty?
Expand Down
39 changes: 27 additions & 12 deletions lib/uploadcare/resources/paginated_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
module Uploadcare
class PaginatedCollection
include Enumerable

attr_reader :resources, :next_page_url, :previous_page_url, :per_page, :total, :client, :resource_class

def initialize(resources:, next_page:, previous_page:, per_page:, total:, client:, resource_class:)
@resources = resources
@next_page_url = next_page
@previous_page_url = previous_page
@per_page = per_page
@total = total
@client = client
@resource_class = resource_class
def initialize(params = {})
@resources = params[:resources]
@next_page_url = params[:next_page]
@previous_page_url = params[:previous_page]
@per_page = params[:per_page]
@total = params[:total]
@client = params[:client]
@resource_class = params[:resource_class]
end

def each(&block)
Expand Down Expand Up @@ -43,10 +42,22 @@ def previous_page
def fetch_page(page_url)
return nil unless page_url

params = extract_params_from_url(page_url)
response = fetch_response(params)
build_paginated_collection(response)
end

def extract_params_from_url(page_url)
uri = URI.parse(page_url)
params = URI.decode_www_form(uri.query.to_s).to_h
response = client.list(params)
new_resources = response['results'].map { |resource_data| resource_class.new(resource_data, client.config) }
URI.decode_www_form(uri.query.to_s).to_h
end

def fetch_response(params)
client.list(params)
end

def build_paginated_collection(response)
new_resources = build_resources(response['results'])

self.class.new(
resources: new_resources,
Expand All @@ -58,5 +69,9 @@ def fetch_page(page_url)
resource_class: resource_class
)
end

def build_resources(results)
results.map { |resource_data| resource_class.new(resource_data, client.config) }
end
end
end
2 changes: 1 addition & 1 deletion spec/uploadcare/clients/webhook_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
it 'deletes the webhook successfully' do
VCR.use_cassette('rest_webhook_destroy') do
expect { subject.delete_webhook(target_url) }.not_to raise_error
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/uploadcare/resources/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
describe '#info' do
subject { file.info }
before do
allow_any_instance_of(Uploadcare::FileClient).to receive(:get).with("/files/#{uuid}/").and_return(response_body)
allow_any_instance_of(Uploadcare::FileClient).to receive(:get).with("/files/#{uuid}/", {}).and_return(response_body)
end

it { is_expected.to be_a(Uploadcare::File) }
Expand Down
2 changes: 1 addition & 1 deletion uploadcare-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 3.0'

spec.add_dependency 'faraday', '~> 2.12'
spec.add_dependency 'zeitwerk', '~> 2.7'
spec.add_dependency 'zeitwerk', '~> 2.6.18'
end

0 comments on commit ccf41eb

Please sign in to comment.