compose_url
compose_url is a Ruby class that abstracts away the details composing a URL with query paramters.
Usage
Instantiate compose_url
by passing a base_url
.
url = ComposeURL.new('http://api.com/1/a')
Include some initial query parameters either in the initial base_url
or as a Hash. Or both at the same time.
url = ComposeURL.new('http://api.com/1/a', { 'foo' => 'bar', 'baz' => 'grault' })
url = ComposeURL.new('http://api.com/1/a?foo=bar&baz=grault')
url = ComposeURL.new('http://api.com/1/a?foo=bar', { 'baz' => 'grault' })
Add additional params to the resulting ComposeURL
object.
url.add_param('a', 'b')
The @params
Hash is accessible using Ruby’s attr_accessor
, so it can be treated directly as a Hash.
url.params['c'] = 'd'
url.params.merge!({ 'e' => 'f' })
Remove a key value pair by calling remove_param
on the key.
url.remove_param('a')
Retrieve your composed URL string by calling url
or to_s
.
url.to_s
Installation
Install as a gem.
gem install compose_url
Or add to your Gemfile
and bundle install
.
# Gemfile
gem "compose_url"
bundle install
Todo
- Support URL hash as it’s described as Relative Reference in RFC 3986
- Validate keys and values according to application/x-www-form-urlencoded
- Improve validation based on suggestions to this Stack Overflow question
- Improve custom error class
- Self-contained testing
History
-
v0.1.3 2014-12-20
- Define dependencies in Gemspec
-
v0.1.2 2014-12-02
- Fix bug where params could be escaped twice
- Rely more on existing Ruby libraries
-
v0.1.1 2014-12-01
- Parse out params in the initial
base_url
and add them to the@params
hash
- Parse out params in the initial
-
v0.1.0 2014-12-01
- Initial release
Contributing
Open an Issue or bring a Rull Request!