rtopia 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/{README.rdoc → README.markdown} +8 -19
- data/VERSION +1 -1
- data/lib/rtopia.rb +31 -9
- data/rtopia.gemspec +4 -4
- data/test/test_rtopia.rb +24 -0
- metadata +5 -5
data/.gitignore
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
-
|
1
|
+
Rtopia
|
2
|
+
======
|
2
3
|
|
3
|
-
A very simple, but useful route generation helper for use anytime, anywhere.
|
4
|
+
A very simple, but useful route generation helper for use anytime, anywhere.
|
5
|
+
See documentation at [http://labs.sinefunc.com/rtopia/doc](http://labs.sinefunc.com/rtopia/doc).
|
4
6
|
|
5
|
-
|
7
|
+
Assumptions
|
8
|
+
-----------
|
6
9
|
|
7
10
|
It checks for a `to_param`, and if Ruby1.9 or greater is used, checks for an `id`. Last fallback uses `to_s`.
|
8
11
|
|
9
|
-
|
12
|
+
Examples
|
13
|
+
--------
|
10
14
|
|
11
15
|
include Rtopia
|
12
16
|
|
@@ -24,18 +28,3 @@ It checks for a `to_param`, and if Ruby1.9 or greater is used, checks for an `id
|
|
24
28
|
@entry = Entry.create # has an id of 1001 for example
|
25
29
|
R(@entry) # => '/1001'
|
26
30
|
R(:entry, @entry) # => '/entry/1001'
|
27
|
-
|
28
|
-
|
29
|
-
== Note on Patches/Pull Requests
|
30
|
-
|
31
|
-
* Fork the project.
|
32
|
-
* Make your feature addition or bug fix.
|
33
|
-
* Add tests for it. This is important so I don't break it in a
|
34
|
-
future version unintentionally.
|
35
|
-
* Commit, do not mess with rakefile, version, or history.
|
36
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
37
|
-
* Send me a pull request. Bonus points for topic branches.
|
38
|
-
|
39
|
-
== Copyright
|
40
|
-
|
41
|
-
Copyright (c) 2010 Sinefunc. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/lib/rtopia.rb
CHANGED
@@ -2,10 +2,12 @@ require 'uri'
|
|
2
2
|
require 'cgi'
|
3
3
|
|
4
4
|
module Rtopia
|
5
|
+
VERSION = "0.2.3"
|
6
|
+
|
5
7
|
# In order to be able to do `Rtopia::R`, Rtopia needs to extend itself
|
6
8
|
extend self
|
7
9
|
|
8
|
-
#
|
10
|
+
# @example
|
9
11
|
#
|
10
12
|
# R(:items) # => /items
|
11
13
|
#
|
@@ -24,19 +26,39 @@ module Rtopia
|
|
24
26
|
# R(:user => { :lname => 'Doe', :fname => 'John' })
|
25
27
|
# => '?user[lname]=Doe&user[fname]=John'
|
26
28
|
#
|
29
|
+
# R("http://google.com/search", :q => "Robots")
|
30
|
+
# => 'http://google.com/search?q=Robots'
|
31
|
+
#
|
32
|
+
# @overload R()
|
33
|
+
# @return [String] returns the root '/'
|
34
|
+
# @overload R(:arg1, :arg2, ..., :argN)
|
35
|
+
# @return [String] all args joined on '/' prefixed with '/'
|
36
|
+
# @overload R(:arg1, :arg2, ..., :argN, hash)
|
37
|
+
# @param [#to_param] arg1 any object checked for #to_param, #id, and #to_s
|
38
|
+
# @param [#to_param] arg2 any object checked for #to_param, #id, and #to_s
|
39
|
+
# @param [#to_param] argN any object checked for #to_param, #id, and #to_s
|
40
|
+
# @param [Hash] hash to be represented as a query string.
|
41
|
+
# @return [String] all args joined on '/' with the hash represented
|
42
|
+
# as a query string.
|
43
|
+
# @overload R(hash)
|
44
|
+
# @param [Hash] hash to be represented as a query string.
|
45
|
+
# @return [String] the hash represented as a query string, prefix with a `?`
|
27
46
|
def R(*args)
|
28
47
|
hash = args.last.is_a?(Hash) ? args.pop : {}
|
29
48
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
49
|
+
return '?' + query_string(hash) if hash.any? and args.empty?
|
50
|
+
|
51
|
+
host_with_port = args.shift if args.first =~ /^(https?|ftp)/
|
52
|
+
|
53
|
+
path = args.unshift('/').map { |arg| to_param(arg) }.join('/').squeeze('/')
|
54
|
+
|
55
|
+
path.tap do |ret|
|
35
56
|
if hash.any?
|
36
|
-
|
37
|
-
|
57
|
+
ret.gsub!(/^\//, '') if ret == '/'
|
58
|
+
ret << '?'
|
59
|
+
ret << query_string(hash)
|
38
60
|
end
|
39
|
-
|
61
|
+
ret.insert(0, host_with_port.gsub(/\/$/, '')) if host_with_port
|
40
62
|
end
|
41
63
|
end
|
42
64
|
|
data/rtopia.gemspec
CHANGED
@@ -5,22 +5,22 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rtopia}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Sinefunc"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-05-25}
|
13
13
|
s.description = %q{For use anywhere you have objects with to_params, ids, or just to_s's}
|
14
14
|
s.email = %q{sinefunc@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.
|
17
|
+
"README.markdown"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
22
|
"LICENSE",
|
23
|
-
"README.
|
23
|
+
"README.markdown",
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"lib/rtopia.rb",
|
data/test/test_rtopia.rb
CHANGED
@@ -78,4 +78,28 @@ class TestRtopia < Test::Unit::TestCase
|
|
78
78
|
assert_equal '/users?user%5Blname%5D=Doe&user%5Bfname%5D=John',
|
79
79
|
R(:users, :user => { :lname => 'Doe', :fname => 'John' })
|
80
80
|
end
|
81
|
+
|
82
|
+
def test_Rtopia_R_of_string_prefixed_with_slash
|
83
|
+
assert_equal '/the-right-path', R('/the-right-path')
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_Rtopia_R_of_string_prefix_with_http
|
87
|
+
assert_equal 'http://test.host/arg1/arg2',
|
88
|
+
R('http://test.host', :arg1, :arg2)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_Rtopia_R_of_string_prefix_with_http_and_hash
|
92
|
+
assert_equal 'http://test.host/arg1/arg2?key1=value1&key2=value2',
|
93
|
+
R('http://test.host', :arg1, :arg2, :key1 => :value1, :key2 => :value2)
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_Rtopia_R_of_string_prefix_with_http_and_hash_and_suffixed
|
97
|
+
assert_equal 'http://test.host/arg1/arg2?key1=value1&key2=value2',
|
98
|
+
R('http://test.host/', :arg1, :arg2, :key1 => :value1, :key2 => :value2)
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_facebook_oauth_example
|
102
|
+
assert_equal "https://graph.facebook.com/oauth/authorize?client_id=123",
|
103
|
+
R('https://graph.facebook.com/oauth/authorize', :client_id => '123')
|
104
|
+
end
|
81
105
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 3
|
9
|
+
version: 0.2.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Sinefunc
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-25 00:00:00 +08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -26,12 +26,12 @@ extensions: []
|
|
26
26
|
|
27
27
|
extra_rdoc_files:
|
28
28
|
- LICENSE
|
29
|
-
- README.
|
29
|
+
- README.markdown
|
30
30
|
files:
|
31
31
|
- .document
|
32
32
|
- .gitignore
|
33
33
|
- LICENSE
|
34
|
-
- README.
|
34
|
+
- README.markdown
|
35
35
|
- Rakefile
|
36
36
|
- VERSION
|
37
37
|
- lib/rtopia.rb
|