api_url_generator 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/.gitignore +14 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +12 -0
- data/api_url_generator.gemspec +28 -0
- data/lib/api_url_generator.rb +50 -0
- data/lib/api_url_generator/version.rb +3 -0
- data/lib/generators/install/install_generator.rb +0 -0
- data/lib/generators/install/templates/api_url_generator.rb.erb +0 -0
- data/test/api_url_generator_test.rb +102 -0
- data/test/models/generic_class.rb +18 -0
- data/test/models/generic_polymorphic_klass.rb +19 -0
- data/test/test_helper.rb +9 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ae092bf15142e19965454ff055d161cb539b92ef
|
4
|
+
data.tar.gz: 2e94fdd761fe31182c71befcd2e97af5cd5e0995
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ca0963fe647a7aeeea388c982445ec906f3e92528de1d86a8cb3a4b1aba62e3f2c0f6dc07018a817f721d39f953b1e90605fc9e23893654e30e50dd7ca161e0
|
7
|
+
data.tar.gz: 61cb7afe695ea3652b81645aa62bc7c895d600c56a1403ecc37ba55b41e20a85b4d3551c8af4a8ec282f434d50825c3479eb85a7178b9fa1bb2c3e9d0544783e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 M. A. Owens
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# API URL Generator
|
2
|
+
|
3
|
+
After writing a few RESTful APIs with Rails, I was always running into issues with generating URL for my resources. Either, I would try to force `url_for` or `path_to` to work, but it was never ideal. Since, I couldn't find anyother solutions, I decide to write my own. This gem helps create urls for your RESTful API to make it easier for client applications to navigate without using `ActionView::Helpers`
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'api_url_generator'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install api_url_generator
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/api-url-generator/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'api_url_generator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "api_url_generator"
|
8
|
+
spec.version = APIURLGenerator::VERSION
|
9
|
+
spec.authors = ["M. A. Owens"]
|
10
|
+
spec.email = ["matt@cremalab.com"]
|
11
|
+
spec.summary = %q{
|
12
|
+
After writing a few RESTful APIs with Rails, I was always running into issues with generating URL for my resources.
|
13
|
+
Either, I would try to force `url_for` or `path_to` to work, but it was never ideal.
|
14
|
+
Since, I couldn't find any other solutions, I decide to write my own.
|
15
|
+
This gem helps create urls for your RESTful API to make it easier for client applications to navigate without using `ActionView::Helpers`
|
16
|
+
}
|
17
|
+
spec.description = %q{}
|
18
|
+
spec.homepage = ""
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0")
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "api_url_generator/version"
|
2
|
+
|
3
|
+
module APIURLGenerator
|
4
|
+
@@url
|
5
|
+
|
6
|
+
def self.url(x)
|
7
|
+
@@url = x
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.get_url
|
11
|
+
@@url
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.dectect_polymorphic(object)
|
15
|
+
object.instance_variables.each do |variable|
|
16
|
+
if variable.to_s[-9..-1] == "able_type"
|
17
|
+
return {
|
18
|
+
name: {variable.to_s[1..-1].to_sym => object.instance_eval(variable.to_s)},
|
19
|
+
id: {(variable.to_s[1..-5] + "id").to_sym => object.instance_eval(variable.to_s[1..-5] + "id")}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
return false
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def self.generate_url(object, nest = nil)
|
30
|
+
polymorphous = dectect_polymorphic(object)
|
31
|
+
if polymorphous && nest
|
32
|
+
nested_id = "/#{object.instance_eval(polymorphous[:name].keys[0].to_s).tableize.downcase}/#{object.instance_eval("#{polymorphous[:id].keys[0]}")}"
|
33
|
+
object_id = "/#{object.class.to_s.tableize.downcase}/#{object.id}"
|
34
|
+
if object.instance_eval(polymorphous[:name].keys[0].to_s).downcase != nest
|
35
|
+
raise ArgumentError, "Invalid nest param"
|
36
|
+
end
|
37
|
+
return "#{@@url}#{nested_id}#{object_id}"
|
38
|
+
elsif nest
|
39
|
+
begin
|
40
|
+
nested_id = "/#{nest.pluralize}/#{object.instance_eval("#{nest}_id")}"
|
41
|
+
object_id = "/#{object.class.to_s.tableize.downcase}/#{object.id}"
|
42
|
+
return "#{@@url}#{nested_id}#{object_id}"
|
43
|
+
rescue NameError
|
44
|
+
raise ArgumentError, "Invalid nest param"
|
45
|
+
end
|
46
|
+
else
|
47
|
+
return "#{@@url}/#{object.class.to_s.tableize.downcase}/#{object.id}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
|
4
|
+
module APIURLGenerator
|
5
|
+
class APIURLGeneratorTest < Minitest::Test
|
6
|
+
def test_url_generator
|
7
|
+
object = Generic.new({
|
8
|
+
id: 5,
|
9
|
+
some_attribute: "This"
|
10
|
+
})
|
11
|
+
assert_equal "0.0.0.0:5000/generics/#{object.id}",
|
12
|
+
APIURLGenerator.generate_url(object)
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_url_generator_polymorphic
|
17
|
+
object = GenericPolymorphic.new({
|
18
|
+
polymorphicable_type: "Project",
|
19
|
+
polymorphicable_id: 22,
|
20
|
+
id: 33
|
21
|
+
})
|
22
|
+
assert_equal "0.0.0.0:5000/generic_polymorphics/#{object.id}",
|
23
|
+
APIURLGenerator.generate_url(object)
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def test_url_generator_nest
|
29
|
+
object = Generic.new({
|
30
|
+
id: 5,
|
31
|
+
some_attribute: "This",
|
32
|
+
project_id: 5
|
33
|
+
})
|
34
|
+
|
35
|
+
assert_equal "0.0.0.0:5000/projects/#{object.project_id}/generics/#{object.id}",
|
36
|
+
APIURLGenerator.generate_url(object, "project")
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_url_generator_nest_not_nested
|
40
|
+
object = Generic.new({
|
41
|
+
id: 5,
|
42
|
+
some_attribute: "This"
|
43
|
+
})
|
44
|
+
|
45
|
+
assert_raises(ArgumentError) do
|
46
|
+
APIURLGenerator.generate_url(object, "project_mm")
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def test_polymorphous
|
53
|
+
object = GenericPolymorphic.new({
|
54
|
+
polymorphicable_type: "Project",
|
55
|
+
polymorphicable_id: 22,
|
56
|
+
id: 44
|
57
|
+
|
58
|
+
})
|
59
|
+
assert_equal "0.0.0.0:5000/projects/#{object.polymorphicable_id}/generic_polymorphics/#{object.id}",
|
60
|
+
APIURLGenerator.generate_url(object, "project")
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_polymorphous_error
|
64
|
+
object = GenericPolymorphic.new({
|
65
|
+
polymorphicable_type: "Project",
|
66
|
+
polymorphicable_id: 22,
|
67
|
+
id: 44
|
68
|
+
|
69
|
+
})
|
70
|
+
assert_raises(ArgumentError) do
|
71
|
+
p APIURLGenerator.generate_url(object, "projectadfsd")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
def test_dectect_polymorphic_true
|
77
|
+
object = GenericPolymorphic.new({
|
78
|
+
polymorphicable_type: "Project",
|
79
|
+
polymorphicable_id: 22,
|
80
|
+
id: 44
|
81
|
+
})
|
82
|
+
|
83
|
+
response = {
|
84
|
+
name: {polymorphicable_type: "Project"},
|
85
|
+
id: {polymorphicable_id: 22}
|
86
|
+
}
|
87
|
+
|
88
|
+
assert_equal response, APIURLGenerator.dectect_polymorphic(object)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_dectect_polymorphic_false
|
92
|
+
object = Generic.new({
|
93
|
+
id: 5,
|
94
|
+
some_attribute: "This",
|
95
|
+
project_id: 5
|
96
|
+
})
|
97
|
+
|
98
|
+
refute APIURLGenerator.dectect_polymorphic(object)
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
class Generic
|
4
|
+
extend ActiveModel::Naming
|
5
|
+
extend ActiveModel::Translation
|
6
|
+
|
7
|
+
include ActiveModel::Validations
|
8
|
+
include ActiveModel::Conversion
|
9
|
+
|
10
|
+
attr_accessor :id, :some_attribute, :project_id
|
11
|
+
|
12
|
+
def initialize(attributes = {})
|
13
|
+
self.id = attributes[:id]
|
14
|
+
self.some_attribute = attributes[:some_attribute]
|
15
|
+
self.project_id = attributes[:project_id]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
class GenericPolymorphic
|
4
|
+
extend ActiveModel::Naming
|
5
|
+
extend ActiveModel::Translation
|
6
|
+
|
7
|
+
include ActiveModel::Validations
|
8
|
+
include ActiveModel::Conversion
|
9
|
+
|
10
|
+
attr_accessor :id, :some_attribute, :polymorphicable_type, :polymorphicable_id
|
11
|
+
|
12
|
+
def initialize(attributes)
|
13
|
+
self.id = attributes[:id]
|
14
|
+
self.some_attribute = attributes[:some_attribute]
|
15
|
+
self.polymorphicable_type = attributes[:polymorphicable_type]
|
16
|
+
self.polymorphicable_id = attributes[:polymorphicable_id]
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_url_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- M. A. Owens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: ''
|
42
|
+
email:
|
43
|
+
- matt@cremalab.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- api_url_generator.gemspec
|
54
|
+
- lib/api_url_generator.rb
|
55
|
+
- lib/api_url_generator/version.rb
|
56
|
+
- lib/generators/install/install_generator.rb
|
57
|
+
- lib/generators/install/templates/api_url_generator.rb.erb
|
58
|
+
- test/api_url_generator_test.rb
|
59
|
+
- test/models/generic_class.rb
|
60
|
+
- test/models/generic_polymorphic_klass.rb
|
61
|
+
- test/test_helper.rb
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.5
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: After writing a few RESTful APIs with Rails, I was always running into issues
|
86
|
+
with generating URL for my resources. Either, I would try to force `url_for` or
|
87
|
+
`path_to` to work, but it was never ideal. Since, I couldn't find any other solutions,
|
88
|
+
I decide to write my own. This gem helps create urls for your RESTful API to make
|
89
|
+
it easier for client applications to navigate without using `ActionView::Helpers`
|
90
|
+
test_files:
|
91
|
+
- test/api_url_generator_test.rb
|
92
|
+
- test/models/generic_class.rb
|
93
|
+
- test/models/generic_polymorphic_klass.rb
|
94
|
+
- test/test_helper.rb
|