sinatra_resource 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.3.2
@@ -45,6 +45,12 @@ module DataCatalog
45
45
  s = "#{Time.now.to_f}#{salt}#{rand(100_000_000)}#{name}#{email}"
46
46
  Digest::SHA1.hexdigest(s)
47
47
  end
48
+
49
+ # == Various Instance Methods
50
+
51
+ def token
52
+ Digest::SHA1.hexdigest("#{id}/#{email}/123456789")
53
+ end
48
54
 
49
55
  end
50
56
 
@@ -20,6 +20,10 @@ module DataCatalog
20
20
  property :email, :r => :owner
21
21
  property :role, :r => :owner, :w => :admin
22
22
  property :_api_key, :r => :owner, :w => :admin
23
+
24
+ property :token, :r => :owner do |user|
25
+ user.token
26
+ end
23
27
 
24
28
  # == Callbacks
25
29
  end
@@ -78,7 +78,8 @@ class UsersGetManyResourceTest < ResourceTestCase
78
78
  test "body elements should be correct" do
79
79
  parsed_response_body.each do |element|
80
80
  if element["id"] == user_for(role).id
81
- assert_properties(%w(name email role _api_key id created_at updated_at), element)
81
+ assert_properties(%w(name email role _api_key token
82
+ id created_at updated_at), element)
82
83
  else
83
84
  assert_properties(%w(name id created_at updated_at), element)
84
85
  end
@@ -109,7 +110,7 @@ class UsersGetManyResourceTest < ResourceTestCase
109
110
  assert_equal EMAILS, actual.sort
110
111
  end
111
112
 
112
- docs_properties %w(name email role _api_key id created_at updated_at)
113
+ docs_properties %w(name email role _api_key token id created_at updated_at)
113
114
  end
114
115
  end
115
116
 
@@ -47,29 +47,31 @@ class UsersGetOneResourceTest < ResourceTestCase
47
47
  before do
48
48
  get "/#{@user.id}", :api_key => api_key_for(role)
49
49
  end
50
-
50
+
51
51
  use "return 200 Ok"
52
52
  doc_properties %w(name id created_at updated_at)
53
53
  end
54
54
  end
55
-
55
+
56
56
  context "owner : get /:id" do
57
57
  before do
58
58
  get "/#{@user.id}", :api_key => @user._api_key
59
59
  end
60
-
60
+
61
61
  use "return 200 Ok"
62
- doc_properties %w(name email role _api_key id created_at updated_at)
62
+ doc_properties %w(name email role _api_key token
63
+ id created_at updated_at)
63
64
  end
64
-
65
+
65
66
  %w(admin).each do |role|
66
67
  context "#{role} : get /:id" do
67
68
  before do
68
69
  get "/#{@user.id}", :api_key => api_key_for(role)
69
70
  end
70
-
71
+
71
72
  use "return 200 Ok"
72
- doc_properties %w(name email role _api_key id created_at updated_at)
73
+ doc_properties %w(name email role _api_key token
74
+ id created_at updated_at)
73
75
  end
74
76
  end
75
77
 
@@ -110,7 +110,8 @@ class UsersPostResourceTest < ResourceTestCase
110
110
  use "return 201 Created"
111
111
  location_header "users"
112
112
  use "one new user"
113
- doc_properties %w(name email role _api_key id created_at updated_at)
113
+ doc_properties %w(name email role _api_key token
114
+ id created_at updated_at)
114
115
 
115
116
  test "should set all fields in database" do
116
117
  user = User.find_by_id(parsed_response_body["id"])
@@ -162,7 +162,8 @@ class UsersPutResourceTest < ResourceTestCase
162
162
  end
163
163
 
164
164
  use "return 200 Ok"
165
- doc_properties %w(name email role _api_key id created_at updated_at)
165
+ doc_properties %w(name email role _api_key token
166
+ id created_at updated_at)
166
167
 
167
168
  test "should change correct fields in database" do
168
169
  user = User.find_by_id(@user.id)
@@ -189,7 +190,8 @@ class UsersPutResourceTest < ResourceTestCase
189
190
  end
190
191
 
191
192
  use "return 200 Ok"
192
- doc_properties %w(name email role _api_key id created_at updated_at)
193
+ doc_properties %w(name email role _api_key token
194
+ id created_at updated_at)
193
195
 
194
196
  test "should change all fields in database" do
195
197
  user = User.find_by_id(@user.id)
data/lib/resource.rb CHANGED
@@ -145,15 +145,20 @@ module SinatraResource
145
145
  raise DefinitionError, "property #{name.inspect} already declared in #{self}"
146
146
  end
147
147
  @resource_config[:properties][name] = {}
148
+
148
149
  if block
149
150
  @resource_config[:properties][name][:w] = :nobody
150
151
  @resource_config[:properties][name][:read_proc] = block
151
- else
152
- access_rules.each_pair do |kind, role|
153
- @resource_config[:roles].validate_role(role)
154
- @resource_config[:properties][name][kind] = role
152
+ if access_rules[:w] && access_rules[:w] != :nobody
153
+ raise DefinitionError, "property #{name.inspect} is using block " +
154
+ "form. :w => :nobody is assumed and cannot be overridden."
155
155
  end
156
156
  end
157
+
158
+ access_rules.each_pair do |kind, role|
159
+ @resource_config[:roles].validate_role(role)
160
+ @resource_config[:properties][name][kind] = role
161
+ end
157
162
  end
158
163
 
159
164
  # Declare a relation with a block of code.
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_resource}
8
- s.version = "0.3.1"
8
+ s.version = "0.3.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["David James"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David James