kartograph 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +12 -1
- data/examples/collection_representation.rb +41 -0
- data/lib/kartograph/dsl.rb +16 -4
- data/lib/kartograph/version.rb +1 -1
- data/spec/lib/kartograph/dsl_spec.rb +43 -0
- data/spec/support/dummy_user.rb +2 -1
- metadata +16 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0295131c24571bb5b97074b8c6ddb293d6e6e5c5
|
4
|
+
data.tar.gz: c54a19d0f60e9671b01f46dc7c3e22e9336f4a1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f9810e797d94884d70753280442945dd1c8a05fc956d0e545e81fed318fdf087468ad315b60ecc98a8bbd3967f8ad82a0234ace82b75ace8b1e9146f5e3c1d7
|
7
|
+
data.tar.gz: 31cd1b4efc22648129ee836936bd90e42ee428ae351526784ae3e307d7148e473558a3982768b8d9137e530d08a50622525410a3d40b5912cc4f1615b587276f
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -36,6 +36,17 @@ user = User.new(name: 'Bobby Tables')
|
|
36
36
|
json_for_create = UserMapping.representation_for(:create, user)
|
37
37
|
```
|
38
38
|
|
39
|
+
### Rendering Collections as JSON
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
user = User.new(name: 'Bobby Tables')
|
43
|
+
users = Array.new(10, user)
|
44
|
+
|
45
|
+
json = UserMapping.represent_collection_for(:read, users)
|
46
|
+
```
|
47
|
+
|
48
|
+
---
|
49
|
+
|
39
50
|
Some API's will give you the created resource back as JSON as well on a successful create. For that, you may do something like this:
|
40
51
|
|
41
52
|
```ruby
|
@@ -93,7 +104,7 @@ class UserMapping
|
|
93
104
|
|
94
105
|
kartograph do
|
95
106
|
mapping User
|
96
|
-
root_key singular: 'user', plural: 'users' scopes: [:read]
|
107
|
+
root_key singular: 'user', plural: 'users', scopes: [:read]
|
97
108
|
property :id, scopes: [:read]
|
98
109
|
end
|
99
110
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'kartograph'
|
2
|
+
require 'pp'
|
3
|
+
|
4
|
+
class User < Struct.new(:id, :name, :comments)
|
5
|
+
end
|
6
|
+
|
7
|
+
class Comment < Struct.new(:id, :text)
|
8
|
+
end
|
9
|
+
|
10
|
+
class UserMapping
|
11
|
+
include Kartograph::DSL
|
12
|
+
|
13
|
+
kartograph do
|
14
|
+
mapping User
|
15
|
+
|
16
|
+
property :id, scopes: [:read]
|
17
|
+
property :name, scopes: [:read, :create]
|
18
|
+
|
19
|
+
property :comments, plural: true, scopes: [:read] do
|
20
|
+
mapping Comment
|
21
|
+
|
22
|
+
property :id, scopes: [:read]
|
23
|
+
property :text, scopes: [:read, :create]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
user = User.new(1, 'he@he.com')
|
29
|
+
comment = Comment.new(12, 'aksjdfhasjkdfh')
|
30
|
+
|
31
|
+
user.comments = Array.new(3, comment)
|
32
|
+
users = Array.new(4, user)
|
33
|
+
|
34
|
+
json = UserMapping.represent_collection_for(:read, users)
|
35
|
+
puts "The JSON generated from that collection:"
|
36
|
+
puts json
|
37
|
+
|
38
|
+
users_again = UserMapping.extract_collection(json, :read)
|
39
|
+
puts "\n"
|
40
|
+
puts "And the JSON slurped back into an array:"
|
41
|
+
pp users_again
|
data/lib/kartograph/dsl.rb
CHANGED
@@ -18,12 +18,15 @@ module Kartograph
|
|
18
18
|
def representation_for(scope, object, dumper = JSON)
|
19
19
|
drawed = Artist.new(object, @kartograph_map).draw(scope)
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
dumper.dump(prepend_root_key(scope, :singular, drawed))
|
22
|
+
end
|
23
|
+
|
24
|
+
def represent_collection_for(scope, objects, dumper = JSON)
|
25
|
+
drawed_objects = objects.map do |object|
|
26
|
+
Artist.new(object, @kartograph_map).draw(scope)
|
24
27
|
end
|
25
28
|
|
26
|
-
dumper.dump(
|
29
|
+
dumper.dump(prepend_root_key(scope, :plural, drawed_objects))
|
27
30
|
end
|
28
31
|
|
29
32
|
def extract_single(content, scope, loader = JSON)
|
@@ -51,12 +54,21 @@ module Kartograph
|
|
51
54
|
end
|
52
55
|
|
53
56
|
private
|
57
|
+
def prepend_root_key(scope, plurality, payload)
|
58
|
+
retrieve_root_key(scope, plurality) do |root_key|
|
59
|
+
# Reassign drawed if a root key exists
|
60
|
+
payload = { root_key => payload }
|
61
|
+
end
|
62
|
+
|
63
|
+
payload
|
64
|
+
end
|
54
65
|
|
55
66
|
def retrieve_root_key(scope, type, &block)
|
56
67
|
if root_key = @kartograph_map.root_key_for(scope, type)
|
57
68
|
yield root_key
|
58
69
|
end
|
59
70
|
end
|
71
|
+
|
60
72
|
end
|
61
73
|
end
|
62
74
|
end
|
data/lib/kartograph/version.rb
CHANGED
@@ -48,6 +48,49 @@ describe Kartograph::DSL do
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
+
describe '.represent_collection_for' do
|
52
|
+
include_context 'DSL Objects'
|
53
|
+
|
54
|
+
let(:users) { Array.new(3, object) }
|
55
|
+
|
56
|
+
subject(:parsed) do
|
57
|
+
json = mapped.represent_collection_for(:read, users)
|
58
|
+
JSON.parse(json)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'returns the objects as a collection' do
|
62
|
+
json = mapped.represent_collection_for(:read, users)
|
63
|
+
parsed = JSON.parse(json)
|
64
|
+
|
65
|
+
expect(parsed).to be_an(Array)
|
66
|
+
expect(parsed.size).to be(3)
|
67
|
+
|
68
|
+
expect(parsed[0]['id']).to eq(users[0].id)
|
69
|
+
expect(parsed[0]['name']).to eq(users[0].name)
|
70
|
+
expect(parsed[1]['id']).to eq(users[1].id)
|
71
|
+
expect(parsed[1]['name']).to eq(users[1].name)
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'with a root key' do
|
75
|
+
it "includes the root key" do
|
76
|
+
root_key_name = "the_root_key"
|
77
|
+
|
78
|
+
mapped.kartograph do
|
79
|
+
root_key plural: root_key_name, scopes: [:read]
|
80
|
+
end
|
81
|
+
|
82
|
+
expect(parsed).to be_an(Hash)
|
83
|
+
expect(parsed.keys.first).to eq(root_key_name)
|
84
|
+
|
85
|
+
parsed_array = parsed[root_key_name]
|
86
|
+
expect(parsed_array[0]['id']).to eq(users[0].id)
|
87
|
+
expect(parsed_array[0]['name']).to eq(users[0].name)
|
88
|
+
expect(parsed_array[1]['id']).to eq(users[1].id)
|
89
|
+
expect(parsed_array[1]['name']).to eq(users[1].name)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
51
94
|
describe '.extract_single' do
|
52
95
|
include_context 'DSL Objects'
|
53
96
|
let(:json) do
|
data/spec/support/dummy_user.rb
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
DummyUser
|
1
|
+
class DummyUser < Struct.new(:id, :name, :comment, :email)
|
2
|
+
end
|
metadata
CHANGED
@@ -1,69 +1,69 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kartograph
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Ross
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 3.0.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 3.0.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: pry
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
description: Kartograph makes it easy to generate and convert JSON. It's intention
|
@@ -75,13 +75,14 @@ executables: []
|
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
-
- .gitignore
|
79
|
-
- .rspec
|
80
|
-
- .travis.yml
|
78
|
+
- ".gitignore"
|
79
|
+
- ".rspec"
|
80
|
+
- ".travis.yml"
|
81
81
|
- Gemfile
|
82
82
|
- LICENSE.txt
|
83
83
|
- README.md
|
84
84
|
- Rakefile
|
85
|
+
- examples/collection_representation.rb
|
85
86
|
- examples/domains.rb
|
86
87
|
- examples/representation_for.rb
|
87
88
|
- kartograph.gemspec
|
@@ -117,12 +118,12 @@ require_paths:
|
|
117
118
|
- lib
|
118
119
|
required_ruby_version: !ruby/object:Gem::Requirement
|
119
120
|
requirements:
|
120
|
-
- -
|
121
|
+
- - ">="
|
121
122
|
- !ruby/object:Gem::Version
|
122
123
|
version: '0'
|
123
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
125
|
requirements:
|
125
|
-
- -
|
126
|
+
- - ">="
|
126
127
|
- !ruby/object:Gem::Version
|
127
128
|
version: '0'
|
128
129
|
requirements: []
|