ananke 1.0.4 → 1.0.5

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.
@@ -5,11 +5,11 @@ module Ananke
5
5
 
6
6
  public
7
7
 
8
- def get_mod(path)
9
- mod = nil
10
- rep = Module.const_get(Ananke.repository) if Module.const_defined?(Ananke.repository)
11
- mod = rep.const_get("#{path.capitalize}".to_sym) if !rep.nil? && rep.const_defined?("#{path.capitalize}".to_sym)
12
- mod
8
+ def get_repository_module(path)
9
+ repository = nil
10
+ repository = Module.const_get(Ananke.repository) if Module.const_defined?(Ananke.repository)
11
+ repository = repository.const_get("#{path.capitalize}".to_sym) if !repository.nil? && repository.const_defined?("#{path.capitalize}".to_sym)
12
+ repository
13
13
  end
14
14
 
15
15
  def get_id(obj, key)
@@ -1,9 +1,9 @@
1
1
  module Ananke
2
- def build_links(link_list, link_to_list, path, id, mod)
2
+ def build_links(link_list, link_to_list, path, id, repository)
3
3
  return if !Ananke.settings[:links]
4
4
 
5
5
  links = id ? build_link_self(path, id) : []
6
- links += build_link_list(path, id, mod, link_list)
6
+ links += build_link_list(path, id, repository, link_list)
7
7
  links += build_link_to_list(path, id, link_to_list)
8
8
 
9
9
  links
@@ -12,28 +12,24 @@ module Ananke
12
12
  def build_link_self(path, id)
13
13
  uri = "/#{path.to_s}"
14
14
  uri << "/#{id}" if id
15
- [{:rel => 'self', :uri => uri}]#"/#{path}/#{id}"}]
15
+ [{:rel => 'self', :uri => uri}]
16
16
  end
17
17
  #===========================LINKED=============================
18
- def build_link_list(path, id, mod, link_list)
18
+ def build_link_list(path, id, repository, link_list)
19
19
  links = []
20
- link_list.each do |l|
21
- mod_method = "#{l[:rel]}_id_list"
22
- if mod.respond_to?(mod_method)
23
- id_list = mod.send(mod_method, id)
24
- id_list.each{|i| links << {:rel => "#{l[:rel]}", :uri => "/#{l[:rel]}/#{i}"}}
20
+ link_list.each do |link|
21
+ repository_method = "#{link[:rel]}_id_list"
22
+ if repository.respond_to?(repository_method)
23
+ id_list = repository.send(repository_method, id)
24
+ links = id_list.collect{|i| {:rel => "#{link[:rel]}", :uri => "/#{link[:rel]}/#{i}"}}
25
25
  else
26
- out :error, "#{path} - #{mod} does not respond to '#{mod_method.to_s}'"
26
+ out :error, "#{path} - #{repository} does not respond to '#{repository_method.to_s}'"
27
27
  end
28
28
  end
29
29
  links
30
30
  end
31
31
  #===========================LINK_TO============================
32
32
  def build_link_to_list(path, id, link_to_list)
33
- links = []
34
- link_to_list.each do |l|
35
- links << {:rel => "#{l[:rel]}", :uri => "/#{l[:rel]}/#{path.to_s.split('/')[0]}/#{id}"}
36
- end
37
- links
33
+ link_to_list.collect { |link| {:rel => "#{link[:rel]}", :uri => "/#{link[:rel]}/#{path.to_s.split('/')[0]}/#{id}"} }
38
34
  end
39
35
  end
@@ -17,15 +17,16 @@ module Ananke
17
17
  Ananke.routes[name.to_sym] << method.to_sym
18
18
  end
19
19
 
20
- def build_route(mod, mod_method, verb, route, &block)
21
- if mod.respond_to? mod_method
22
- define_repository_call(mod, mod_method)
23
- add_route(route.split('/')[1], mod_method)
20
+ def build_route(repository_module, repository_method, verb, route, &block)
21
+ if repository_module.respond_to? repository_method
22
+ define_repository_call(repository_module, repository_method)
23
+ add_route(route.split('/')[1], repository_method)
24
24
  Sinatra::Base.send verb, "#{route}", do
