actionback 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +93 -0
- data/lib/actionback.rb +8 -0
- data/lib/actionback/acronym.rb +5 -0
- data/lib/actionback/controller_additions.rb +16 -0
- data/lib/actionback/route_back.rb +24 -0
- data/lib/actionback/version.rb +3 -0
- data/lib/tasks/actionback_tasks.rake +4 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 848b3198c4623ac5a1b7d75f957af2e1c8c021a7
|
4
|
+
data.tar.gz: 6744b31b2d4d21f351aba3c2920c1abd9f833bd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eec5b6b825cb1ba829d5cde32ccd9e48241dbf9dc924cba5fea8374f4368e1091f79e3090bb7be53d4b1ccd0b18b3fcfb87c4959488e9a62450b13288ace4620
|
7
|
+
data.tar.gz: c35a63213f5f2334e37d6edc114aae455b829b40285c8a2a8653fe27f37180fbc68c2b7989a96e5d50c858f2eb1db330c4a9b8ffee285182efdc97044468a8db
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2014 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
actionback
|
2
|
+
==========
|
3
|
+
[![Build Status](https://travis-ci.org/sweatshirtio/actionback.svg?branch=master)](https://travis-ci.org/sweatshirtio/actionback)
|
4
|
+
|
5
|
+
`Action Pack` can now go **BACK!**
|
6
|
+
|
7
|
+
Deserialize URLs to resources or resource IDs. Action Back uses your defined routes and controllers to determine the correct resource to return. This is great for `Hypermedia APIs` and following [HATEOAS](http://en.wikipedia.org/wiki/HATEOAS) principles in your REST APIs.
|
8
|
+
|
9
|
+
Put more simply, actionback will turn
|
10
|
+
`http://api.yourapp.com/users/3` into `#<User id:3, first_name: "Bob">`.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
Add `actionback` to your Gemfile:
|
14
|
+
```ruby
|
15
|
+
gem 'actionback'
|
16
|
+
```
|
17
|
+
|
18
|
+
## Supported Ruby/Rails versions
|
19
|
+
- ruby >= 1.9.3
|
20
|
+
- rails >= 3.0
|
21
|
+
|
22
|
+
## Deserializing URLs to Resources
|
23
|
+
|
24
|
+
### Serializers
|
25
|
+
Include `ActionBack::RouteBack` in your serializer (or wherever you may want to deserialize URLs to resources). This will give the serializer the ability to `find` a resource given a `URL`. Behind the scenes, `ActionBack::RouteBack` is calling on the inferred controller to return the correct resource.
|
26
|
+
|
27
|
+
|
28
|
+
Here is an example of using actionback with [Roar-Rails](https://github.com/apotonick/roar-rails)
|
29
|
+
```ruby
|
30
|
+
# Represent user
|
31
|
+
class UserRepresenter < Representable::Decorator
|
32
|
+
include Roar::Representer::JSON
|
33
|
+
include Roar::Representer::Feature::Hypermedia
|
34
|
+
include ActionBack::RouteBack
|
35
|
+
|
36
|
+
property :name
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
# Represent awesomeness
|
41
|
+
class AwesomeRepresenter < Representable::Decorator
|
42
|
+
include Roar::Representer::JSON
|
43
|
+
include Roar::Representer::Feature::Hypermedia
|
44
|
+
|
45
|
+
# Deserialize the passed in user URL to a user instance
|
46
|
+
property :user,
|
47
|
+
decorator: UserRepresenter,
|
48
|
+
instance: lambda { |fragment, *args| fragment },
|
49
|
+
deserialize: lambda { |object, fragment, *args| object.resource_from_url(fragment) }
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
The above example expects a URL, such as `http://api.awesomeapp.com/users/3`,
|
54
|
+
to be passed in as the value for the user property. `AwesomeRepresenter` will then call on `ActionBack` with this URL for the correct User resource.
|
55
|
+
|
56
|
+
`#<User id:3 >` will now officially be related to **Awesome**! You're happy because that couldn't have been easier. `#<User id:3 >` is happy because he/she is now awesome :satisfied:. Hooray... Everyones happy :+1:
|
57
|
+
|
58
|
+
#### ActionBack::RouteBack Methods
|
59
|
+
- `#resource_from_url(url):` returns ActiveRecord model instantce
|
60
|
+
|
61
|
+
- `#id_from_url(url):` returns ActiveRecord model ID
|
62
|
+
|
63
|
+
### Controllers
|
64
|
+
`ActionBack::ControllerAdditions` adds class methods to your controller to give it the ability to return a resource/resource ID given route params.
|
65
|
+
|
66
|
+
Controllers are inferred in actionback via rails routes. They are called on to return the classified resource or resource ID. You will need to include `ActionBack::ControllerAdditions` in the relevant controllers.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class AwesomeController < ApplicationController
|
70
|
+
include ActionBack::ControllerAdditions
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
## Custom Behavior
|
75
|
+
### Controllers
|
76
|
+
It is more likely that you will want to override `ActionBack::Controller` methods.
|
77
|
+
|
78
|
+
By overriding the controller methods, you can create your own complex queries to fetch resources. You will want to do this for queries that involve more than one ID, such as for nested routes.
|
79
|
+
|
80
|
+
For example:
|
81
|
+
```ruby
|
82
|
+
class GroupUsersController < ApplicationController
|
83
|
+
include ActionBack::ControllerAdditions
|
84
|
+
|
85
|
+
def self.fetch_resource(route_params)
|
86
|
+
User.where id: route_params[:id], group_id: route_params[:group_id]
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.fetch_resource_id(route_params)
|
90
|
+
{ id: route_params[:id], group_id: route_params[:group_id] }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
data/lib/actionback.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
module ActionBack
|
2
|
+
module ControllerAdditions
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
def fetch_resource_id(route_params)
|
7
|
+
route_params[:id]
|
8
|
+
end
|
9
|
+
|
10
|
+
def fetch_resource(route_params)
|
11
|
+
resource_id = fetch_resource_id route_params
|
12
|
+
controller_name.classify.constantize.find resource_id
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module ActionBack
|
2
|
+
module RouteBack
|
3
|
+
def match_path(path)
|
4
|
+
Rails.application.routes.recognize_path path
|
5
|
+
end
|
6
|
+
|
7
|
+
def infer_controller(route_params)
|
8
|
+
# OPTIMIZE possible to do without instantiating new instance?
|
9
|
+
ActionDispatch::Routing::RouteSet::Dispatcher.new.controller route_params
|
10
|
+
end
|
11
|
+
|
12
|
+
def id_from_url(url)
|
13
|
+
match = match_path url
|
14
|
+
|
15
|
+
infer_controller(match).fetch_resource_id match
|
16
|
+
end
|
17
|
+
|
18
|
+
def resource_from_url(url)
|
19
|
+
match = match_path url
|
20
|
+
|
21
|
+
infer_controller(match).fetch_resource match
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: actionback
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ari Summer
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.1.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Deserialize URLs to resources or resource IDs. Great for Hypermedia APIs.
|
56
|
+
email:
|
57
|
+
- aribsummer@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- MIT-LICENSE
|
63
|
+
- README.md
|
64
|
+
- lib/actionback.rb
|
65
|
+
- lib/actionback/acronym.rb
|
66
|
+
- lib/actionback/controller_additions.rb
|
67
|
+
- lib/actionback/route_back.rb
|
68
|
+
- lib/actionback/version.rb
|
69
|
+
- lib/tasks/actionback_tasks.rake
|
70
|
+
homepage: https://github.com/sweatshirtio/actionback
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: actionpack with back
|
94
|
+
test_files: []
|