medea 0.6.9 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,9 +2,12 @@ module Medea
2
2
  class JasonBase
3
3
  #meta-programming interface for lists
4
4
  include ClassLevelInheritableAttributes
5
- inheritable_attributes :owned
5
+ inheritable_attributes :owned, :public
6
6
  @owned = false
7
7
 
8
+ #these verbs are able to be made public
9
+ HTTP_VERBS = [:GET, :POST, :PUT, :DELETE]
10
+
8
11
  #the resolve method takes a key and returns the JasonObject that has that key
9
12
  #This is useful when you have the key, but not the class
10
13
  def self.resolve(key, mode=:lazy)
@@ -30,6 +33,13 @@ module Medea
30
33
  end
31
34
  end
32
35
 
36
+ def initialize
37
+ @public = []
38
+ if opts[:public]
39
+ opts[:public].each {|i| @public << i}
40
+ end
41
+ end
42
+
33
43
  def ==(other)
34
44
  return false if not other.is_a? JasonBase
35
45
  jason_key == other.jason_key
@@ -124,12 +134,72 @@ module Medea
124
134
  @__jason_data = JSON.parse response
125
135
  @__jason_etag = response.headers[:etag]
126
136
  @__jason_timestamp = response.headers[:timestamp]
127
- if response.headers[:http_x_public]
128
- @public = response.headers[:http_x_public].split(",")
137
+ if response.headers[:http_x_permissions]
138
+ @public = []
139
+ response.headers[:http_x_permissions].match /PUBLIC:\[([A-Z]+)(,[A-Z]+)*\]/ do |m|
140
+ m.captures.each do |c|
141
+ @public << c.slice(/[A-Z]+/)
142
+ end
143
+ end
129
144
  end
130
145
  @__jason_state = :stale
131
146
  end
132
147
 
148
+ def add_public *args
149
+ args.reject! do |i|
150
+ not HTTP_VERBS.include? i
151
+ end
152
+ args.uniq!
153
+ @public += args
154
+ @public.uniq!
155
+ @__jason_state = :dirty unless args.empty?
156
+ end
157
+
158
+ def remove_public *args
159
+ args.reject! do |i|
160
+ not HTTP_VERBS.include? i
161
+ end
162
+ @public -= args
163
+ @public.uniq!
164
+ @__jason_state = :dirty unless args.empty?
165
+ end
133
166
 
167
+ def set_public *args
168
+ args.reject! do |i|
169
+ not HTTP_VERBS.include? i
170
+ end
171
+ args.uniq!
172
+ @__jason_state = :dirty unless args.sort == @public.sort
173
+ @public = args
174
+ end
175
+
176
+ def get_public
177
+ @public
178
+ end
179
+
180
+ def permissions_header
181
+ permissions = {}
182
+ if self.get_public.any?
183
+ permissions["PUBLIC"] = self.get_public.join(",")
184
+ end
185
+
186
+ result = []
187
+
188
+ permissions.each do |k, v|
189
+ result << "#{k}:[#{v}]"
190
+ end
191
+
192
+ {"X-PERMISSIONS" => "{#{result.join ","}}"}
193
+ end
194
+
195
+ private
196
+ def opts
197
+ if self.class.class_variable_defined? :@@opts
198
+ self.class.class_variable_get :@@opts
199
+ else
200
+ {}
201
+ end
202
+ end
203
+
134
204
  end
135
205
  end
@@ -1,8 +1,14 @@
1
1
  module Medea
2
- class JasonBlob < JasonBase
2
+ class JasonBlob < Medea::JasonBase
3
3
  attr_accessor :parent, :attachment_name
4
4
 
5
5
  def initialize initialiser=nil, mode=:eager
6
+
7
+ @public = []
8
+ if opts[:public]
9
+ opts[:public].each {|i| @public << i}
10
+ end
11
+
6
12
  if initialiser
7
13
  if initialiser.is_a? Hash
8
14
  @parent = initialiser[:parent]
@@ -102,6 +108,8 @@ module Medea
102
108
  #may also want to add any other indexable fields that the user specifies?
103
109
  }
104
110
 
111
+ post_headers.merge! permissions_header
112
+
105
113
  resp = RestClient.post to_url, contents, post_headers
106
114
 
107
115
  if resp.code == 201
@@ -23,7 +23,8 @@ module JasonDB
23
23
  protocol << "s"
24
24
  "#{protocol}://#{user}:#{password}@#{host}/#{topic}/"
25
25
  else #mode == :public
