Skip to content

Commit

Permalink
Add method "Discordrb::API.uri_with_query".
Browse files Browse the repository at this point in the history
  • Loading branch information
expeehaa committed Oct 15, 2024
1 parent e3635c6 commit ed3bd5a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/discordrb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ def mutex_wait(mutex)
mutex.unlock
end

# Add query parameters to a URI string.
# @param uri_string [String] base URI
# @param query [Hash, nil] Hash of query parameters
# @return [String] `uri_string` with the query appended, if `query` contained non-nil items, otherwise `uri_string`
def uri_with_query(uri_string, query)
URI.parse(uri_string).tap do |uri|
URI.encode_www_form((query || {}).compact).tap do |query_string|
unless query_string.empty?
if uri.query && !uri.query.empty?
uri.query += "&#{query_string}"
else
uri.query = query_string
end
end
end
end.to_s
end

# Performs a RestClient request.
# @param type [Symbol] The type of HTTP request to use.
# @param attributes [Array] The attributes for the request.
Expand Down
37 changes: 37 additions & 0 deletions spec/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'discordrb'

describe Discordrb::API do
describe '.uri_with_query' do
it 'requires a String as uri_string' do
expect { Discordrb::API.uri_with_query(5, nil) }.to raise_error URI::InvalidURIError, 'bad URI(is not URI?): 5'
expect { Discordrb::API.uri_with_query(:test, nil) }.to raise_error URI::InvalidURIError, 'bad URI(is not URI?): :test'
expect(Discordrb::API.uri_with_query('path', nil)).to eq 'path'
end

it 'does not transform uri_string if no or nil-valued query parameters are provided' do
[
nil,
{},
{ test: nil }
].each do |query|
expect(Discordrb::API.uri_with_query('path', query)).to eq 'path'
expect(Discordrb::API.uri_with_query('/path', query)).to eq '/path'
expect(Discordrb::API.uri_with_query('/path?', query)).to eq '/path?'
expect(Discordrb::API.uri_with_query('/path?asdf', query)).to eq '/path?asdf'
expect(Discordrb::API.uri_with_query('https://discord.com/path?asdf', query)).to eq 'https://discord.com/path?asdf'
end
end

it 'appends serialized query parameters to uri_string' do
expect(Discordrb::API.uri_with_query('path', { test: 123 })).to eq 'path?test=123'
expect(Discordrb::API.uri_with_query('/path', { test: 123 })).to eq '/path?test=123'
expect(Discordrb::API.uri_with_query('/path?', { test: 123 })).to eq '/path?test=123'
expect(Discordrb::API.uri_with_query('/path?asdf', { test: 123 })).to eq '/path?asdf&test=123'
expect(Discordrb::API.uri_with_query('https://discord.com/path?asdf', { test: 123 })).to eq 'https://discord.com/path?asdf&test=123'
expect(Discordrb::API.uri_with_query('https://discord.com/path?asdf', { test!: 123 })).to eq 'https://discord.com/path?asdf&test%21=123'
expect(Discordrb::API.uri_with_query('https://discord.com/path?asdf', { test?: 123 })).to eq 'https://discord.com/path?asdf&test%3F=123'
end
end
end

0 comments on commit ed3bd5a

Please sign in to comment.