responsible 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/Gemfile +2 -4
- data/README.md +129 -3
- data/lib/consumer.rb +2 -1
- data/lib/responsible.rb +2 -0
- data/responsible.gemspec +3 -2
- data/spec/consumer_spec.rb +36 -0
- metadata +27 -12
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
NDNkMGQ5NjRlMjhlYTM4ODEwNWY4NGRhNzMyM2YzMGQ3OTE4YTFiOQ==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb9458a07f1290d5222ff96bf060736d2324b849
|
4
|
+
data.tar.gz: d3675f0d6cac094f51f3b1dde2a3f4348cc3f374
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
NzAzMTYzODVhY2M5YmI0MTY0ZmM4OTA2NjE1ZGEwMjc1Mzc0YjY5OTEwNGVj
|
11
|
-
NjYyOGE4OWI0MDEzYzQ4NTc0YTg0NWZjOWY2Y2FlNGU2ZGRhNjc=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
OTM3MTljZDdkOWMyMTM0NjU1NmI2Mzg1N2JjOGUyOTY3MjM3Y2YzMjYzMDUw
|
14
|
-
YmRlZjBjMDY3OTViNjMxYzdmODFhYzU0OTQzNzYzMTVjN2I0MTQzNmI2YmM4
|
15
|
-
OTUyMGVhMjc2NDhlY2IwOGFmOTlmNzgwOGM0NjczODFhNzdiNjE=
|
6
|
+
metadata.gz: 9452b791b9165bb2c526659b705ddde6fbcdf1fe68c399bc05af2b2cc2d1b1c7d7c1d5784541b4d69d1dbd693b1c0d7ca121082920fd8e7717cc010543907ee6
|
7
|
+
data.tar.gz: 79235cf791daff1e156f44993df1f770fdc45fec06003f4a1420025caaa3fe7faa25bb5e0b2c41b9765cb5138a60a8f4ecb9a556fabddc673ccc890e4c92e5ba
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,130 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Responsible - A Generic Response Builder
|
2
|
+
|
3
|
+
This is a alternative to [ActiveModel::Serializer](https://github.com/rails-api/active_model_serializers). Why you may ask, well when we first built this we did not know about it, any then when we saw how they it had been implemented we decided it had too much magic.
|
4
|
+
|
5
|
+
So doesn't Responsible have a lot of magic I hear you ask. And the answer is yes but the magic is clearly defined, with the user needed to explicity set all input, have a look and see what I mean.
|
6
|
+
|
7
|
+
## Responsible Base
|
8
|
+
|
9
|
+
This is the main class used for declaring what the generated JSON will look like.
|
10
|
+
|
11
|
+
```
|
12
|
+
require 'json'
|
13
|
+
class MySerializer < Responsible::Base
|
14
|
+
# data_object_name :number
|
15
|
+
|
16
|
+
property :value_1, delegate: true
|
17
|
+
property :value_2
|
18
|
+
|
19
|
+
def value_2
|
20
|
+
# number.other[:value]
|
21
|
+
data.other[:value]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Number
|
26
|
+
attr_accessor :other
|
27
|
+
|
28
|
+
def initialize(other)
|
29
|
+
@other = other
|
30
|
+
end
|
31
|
+
|
32
|
+
def value_1
|
33
|
+
'one'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
data = Number.new(value: 'two')
|
38
|
+
consumer = Responsible::Consumer.new
|
39
|
+
|
40
|
+
MySerializer.new(consumer, data).to_json =>
|
41
|
+
|
42
|
+
{
|
43
|
+
value_1: 'one',
|
44
|
+
value_2: 'two'
|
45
|
+
}
|
46
|
+
```
|
47
|
+
|
48
|
+
### Initialization
|
49
|
+
|
50
|
+
It is initialized using a consumer (see below) and a data object that the JSON will be generated from.
|
51
|
+
|
52
|
+
### data_object_name
|
53
|
+
|
54
|
+
#### Pending code merge
|
55
|
+
This is a convenience method and allow you to specify an access name for the data object you passed into the initializer (you can also access this object using the ```data``` method)
|
56
|
+
|
57
|
+
### property
|
58
|
+
|
59
|
+
This is how you add items to the json output. Items are added in the order they are declared and have a number of options that can be set.
|
60
|
+
|
61
|
+
#### delegate
|
62
|
+
|
63
|
+
If set to true the property be automatically read from the data object that was passed in.
|
64
|
+
|
65
|
+
#### to
|
66
|
+
|
67
|
+
This should be used with delegate if for any reason the method on the object does not directly match the name you want to use in the json output
|
68
|
+
|
69
|
+
i.e.
|
70
|
+
|
71
|
+
```
|
72
|
+
property :is_king, delegate: true, to: :is_king?
|
73
|
+
```
|
74
|
+
|
75
|
+
#### restrict_to
|
76
|
+
|
77
|
+
Used in conjunction with the consumer (see below) to determine if the property should be included in the json output at runtime. This allows a single Responsible builder to be able to output different json data based on a users permissions.
|
78
|
+
|
79
|
+
|
80
|
+
## Consumer
|
81
|
+
|
82
|
+
```
|
83
|
+
require 'json'
|
84
|
+
class RestrictedSerializer < Responsible::Base
|
85
|
+
property :always,delegate: true
|
86
|
+
property :two, delegate: true, restrict_to: [:even, :prime]
|
87
|
+
property :three, delegate: true, restrict_to: :prime
|
88
|
+
property :four, delegate: true, restrict_to: :even
|
89
|
+
end
|
90
|
+
|
91
|
+
class Number
|
92
|
+
def always; 'here'; end
|
93
|
+
def two; 2; end
|
94
|
+
def three; 3; end
|
95
|
+
def four; 4; end
|
96
|
+
end
|
97
|
+
|
98
|
+
number = Number.new
|
99
|
+
|
100
|
+
even_consumer = Responsible::Consumer.new(:even)
|
101
|
+
RestrictedSerializer.new(even_consumer, number).to_json
|
102
|
+
# => {always: 'here', two: 2, four: 4}
|
103
|
+
|
104
|
+
prime_consumer = Responsible::Consumer.new(:prime)
|
105
|
+
RestrictedSerializer.new(prime_consumer, number).to_json
|
106
|
+
# => {always: 'here', two: 2, three: 3}
|
107
|
+
|
108
|
+
all_permission_consumer = Responsible::Consumer.new(:prime, :even)
|
109
|
+
RestrictedSerializer.new(all_permission_consumer, number).to_json
|
110
|
+
# => {always: 'here', two: 2, three: 3, four: 4}
|
111
|
+
|
112
|
+
no_permission_consumer = Responsible::Consumer.new
|
113
|
+
RestrictedSerializer.new(no_permission_consumer, number).to_json
|
114
|
+
# => {always: 'here'}
|
115
|
+
```
|
116
|
+
|
117
|
+
The consumer object is responsible for handling permissioning within the system. The base consumer object that is supplied with the gem can be initialized with a list of restrictions that the currently user can see, when passed into a Responsible class, this will limit the properties that are output to JSON to those which either:
|
118
|
+
|
119
|
+
* have no restrictions
|
120
|
+
* Are restricted to one of the values supplied to the consumer object on creation.
|
121
|
+
|
122
|
+
### Custom Consumers
|
123
|
+
|
124
|
+
Please not that the Consumer class is here as a starting point only and that in a production system we would expect a more complicated set of rules to be required. As such do not limit yourself to the sets of restriction functionality outlined here if it does not meet your usecase
|
125
|
+
|
126
|
+
### Release notes:
|
127
|
+
|
128
|
+
0.0.3
|
129
|
+
Ensure that property is always included when no restrictions are specificed.
|
3
130
|
|
4
|
-
Generic Response Builder
|
data/lib/consumer.rb
CHANGED
data/lib/responsible.rb
CHANGED
data/responsible.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.unshift 'lib'
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.platform = Gem::Platform::RUBY
|
5
5
|
s.name = "responsible"
|
6
|
-
s.version = "0.0.
|
6
|
+
s.version = "0.0.3"
|
7
7
|
s.date = Time.now.strftime('%Y-%m-%d')
|
8
8
|
s.homepage = "https://github.com/reevoo/responsible"
|
9
9
|
s.authors = ["dw_henry", "lamp"]
|
@@ -16,4 +16,5 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_development_dependency 'rake'
|
18
18
|
s.add_development_dependency 'minitest'
|
19
|
-
|
19
|
+
s.add_development_dependency 'rspec'
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Responsible::Consumer do
|
4
|
+
context '#can_see?' do
|
5
|
+
let(:with_no_restrictions) { described_class.new }
|
6
|
+
let(:with_one_restrictions) { described_class.new(:even) }
|
7
|
+
let(:with_two_restrictions) { described_class.new(:even, :prime) }
|
8
|
+
|
9
|
+
it "always return true when no restrictions passed in" do
|
10
|
+
expect(with_no_restrictions.can_see?(nil)).to be_true
|
11
|
+
expect(with_one_restrictions.can_see?(nil)).to be_true
|
12
|
+
expect(with_two_restrictions.can_see?(nil)).to be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'return true if at the restriction is in the valid list' do
|
16
|
+
expect(with_no_restrictions.can_see?(:even)).to be_false
|
17
|
+
|
18
|
+
expect(with_one_restrictions.can_see?(:even)).to be_true
|
19
|
+
expect(with_two_restrictions.can_see?(:even)).to be_true
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'return true if any of the restriction are in the valid list' do
|
23
|
+
expect(with_no_restrictions.can_see?([:even, :prime])).to be_false
|
24
|
+
|
25
|
+
expect(with_one_restrictions.can_see?([:even, :prime])).to be_true
|
26
|
+
expect(with_two_restrictions.can_see?([:even, :prime])).to be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'return false if none of the restriction are in the valid list' do
|
30
|
+
expect(with_no_restrictions.can_see?(:other)).to be_false
|
31
|
+
|
32
|
+
expect(with_one_restrictions.can_see?(:other)).to be_false
|
33
|
+
expect(with_two_restrictions.can_see?(:other)).to be_false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: responsible
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dw_henry
|
@@ -9,34 +9,48 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- -
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
20
|
version: '0'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
|
-
- -
|
25
|
+
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: minitest
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
|
-
- -
|
32
|
+
- - ">="
|
33
33
|
- !ruby/object:Gem::Version
|
34
34
|
version: '0'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
|
-
- -
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
40
54
|
- !ruby/object:Gem::Version
|
41
55
|
version: '0'
|
42
56
|
description:
|
@@ -46,12 +60,13 @@ extensions: []
|
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
62
|
- Gemfile
|
49
|
-
- README.md
|
50
|
-
- responsible.gemspec
|
51
63
|
- LICENSE
|
64
|
+
- README.md
|
52
65
|
- lib/consumer.rb
|
53
66
|
- lib/responsible.rb
|
67
|
+
- responsible.gemspec
|
54
68
|
- spec/base_spec.rb
|
69
|
+
- spec/consumer_spec.rb
|
55
70
|
- spec/spec_helper.rb
|
56
71
|
homepage: https://github.com/reevoo/responsible
|
57
72
|
licenses: []
|
@@ -62,21 +77,21 @@ require_paths:
|
|
62
77
|
- lib
|
63
78
|
required_ruby_version: !ruby/object:Gem::Requirement
|
64
79
|
requirements:
|
65
|
-
- -
|
80
|
+
- - ">="
|
66
81
|
- !ruby/object:Gem::Version
|
67
82
|
version: '0'
|
68
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
84
|
requirements:
|
70
|
-
- -
|
85
|
+
- - ">="
|
71
86
|
- !ruby/object:Gem::Version
|
72
87
|
version: '0'
|
73
88
|
requirements: []
|
74
89
|
rubyforge_project:
|
75
|
-
rubygems_version: 2.
|
90
|
+
rubygems_version: 2.2.0
|
76
91
|
signing_key:
|
77
92
|
specification_version: 4
|
78
93
|
summary: Response builders
|
79
94
|
test_files:
|
80
95
|
- spec/base_spec.rb
|
96
|
+
- spec/consumer_spec.rb
|
81
97
|
- spec/spec_helper.rb
|
82
|
-
has_rdoc:
|