26
- "#{protocol}://#{host}/#{topic}/"
26
+ #TODO Remove the dummy "a:a" here...
27
+ "#{protocol}://a:a@#{host}/#{topic}/"
27
28
  end
28
29
  end
29
30
 
@@ -5,10 +5,7 @@ module Medea
5
5
  require 'json'
6
6
  require 'uuidtools'
7
7
 
8
- class JasonObject < JasonBase
9
-
10
- #these verbs are able to be made public
11
- HTTP_VERBS = [:GET, :POST, :PUT, :DELETE]
8
+ class JasonObject < Medea::JasonBase
12
9
 
13
10
  include Medea::ActiveModelMethods
14
11
  if defined? ActiveModel
@@ -192,30 +189,6 @@ module Medea
192
189
  persist_changes :post
193
190
  end
194
191
 
195
- def add_public *args
196
- args.reject! do |i|
197
- not HTTP_VERBS.include? i
198
- end
199
- @public += args
200
- @public.uniq!
201
- end
202
-
203
- def remove_public *args
204
- args.reject! do |i|
205
- not HTTP_VERBS.include? i
206
- end
207
- @public -= args
208
- @public.uniq!
209
- end
210
-
211
- def set_public *args
212
- args.reject! do |i|
213
- not HTTP_VERBS.include? i
214
- end
215
- @public = args
216
- @public.uniq!
217
- end
218
-
219
192
  def to_url mode=:secure
220
193
  "#{JasonDB::db_auth_url mode}#{self.class.name}/#{self.jason_key}"
221
194
  end
@@ -250,9 +223,7 @@ module Medea
250
223
  end
251
224
  end
252
225
 
253
- if @public.any?
254
- post_headers["X-PUBLIC"] = @public.join(",")
255
- end
226
+ post_headers.merge! permissions_header
256
227
 
257
228
  url = to_url()
258
229
 
@@ -280,13 +251,6 @@ module Medea
280
251
 
281
252
  #end object persistence
282
253
 
283
- private
284
- def opts
285
- if self.class.class_variable_defined? :@@opts
286
- self.class.class_variable_get :@@opts
287
- else
288
- {}
289
- end
290
- end
254
+
291
255
  end
292
256
  end
@@ -1,5 +1,5 @@
1
1
  module Medea
2
- VERSION = "0.6.9"
2
+ VERSION = "0.7.0"
3
3
 
4
4
  #When the templates are changed, this version should be incremented
5
5
  #This version is used when uploading/updating the templates
@@ -33,7 +33,7 @@ describe "Deferred Query" do
33
33
  end
34
34
 
35
35
  it "should be able to fetch those since a particular time" do
36
- u = User.all[10]
36
+ u = User.all[User.all.count - 1]
37
37
  User.all(:since => u.jason_timestamp).each do |i|
38
38
  i.jason_timestamp.should be >= u.jason_timestamp
39
39
  end
@@ -121,13 +121,13 @@ describe "JasonObject" do
121
121
 
122
122
  it "should post security information properly" do
123
123
  @user.add_public :GET, :POST
124
- RestClient.should_receive(:post).with(anything(), anything(), hash_including("X-PUBLIC" => "GET,POST")).and_return(DummyResponse.new)
124
+ RestClient.should_receive(:post).with(anything(), anything(), hash_including("X-PERMISSIONS" => "{PUBLIC:[GET,POST]}")).and_return(DummyResponse.new)
125
125
  @user.save!
126
126
  end
127
127
 
128
128
  it "should only post valid verbs" do
129
129
  @user.add_public :GET, :FAKEVERB
130
- RestClient.should_receive(:post).with(anything(), anything(), hash_including("X-PUBLIC" => "GET")).and_return(DummyResponse.new)
130
+ RestClient.should_receive(:post).with(anything(), anything(), hash_including("X-PERMISSIONS" => "{PUBLIC:[GET]}")).and_return(DummyResponse.new)
131
131
  @user.save!
132
132
  end
133
133
 
@@ -135,6 +135,6 @@ describe "JasonObject" do
135
135
  @user.set_public :GET, :POST
136
136
  @user.save!
137
137
  retrieved_user = User.get_by_key @user.jason_key
138
- (retrieved_user.send(:instance_variable_get, :@public)).should eq(["GET", "POST"])
138
+ retrieved_user.get_public.should eq(["GET", "POST"])
139
139
  end
140
140
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: medea
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.9
5
+ version: 0.7.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Michael Jensen
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-24 00:00:00 +11:00
13
+ date: 2011-02-28 00:00:00 +11:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency