ananke 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/ananke/helpers.rb +6 -13
- data/lib/ananke/linking.rb +3 -1
- data/lib/ananke/routing.rb +44 -39
- data/lib/ananke/serialize.rb +38 -0
- data/lib/ananke.rb +1 -1
- data/lib/version.rb +1 -1
- data/spec/lib/route_for_spec.rb +6 -6
- metadata +20 -19
data/lib/ananke/helpers.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'json'
|
1
2
|
module Ananke
|
2
3
|
|
3
4
|
extend Colored
|
@@ -11,20 +12,12 @@ module Ananke
|
|
11
12
|
mod
|
12
13
|
end
|
13
14
|
|
14
|
-
def get_json(path, obj, links)
|
15
|
-
if obj.nil?
|
16
|
-
out :error, "#{path} - No return object"
|
17
|
-
''
|
18
|
-
else
|
19
|
-
root_path = path.to_s.split('/')[0]
|
20
|
-
dic = {root_path.to_sym => obj}
|
21
|
-
dic[:links] = links unless links.nil?
|
22
|
-
dic.to_json
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
15
|
def get_id(obj, key)
|
27
|
-
|
16
|
+
if !key
|
17
|
+
out :error, "Cannot get id on object #{obj}, key is nil"
|
18
|
+
return nil
|
19
|
+
end
|
20
|
+
obj.class == Hash && obj.has_key?(key) ? obj[key] : obj.respond_to?(key) ? obj.instance_variable_get("@#{key}") : nil
|
28
21
|
end
|
29
22
|
|
30
23
|
def out(type, message)
|
data/lib/ananke/linking.rb
CHANGED
@@ -10,7 +10,9 @@ module Ananke
|
|
10
10
|
end
|
11
11
|
#===========================SELF===============================
|
12
12
|
def build_link_self(path, id)
|
13
|
-
|
13
|
+
uri = "/#{path.to_s}"
|
14
|
+
uri << "/#{id}" if id
|
15
|
+
[{:rel => 'self', :uri => uri}]#"/#{path}/#{id}"}]
|
14
16
|
end
|
15
17
|
#===========================LINKED=============================
|
16
18
|
def build_link_list(path, id, mod, link_list)
|
data/lib/ananke/routing.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
require '
|
2
|
-
require '
|
3
|
-
require '
|
1
|
+
require 'ananke/linking'
|
2
|
+
require 'ananke/helpers'
|
3
|
+
require 'ananke/serialize'
|
4
|
+
require 'ananke/validation'
|
4
5
|
|
5
6
|
module Ananke
|
6
7
|
public
|
@@ -28,12 +29,44 @@ module Ananke
|
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
32
|
+
def make_response_item(path, mod, link_list, link_to_list, obj, key)
|
33
|
+
item = nil
|
34
|
+
id = get_id(obj, key)
|
35
|
+
if !id.nil?
|
36
|
+
dic = {path.to_sym => obj}
|
37
|
+
links = build_links(link_list, link_to_list, path, id, mod) if Ananke.settings[:links]
|
38
|
+
dic[:links] = links if links
|
39
|
+
item = dic
|
40
|
+
else
|
41
|
+
out :info, "#{path} - Cannot find key(#{key}) on object #{obj}"
|
42
|
+
end
|
43
|
+
item
|
44
|
+
end
|
45
|
+
|
46
|
+
def make_response(path, mod, link_list, link_to_list, obj, key)
|
47
|
+
if obj.class == Array
|
48
|
+
result_list = []
|
49
|
+
obj.each{|i| result_list << make_response_item(path, mod, link_list, link_to_list, i, key)}
|
50
|
+
|
51
|
+
dic = result_list.empty? ? {} : {"#{path}_list".to_sym => result_list}
|
52
|
+
link_self = build_link_self(path, '') if Ananke.settings[:links]
|
53
|
+
dic[:links] = link_self if link_self
|
54
|
+
|
55
|
+
Serialize.to_j(dic)
|
56
|
+
else
|
57
|
+
Serialize.to_j(make_response_item(path, mod, link_list, link_to_list, obj, key))
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
31
61
|
def build(path)
|
32
62
|
mod = get_mod(path)
|
33
63
|
if mod.nil?
|
34
64
|
out(:error, "Repository for #{path} not found")
|
35
65
|
return
|
36
66
|
end
|
67
|
+
if @id.empty?
|
68
|
+
out :info, "No Id specified for #{path}"
|
69
|
+
end
|
37
70
|
key = @id[:key]
|
38
71
|
fields = @fields
|
39
72
|
link_list = @link_list
|
@@ -45,36 +78,16 @@ module Ananke
|
|
45
78
|
param_missing!(key) if params[key].nil?
|
46
79
|
obj = mod.one(params[key])
|
47
80
|
|
48
|
-
links = build_links(link_list, link_to_list, path, params[key], mod)
|
49
|
-
json = get_json(path, obj, links)
|
50
|
-
|
51
81
|
status 200
|
52
|
-
|
82
|
+
make_response(path, mod, link_list, link_to_list, obj, key)
|
53
83
|
end
|
54
84
|
|
55
85
|
#===========================GET================================
|
56
86
|
build_route mod, :all, :get, "/#{path}/?" do
|
57
|
-
|
87
|
+
obj = mod.all
|
58
88
|
|
59
89
|
status 200
|
60
|
-
|
61
|
-
result_list = []
|
62
|
-
obj_list.each do |obj|
|
63
|
-
id = get_id(obj, key)
|
64
|
-
if !id.nil?
|
65
|
-
dic = {path.to_sym => obj}
|
66
|
-
links = build_links(link_list, link_to_list, path, id, mod) if Ananke.settings[:links]
|
67
|
-
dic[:links] = links unless links.nil?
|
68
|
-
result_list << dic
|
69
|
-
else
|
70
|
-
out :error, "#{path} - Cannot find key(#{key}) on object #{obj}"
|
71
|
-
end
|
72
|
-
end
|
73
|
-
dic = {"#{path}_list".to_sym => result_list}
|
74
|
-
link_self = build_link_self(path, '') if Ananke.settings[:links]
|
75
|
-
dic[:links] = link_self unless link_self.nil?
|
76
|
-
|
77
|
-
dic.to_json
|
90
|
+
make_response(path, mod, link_list, link_to_list, obj, key)
|
78
91
|
end
|
79
92
|
|
80
93
|
#===========================POST===============================
|
@@ -85,11 +98,8 @@ module Ananke
|
|
85
98
|
|
86
99
|
obj = repository_call(mod, :add, new_params)
|
87
100
|
|
88
|
-
links = build_links(link_list, link_to_list, path, new_params[key], mod)
|
89
|
-
json = get_json(path, obj, links)
|
90
|
-
|
91
101
|
status 201
|
92
|
-
|
102
|
+
make_response(path, mod, link_list, link_to_list, obj, key)
|
93
103
|
end
|
94
104
|
|
95
105
|
#===========================PUT================================
|
@@ -100,12 +110,9 @@ module Ananke
|
|
100
110
|
error status, message unless status.nil?
|
101
111
|
|
102
112
|
obj = repository_call(mod, :edit, new_params)
|
103
|
-
|
104
|
-
links = build_links(link_list, link_to_list, path, params[key], mod)
|
105
|
-
json = get_json(path, obj, links)
|
106
|
-
|
113
|
+
|
107
114
|
status 200
|
108
|
-
|
115
|
+
make_response(path, mod, link_list, link_to_list, obj, key)
|
109
116
|
end
|
110
117
|
|
111
118
|
build_route mod, :edit, :put, "/#{path}/?" do
|
@@ -134,12 +141,10 @@ module Ananke
|
|
134
141
|
param_missing!(:key) if inputs.length == 1 && new_params[:key].nil?
|
135
142
|
|
136
143
|
obj = repository_call(mod, r[:name], new_params)
|
137
|
-
|
138
|
-
links = build_links(link_list, link_to_list, "#{path}/#{r[:name]}", params[:key], mod)
|
139
|
-
json = get_json("#{path}/#{r[:name]}", obj, links)
|
144
|
+
obj_list = obj.class == Array ? obj : [obj]
|
140
145
|
|
141
146
|
status 200
|
142
|
-
|
147
|
+
make_response(path, mod, link_list, link_to_list, obj_list, key).gsub("\"/#{path}/\"", "\"#{request.path}\"")
|
143
148
|
end
|
144
149
|
end
|
145
150
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'json'
|
2
|
+
module Serialize
|
3
|
+
|
4
|
+
def self.can_serialize?(obj)
|
5
|
+
obj.class != Array and !obj.to_json.start_with?('"#<')
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.to_h(obj)
|
9
|
+
ret = {}
|
10
|
+
|
11
|
+
if obj.class == Hash
|
12
|
+
obj.each do |k, v|
|
13
|
+
ret[k.to_sym] = (can_serialize?(v) ? v : Serialize.to_h(v))
|
14
|
+
end
|
15
|
+
elsif obj.class == Array
|
16
|
+
ret = []
|
17
|
+
obj.each do |i|
|
18
|
+
#ret << (can_serialize?(i) ? i : Serialize.to_hash(i))
|
19
|
+
ret << Serialize.to_h(i)
|
20
|
+
end
|
21
|
+
else
|
22
|
+
obj.instance_variables.each do |e|
|
23
|
+
value = obj.instance_variable_get e.to_sym
|
24
|
+
ret[e[1..-1]] = (can_serialize?(value) ? value : Serialize.to_h(value))
|
25
|
+
end
|
26
|
+
end
|
27
|
+
ret
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.to_j(obj)
|
31
|
+
Serialize.to_h(obj).to_json
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.to_json_pretty(obj)
|
35
|
+
JSON.pretty_generate(Serialize.to_h(obj), opts = {:indent => ' '})
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/lib/ananke.rb
CHANGED
@@ -2,13 +2,13 @@ libdir = File.dirname(__FILE__)
|
|
2
2
|
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
|
3
3
|
|
4
4
|
require 'colored'
|
5
|
-
require 'json'
|
6
5
|
require 'sinatra/base'
|
7
6
|
|
8
7
|
require 'ananke/helpers'
|
9
8
|
require 'ananke/linking'
|
10
9
|
require 'ananke/routing'
|
11
10
|
require 'ananke/settings'
|
11
|
+
require 'ananke/serialize'
|
12
12
|
require 'ananke/validation'
|
13
13
|
|
14
14
|
module Ananke
|
data/lib/version.rb
CHANGED
data/spec/lib/route_for_spec.rb
CHANGED
@@ -15,18 +15,18 @@ describe 'Resource Route-For' do
|
|
15
15
|
module Repository
|
16
16
|
module Route_for
|
17
17
|
def self.custom(id)
|
18
|
-
{:content => 'Test'}
|
18
|
+
{:route_for_id => id, :content => 'Test'}
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
22
22
|
route :route_for do
|
23
|
-
id :
|
23
|
+
id :route_for_id
|
24
24
|
route_for :custom
|
25
25
|
end
|
26
26
|
|
27
27
|
get "/route_for/custom/1"
|
28
28
|
check_status(200)
|
29
|
-
last_response.body.should == '{"route_for":{"content":"Test"},"links":[{"rel":"self","uri":"/route_for/custom/1"}]}'
|
29
|
+
last_response.body.should == '{"route_for_list":[{"route_for":{"route_for_id":"1","content":"Test"},"links":[{"rel":"self","uri":"/route_for/1"}]}],"links":[{"rel":"self","uri":"/route_for/custom/1"}]}'
|
30
30
|
end
|
31
31
|
|
32
32
|
it """
|
@@ -35,17 +35,17 @@ describe 'Resource Route-For' do
|
|
35
35
|
module Repository
|
36
36
|
module Route_for
|
37
37
|
def self.multi(id, name)
|
38
|
-
{:content => 'Test'}
|
38
|
+
{:route_for_id => id, :content => 'Test'}
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
42
42
|
route :route_for do
|
43
|
-
id :
|
43
|
+
id :route_for_id
|
44
44
|
route_for :multi, :post
|
45
45
|
end
|
46
46
|
|
47
47
|
post "/route_for/multi", body={:id => 1, :name => 'some name'}
|
48
48
|
check_status(200)
|
49
|
-
last_response.body.should == '{"route_for":{"content":"Test"},"links":[{"rel":"self","uri":"/route_for/multi
|
49
|
+
last_response.body.should == '{"route_for_list":[{"route_for":{"route_for_id":"1","content":"Test"},"links":[{"rel":"self","uri":"/route_for/1"}]}],"links":[{"rel":"self","uri":"/route_for/multi"}]}'
|
50
50
|
end
|
51
51
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: ananke
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 1.0.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Andries Coetzee
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-02-
|
13
|
+
date: 2011-02-22 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -100,24 +100,25 @@ extra_rdoc_files:
|
|
100
100
|
- README.rdoc
|
101
101
|
files:
|
102
102
|
- lib/ananke.rb
|
103
|
-
- lib/version.rb
|
104
103
|
- lib/ananke/settings.rb
|
104
|
+
- lib/ananke/helpers.rb
|
105
105
|
- lib/ananke/linking.rb
|
106
|
-
- lib/ananke/
|
106
|
+
- lib/ananke/serialize.rb
|
107
107
|
- lib/ananke/validation.rb
|
108
|
-
- lib/ananke/
|
108
|
+
- lib/ananke/routing.rb
|
109
|
+
- lib/version.rb
|
110
|
+
- spec/nice_formatter.rb
|
111
|
+
- spec/call_chain.rb
|
109
112
|
- spec/dumping.rb
|
110
113
|
- spec/cov_adapter.rb
|
111
|
-
- spec/lib/ananke_spec.rb
|
112
|
-
- spec/lib/validation_spec.rb
|
113
|
-
- spec/lib/out_spec.rb
|
114
114
|
- spec/lib/json_spec.rb
|
115
|
-
- spec/lib/
|
115
|
+
- spec/lib/ananke_spec.rb
|
116
116
|
- spec/lib/route_for_spec.rb
|
117
|
+
- spec/lib/out_spec.rb
|
118
|
+
- spec/lib/validation_spec.rb
|
117
119
|
- spec/lib/linked_spec.rb
|
120
|
+
- spec/lib/link_to_spec.rb
|
118
121
|
- spec/spec_helper.rb
|
119
|
-
- spec/call_chain.rb
|
120
|
-
- spec/nice_formatter.rb
|
121
122
|
- Gemfile
|
122
123
|
- Rakefile
|
123
124
|
- README.rdoc
|
@@ -128,7 +129,7 @@ licenses: []
|
|
128
129
|
post_install_message: |
|
129
130
|
**************************************************
|
130
131
|
|
131
|
-
Thank you for installing ananke-0.
|
132
|
+
Thank you for installing ananke-1.0.0
|
132
133
|
|
133
134
|
Please be sure to look at README.rdoc to see what might have changed
|
134
135
|
since the last release and how to use this GEM.
|
@@ -157,17 +158,17 @@ rubyforge_project:
|
|
157
158
|
rubygems_version: 1.5.0
|
158
159
|
signing_key:
|
159
160
|
specification_version: 3
|
160
|
-
summary: ananke-0.
|
161
|
+
summary: ananke-1.0.0
|
161
162
|
test_files:
|
163
|
+
- spec/nice_formatter.rb
|
164
|
+
- spec/call_chain.rb
|
162
165
|
- spec/dumping.rb
|
163
166
|
- spec/cov_adapter.rb
|
164
|
-
- spec/lib/ananke_spec.rb
|
165
|
-
- spec/lib/validation_spec.rb
|
166
|
-
- spec/lib/out_spec.rb
|
167
167
|
- spec/lib/json_spec.rb
|
168
|
-
- spec/lib/
|
168
|
+
- spec/lib/ananke_spec.rb
|
169
169
|
- spec/lib/route_for_spec.rb
|
170
|
+
- spec/lib/out_spec.rb
|
171
|
+
- spec/lib/validation_spec.rb
|
170
172
|
- spec/lib/linked_spec.rb
|
173
|
+
- spec/lib/link_to_spec.rb
|
171
174
|
- spec/spec_helper.rb
|
172
|
-
- spec/call_chain.rb
|
173
|
-
- spec/nice_formatter.rb
|