25
+ @params = collect_params(@params)
25
26
  instance_eval(&block)
26
27
  end
27
28
  else
28
- out(:warning, "#{mod} does not respond to '#{mod_method.to_s}'")
29
+ out(:warning, "#{repository_module} does not respond to '#{repository_method.to_s}'")
29
30
  end
30
31
  end
31
32
 
@@ -40,10 +41,10 @@ module Ananke
40
41
 
41
42
  def make_response(path, mod, link_list, link_to_list, obj, key)
42
43
  if obj.class == Array
43
- result_list = []
44
- obj.each{|i| result_list << make_response_item(path, mod, link_list, link_to_list, i, key) if i}
44
+ result_list = obj.collect{|item| make_response_item(path, mod, link_list, link_to_list, item, key) if item}.compact
45
45
 
46
- dic = result_list.empty? ? {} : {"#{path}_list".to_sym => result_list}
46
+ #dic = result_list.empty? ? {} : {"#{path}_list".to_sym => result_list}
47
+ dic = result_list.empty? ? {} : {"items".to_sym => result_list}
47
48
  link_self = build_link_self(path, '') if Ananke.settings[:links]
48
49
  dic[:links] = link_self if link_self
49
50
 
@@ -54,7 +55,7 @@ module Ananke
54
55
  end
55
56
 
56
57
  def build(path)
57
- mod = get_mod(path)
58
+ mod = get_repository_module(path)
58
59
  if mod.nil?
59
60
  out(:error, "Repository for #{path} not found")
60
61
  return
@@ -68,10 +69,9 @@ module Ananke
68
69
 
69
70
  #===========================GET/ID=============================
70
71
  build_route mod, :one, :get, "/#{path}/:#{key}" do
71
- new_params = collect_params(params)
72
- param_missing!(key) if new_params[key].nil?
72
+ param_missing!(key) if params[key].nil?
73
73
 
74
- obj, obj_status = repository_call(mod, :one, new_params)
74
+ obj, obj_status = repository_call(mod, :one, params)
75
75
  error obj_status, obj if obj_status >= 400
76
76
  not_found if obj && obj.class == Array && obj.empty?
77
77
 
@@ -81,7 +81,6 @@ module Ananke
81
81
 
82
82
  #===========================GET================================
83
83
  build_route mod, :all, :get, "/#{path}/?" do
84
-
85
84
  obj, obj_status = repository_call(mod, :all)
86
85
  error obj_status, obj if obj_status >= 400
87
86
  not_found if obj && obj.class == Array && obj.empty?
@@ -92,11 +91,10 @@ module Ananke
92
91
 
93
92
  #===========================POST===============================
94
93
  build_route mod, :add, :post, "/#{path}/?" do
95
- new_params = collect_params(params)
96
- status, message = validate(fields, new_params)
94
+ status, message = validate(fields, params)
97
95
  error status, message unless status.nil?
98
96
 
99
- obj, obj_status = repository_call(mod, :add, new_params)
97
+ obj, obj_status = repository_call(mod, :add, params)
100
98
  error obj_status, obj if obj_status >= 400
101
99
  not_found if obj && obj.class == Array && obj.empty?
102
100
 
@@ -106,12 +104,11 @@ module Ananke
106
104
 
107
105
  #===========================PUT================================
108
106
  build_route mod, :edit, :put, "/#{path}/:#{key}" do
109
- new_params = collect_params(params)
110
- param_missing!(key) if new_params[key].nil?
111
- status, message = validate(fields, new_params)
107
+ param_missing!(key) if params[key].nil?
108
+ status, message = validate(fields, params)
112
109
  error status, message unless status.nil?
113
110
 
114
- obj, obj_status = repository_call(mod, :edit, new_params)
111
+ obj, obj_status = repository_call(mod, :edit, params)
115
112
  error obj_status, obj if obj_status >= 400
116
113
  not_found if obj && obj.class == Array && obj.empty?
117
114
 
@@ -125,10 +122,9 @@ module Ananke
125
122
 
126
123
  #===========================DELETE=============================
