compose_url 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/compose_url.rb +66 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4f02e1ca714b06f3162a64ccc1aad00263342ae5
|
4
|
+
data.tar.gz: a02d63d16c71d7519c1379fb986cf9f1003a0e1e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 60b15d22131abe5877144faaffdae86a8f8fac04820aef93d299f39429bf616e804cf4bbdc7d031cc26dbba6ccb88df9d6f7ee5ac6f0653b63f9d5eb394bcf20
|
7
|
+
data.tar.gz: 94d78cbcfaefd8baf1e98e18fe153507f4feee202cccae8baf122dbdfe7f8ab9fa86769e7ce762218a3e0aa53d2adc8bf8c8e6be1692c2c3846fd048a117dc3f
|
data/lib/compose_url.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
class ComposeURL
|
5
|
+
attr_accessor :params
|
6
|
+
|
7
|
+
def initialize(baseurl, params={})
|
8
|
+
@url = baseurl
|
9
|
+
@params = params
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_param(k, v)
|
13
|
+
@params[k] = v
|
14
|
+
end
|
15
|
+
|
16
|
+
def remove_param(k)
|
17
|
+
@params.delete(k)
|
18
|
+
end
|
19
|
+
|
20
|
+
def url
|
21
|
+
validate_url(build_url)
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
url
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# todo: is this a valid key?
|
31
|
+
def validate_key(k)
|
32
|
+
raise ComposeURLError.new('Invalid key') unless k == escape(k) # key shouldn't require escaping, or should it?
|
33
|
+
escape(k)
|
34
|
+
end
|
35
|
+
|
36
|
+
# todo: is this a valid value?
|
37
|
+
def validate_value(v)
|
38
|
+
escape(v)
|
39
|
+
end
|
40
|
+
|
41
|
+
def validate_url(url)
|
42
|
+
raise ComposeURLError.new('Resulting URL is invalid') unless url =~ /\A#{URI::regexp}\z/
|
43
|
+
url
|
44
|
+
end
|
45
|
+
|
46
|
+
def params_string
|
47
|
+
@params.map { |k,v|
|
48
|
+
"#{validate_key(k)}=#{validate_value(v)}"
|
49
|
+
}.join('&')
|
50
|
+
end
|
51
|
+
|
52
|
+
def escape(s)
|
53
|
+
CGI.escape(s)
|
54
|
+
end
|
55
|
+
|
56
|
+
def build_url
|
57
|
+
@params.length == 0 ? "#{@url}" : "#{@url}?#{params_string}"
|
58
|
+
end
|
59
|
+
|
60
|
+
# todo: support arrays
|
61
|
+
# todo: support hash
|
62
|
+
end
|
63
|
+
|
64
|
+
class ComposeURLError < StandardError
|
65
|
+
# todo: custom error handling
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: compose_url
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Burnette
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A Ruby class that helps you compose a URL string with query parameters
|
14
|
+
without all the fuss.
|
15
|
+
email: ryan.burnette@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/compose_url.rb
|
21
|
+
homepage: https://github.com/ryanburnette/compose_url
|
22
|
+
licenses:
|
23
|
+
- Apache2
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.2.2
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Helps you compose a URL.
|
45
|
+
test_files: []
|