127
124
  build_route mod, :delete, :delete, "/#{path}/:#{key}" do
128
- new_params = collect_params(params)
129
- param_missing!(key) if new_params[key].nil?
125
+ param_missing!(key) if params[key].nil?
130
126
 
131
- obj, obj_status = repository_call(mod, :delete, new_params)
127
+ obj, obj_status = repository_call(mod, :delete, params)
132
128
  error obj_status, obj if obj_status >= 400
133
129
  not_found if obj && obj.class == Array && obj.empty?
134
130
 
@@ -146,10 +142,9 @@ module Ananke
146
142
  full_path << "/:key" if inputs.length == 1
147
143
 
148
144
  build_route mod, r[:name], r[:verb], full_path do
149
- new_params = collect_params(params)
150
- param_missing!(:key) if inputs.length == 1 && new_params[:key].nil?
145
+ param_missing!(:key) if inputs.length == 1 && params[:key].nil?
151
146
 
152
- obj, obj_status = repository_call(mod, r[:name], new_params)
147
+ obj, obj_status = repository_call(mod, r[:name], params)
153
148
  error obj_status, obj if obj_status >= 400
154
149
  not_found if obj && obj.class == Array && obj.empty?
155
150
 
@@ -18,6 +18,8 @@ module Serialize
18
18
  #ret << (can_serialize?(i) ? i : Serialize.to_hash(i))
19
19
  ret << Serialize.to_h(i)
20
20
  end
21
+ elsif obj.instance_variables.empty?
22
+ ret = obj
21
23
  else
22
24
  obj.instance_variables.each do |e|
23
25
  value = obj.instance_variable_get e.to_sym
@@ -31,7 +33,7 @@ module Serialize
31
33
  Serialize.to_h(obj).to_json
32
34
  end
33
35
 
34
- def self.to_json_pretty(obj)
36
+ def self.to_j_pretty(obj)
35
37
  JSON.pretty_generate(Serialize.to_h(obj), opts = {:indent => ' '})
36
38
  end
37
39
 
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ananke
2
- VERSION = "1.0.4"
2
+ VERSION = "1.0.5"
3
3
  end
@@ -70,7 +70,7 @@ describe 'Basic Ananke REST' do
70
70
  """ do
71
71
  get "/user"
72
72
  check_status(200)
73
- last_response.body.should == '{"user_list":[{"user":{"user_id":1,"username":"one"}},{"user":{"user_id":2,"username":"two"}}]}'
73
+ last_response.body.should == '{"items":[{"user":{"user_id":1,"username":"one"}},{"user":{"user_id":2,"username":"two"}}]}'
74
74
  end
75
75
 
76
76
  it """
@@ -26,7 +26,7 @@ describe 'Resource Route-For' do
26
26
 
27
27
  get "/route_for/custom/1"
28
28
  check_status(200)
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"}]}'
29
+ last_response.body.should == '{"items":[{"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 """
@@ -46,7 +46,7 @@ describe 'Resource Route-For' do
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_list":[{"route_for":{"route_for_id":"1","content":"Test"},"links":[{"rel":"self","uri":"/route_for/1"}]}],"links":[{"rel":"self","uri":"/route_for/multi"}]}'
49
+ last_response.body.should == '{"items":[{"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
 
52
52
  it """
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ananke
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.4
5
+ version: 1.0.5
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-23 00:00:00 +02:00
13
+ date: 2011-03-24 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -130,7 +130,7 @@ licenses: []
130
130
  post_install_message: |
131
131
  **************************************************
132
132
 
133
- Thank you for installing ananke-1.0.4
133
+ Thank you for installing ananke-1.0.5
134
134
 
135
135
  Please be sure to look at README.rdoc to see what might have changed
136
136
  since the last release and how to use this GEM.
@@ -159,7 +159,7 @@ rubyforge_project:
159
159
  rubygems_version: 1.5.0
160
160
  signing_key:
161
161
  specification_version: 3
162
- summary: ananke-1.0.4
162
+ summary: ananke-1.0.5
163
163
  test_files:
164
164
  - spec/dumping.rb
165
165
  - spec/cov_adapter.rb