arrest 0.0.82 → 0.0.83.crud
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arrest/abstract_resource.rb +1 -1
- data/lib/arrest/attributes/attribute.rb +12 -3
- data/lib/arrest/attributes/belongs_to.rb +10 -9
- data/lib/arrest/attributes/belongs_to_attribute.rb +2 -2
- data/lib/arrest/attributes/has_attributes.rb +20 -14
- data/lib/arrest/attributes/nested_attribute.rb +2 -2
- data/lib/arrest/attributes/nested_collection.rb +2 -2
- data/lib/arrest/attributes/polymorphic_attribute.rb +6 -2
- data/lib/arrest/exceptions.rb +11 -1
- data/lib/arrest/helper/ordered_collection.rb +1 -0
- data/lib/arrest/root_resource.rb +15 -14
- data/lib/arrest/single_resource.rb +3 -3
- data/lib/arrest/transport/http_source.rb +8 -16
- data/lib/arrest/version.rb +1 -1
- metadata +26 -32
@@ -1,13 +1,22 @@
|
|
1
1
|
module Arrest
|
2
2
|
class Attribute
|
3
|
-
attr_accessor :name, :
|
4
|
-
|
3
|
+
attr_accessor :name, :actions, :clazz, :json_name
|
4
|
+
|
5
|
+
def initialize name, clazz, actions = nil
|
5
6
|
@name = name.to_sym
|
6
|
-
@
|
7
|
+
@actions = actions || [:create, :retrieve, :update, :delete]
|
7
8
|
@clazz = clazz
|
8
9
|
@json_name = Source.json_key_converter.key_to_json(name).to_sym
|
9
10
|
end
|
10
11
|
|
12
|
+
def read_only?
|
13
|
+
@actions == [:retrieve]
|
14
|
+
end
|
15
|
+
|
16
|
+
def mutable?
|
17
|
+
@actions.include?(:create) || @actions.include?(:update)
|
18
|
+
end
|
19
|
+
|
11
20
|
def from_hash(parent, value)
|
12
21
|
return if value == nil
|
13
22
|
|
@@ -17,11 +17,11 @@ module Arrest
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def create_and_add_attribute(field_name, polymorphic,
|
20
|
+
def create_and_add_attribute(field_name, polymorphic, actions, foreign_key, class_name)
|
21
21
|
if polymorphic
|
22
|
-
add_attribute(PolymorphicAttribute.new(field_name.to_sym,
|
22
|
+
add_attribute(PolymorphicAttribute.new(field_name.to_sym, actions))
|
23
23
|
else
|
24
|
-
add_attribute(BelongsToAttribute.new(field_name.to_sym,
|
24
|
+
add_attribute(BelongsToAttribute.new(field_name.to_sym, actions, String, foreign_key, class_name))
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -32,8 +32,9 @@ module Arrest
|
|
32
32
|
foreign_key = "#{StringUtils.underscore(ClassUtils.simple_name(self))}_id"
|
33
33
|
params = args[1] unless args.length < 2
|
34
34
|
|
35
|
+
actions = [:create, :retrieve, :update, :delete]
|
35
36
|
if params
|
36
|
-
|
37
|
+
actions = params[:actions] if params[:actions]
|
37
38
|
polymorphic = !!params[:polymorphic]
|
38
39
|
class_name = params[:class_name].to_s unless params[:class_name] == nil
|
39
40
|
foreign_key = params[:foreign_key].to_s unless params[:foreign_key] == nil
|
@@ -41,7 +42,7 @@ module Arrest
|
|
41
42
|
|
42
43
|
field_name = create_field_name(name, params, polymorphic)
|
43
44
|
|
44
|
-
create_and_add_attribute(field_name, polymorphic,
|
45
|
+
create_and_add_attribute(field_name, polymorphic, actions, foreign_key, class_name)
|
45
46
|
|
46
47
|
send :define_method, name do
|
47
48
|
val = self.send(field_name)
|
@@ -54,12 +55,12 @@ module Arrest
|
|
54
55
|
begin
|
55
56
|
if polymorphic
|
56
57
|
clazz = self.class.json_type_to_class(val.type)
|
57
|
-
|
58
|
+
id = val.id
|
58
59
|
else
|
59
|
-
Arrest::Source.mod.const_get(class_name)
|
60
|
+
clazz = Arrest::Source.mod.const_get(class_name)
|
61
|
+
id = val
|
60
62
|
end
|
61
|
-
|
62
|
-
raise Errors::DocumentNotFoundError, "Couldnt find a #{class_name} with id #{val}"
|
63
|
+
clazz.find(self.context, id)
|
63
64
|
end
|
64
65
|
|
65
66
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module Arrest
|
2
2
|
class BelongsToAttribute < Attribute
|
3
3
|
attr_accessor :foreign_key
|
4
|
-
def initialize(name,
|
5
|
-
super(name,
|
4
|
+
def initialize(name, actions, field_class, foreign_key, target_class_name)
|
5
|
+
super(name, field_class, actions)
|
6
6
|
@foreign_key = foreign_key
|
7
7
|
@target_class_name = target_class_name
|
8
8
|
end
|
@@ -61,6 +61,9 @@ module Arrest
|
|
61
61
|
end
|
62
62
|
value = as[key]
|
63
63
|
converted = field.from_hash(self, value)
|
64
|
+
|
65
|
+
@attribute_values[field.name.to_sym] = converted
|
66
|
+
|
64
67
|
self.send(field.name.to_s + '=', converted) unless converted == nil
|
65
68
|
end
|
66
69
|
end
|
@@ -93,13 +96,16 @@ module Arrest
|
|
93
96
|
end
|
94
97
|
|
95
98
|
|
96
|
-
def to_jhash
|
97
|
-
to_hash(false, true)
|
99
|
+
def to_jhash(action)
|
100
|
+
to_hash(false, true, action)
|
98
101
|
end
|
99
102
|
|
100
|
-
def to_hash(show_all_fields = true, json_names = false)
|
103
|
+
def to_hash(show_all_fields = true, json_names = false, action = nil)
|
101
104
|
result = {}
|
102
|
-
|
105
|
+
|
106
|
+
self.class.all_fields.find_all{|a| show_all_fields ||
|
107
|
+
!action ||
|
108
|
+
a.actions.include?(action)}.each do |field|
|
103
109
|
if json_names
|
104
110
|
json_name = field.json_name
|
105
111
|
else
|
@@ -107,9 +113,8 @@ module Arrest
|
|
107
113
|
end
|
108
114
|
val = self.send(field.name)
|
109
115
|
converted = field.to_hash val
|
110
|
-
|
111
|
-
|
112
|
-
end
|
116
|
+
|
117
|
+
result[json_name] = converted
|
113
118
|
end
|
114
119
|
result
|
115
120
|
end
|
@@ -122,8 +127,12 @@ module Arrest
|
|
122
127
|
end
|
123
128
|
|
124
129
|
def attribute(name, clazz, attribs = {})
|
125
|
-
|
126
|
-
|
130
|
+
if !!attribs[:read_only] && !attribs[:actions]
|
131
|
+
actions = [:retrieve]
|
132
|
+
else
|
133
|
+
actions = attribs[:actions]
|
134
|
+
end
|
135
|
+
add_attribute Attribute.new(name, clazz, actions)
|
127
136
|
end
|
128
137
|
|
129
138
|
def attributes(args)
|
@@ -140,7 +149,6 @@ module Arrest
|
|
140
149
|
Arrest::debug "setter #{self.class.name} #{attribute.name} = #{converted_v}"
|
141
150
|
self.attribute_values[attribute.name] = converted_v
|
142
151
|
end
|
143
|
-
|
144
152
|
# define getter for attribute value
|
145
153
|
send :define_method, "#{attribute.name}" do
|
146
154
|
Arrest::debug "getter #{self.class.name} #{attribute.name}"
|
@@ -156,13 +164,11 @@ module Arrest
|
|
156
164
|
end
|
157
165
|
|
158
166
|
def nested name, clazz, options = {}
|
159
|
-
|
160
|
-
add_attribute NestedAttribute.new(name, read_only, clazz)
|
167
|
+
add_attribute NestedAttribute.new(name, clazz, options[:actions])
|
161
168
|
end
|
162
169
|
|
163
170
|
def nested_array name, clazz, options = {}
|
164
|
-
|
165
|
-
add_attribute Arrest::NestedCollection.new(name, read_only, clazz)
|
171
|
+
add_attribute Arrest::NestedCollection.new(name, clazz, options[:actions])
|
166
172
|
end
|
167
173
|
end
|
168
174
|
|
@@ -4,11 +4,15 @@ module Arrest
|
|
4
4
|
|
5
5
|
attribute :id, String
|
6
6
|
attribute :type, String
|
7
|
+
|
8
|
+
def self.mk_json(value)
|
9
|
+
{:id => value.id, :type => value.type}.to_json
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
13
|
class PolymorphicAttribute < NestedAttribute
|
10
|
-
def initialize name,
|
11
|
-
super name,
|
14
|
+
def initialize name, actions
|
15
|
+
super name, Ref, actions
|
12
16
|
end
|
13
17
|
|
14
18
|
def from_hash(parent, value)
|
data/lib/arrest/exceptions.rb
CHANGED
@@ -1,7 +1,16 @@
|
|
1
1
|
module Arrest
|
2
2
|
module Errors
|
3
|
-
class DocumentNotFoundError < StandardError
|
4
3
|
|
4
|
+
class DocumentNotFoundError < StandardError
|
5
|
+
end
|
6
|
+
|
7
|
+
class SpecifiedDocumentNotFoundError < DocumentNotFoundError
|
8
|
+
attr_reader :id, :class_type
|
9
|
+
|
10
|
+
def initialize(id = nil, class_type = nil)
|
11
|
+
@id = id
|
12
|
+
@class_type = class_type
|
13
|
+
end
|
5
14
|
end
|
6
15
|
|
7
16
|
class PermissionDeniedError < StandardError
|
@@ -9,5 +18,6 @@ module Arrest
|
|
9
18
|
super(err_obj)
|
10
19
|
end
|
11
20
|
end
|
21
|
+
|
12
22
|
end
|
13
23
|
end
|
data/lib/arrest/root_resource.rb
CHANGED
@@ -43,20 +43,21 @@ module Arrest
|
|
43
43
|
|
44
44
|
def find(context, id)
|
45
45
|
context.cache.lookup(id) do
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
body = body_root(r)
|
53
|
-
if body == nil || body.empty?
|
54
|
-
Arrest::logger.info "DocumentNotFoundError for #{full_resource_path}"
|
55
|
-
raise Errors::DocumentNotFoundError.new
|
56
|
-
end
|
46
|
+
begin
|
47
|
+
raise "Document Id must not be blank" if id == nil || "" == id
|
48
|
+
|
49
|
+
full_resource_path = "#{self.resource_path}/#{id}"
|
50
|
+
r = source().get(context, full_resource_path)
|
51
|
+
body = body_root(r)
|
57
52
|
|
58
|
-
|
59
|
-
|
53
|
+
raise "Response body must not be empty for #{full_resource_path}" if body == nil || body.empty?
|
54
|
+
|
55
|
+
resource = self.build(context, body.merge({:id => id}))
|
56
|
+
resource
|
57
|
+
rescue Exception => e
|
58
|
+
Arrest::logger.info e.message if e.message
|
59
|
+
raise Errors::SpecifiedDocumentNotFoundError.new(id, self.class)
|
60
|
+
end
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
@@ -129,7 +130,7 @@ module Arrest
|
|
129
130
|
protected
|
130
131
|
def internal_reload
|
131
132
|
context.cache.remove(self.id)
|
132
|
-
self.class.find(self.context, self.id).to_hash
|
133
|
+
self.class.find(self.context, self.id).to_hash(false, false, :retrieve)
|
133
134
|
end
|
134
135
|
end
|
135
136
|
end
|
@@ -7,14 +7,14 @@ module Arrest
|
|
7
7
|
r = source().get(context, "#{self.resource_path}")
|
8
8
|
body = body_root(r)
|
9
9
|
if body == nil || body.empty?
|
10
|
-
Arrest::logger.info "
|
11
|
-
raise Errors::
|
10
|
+
Arrest::logger.info "SpecifiedDocumentNotFoundError for #{self.resource_path}"
|
11
|
+
raise Errors::SpecifiedDocumentNotFoundError.new(nil, self.class)
|
12
12
|
end
|
13
13
|
self.build(context, body)
|
14
14
|
end
|
15
15
|
|
16
16
|
def find(context, id)
|
17
|
-
raise "A find is not possible for a
|
17
|
+
raise "A find is not possible for a SingleResource"
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -84,8 +84,7 @@ module Arrest
|
|
84
84
|
|
85
85
|
def put(context, rest_resource)
|
86
86
|
raise "To change an object it must have an id" unless rest_resource.respond_to?(:id) && rest_resource.id != nil
|
87
|
-
hash = rest_resource.to_jhash
|
88
|
-
insert_nulls!(rest_resource,hash)
|
87
|
+
hash = rest_resource.to_jhash(:update)
|
89
88
|
hash.delete(:id)
|
90
89
|
hash.delete("id")
|
91
90
|
body = JSON.generate(hash)
|
@@ -96,7 +95,9 @@ module Arrest
|
|
96
95
|
def internal_put(rest_resource, location, body)
|
97
96
|
profiler_status_str = ""
|
98
97
|
::ActiveSupport::Notifications.instrument("http.sgdb",
|
99
|
-
|
98
|
+
:method => :delete,
|
99
|
+
:url => rest_resource.resource_location,
|
100
|
+
:status => profiler_status_str) do
|
100
101
|
headers = nil
|
101
102
|
response = self.connection().put do |req|
|
102
103
|
req.url(location)
|
@@ -114,27 +115,18 @@ module Arrest
|
|
114
115
|
end
|
115
116
|
end
|
116
117
|
|
117
|
-
def insert_nulls!(rest_resource, hash)
|
118
|
-
rest_resource.class.all_fields.each do |field|
|
119
|
-
changed = (!rest_resource.respond_to?("#{field.name}_changed?" || rest_resource.send("#{field}_changed?")))
|
120
|
-
if !field.read_only && hash[field.json_name] == nil && changed
|
121
|
-
hash[field.json_name] = Null.singleton
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
118
|
def post(context, rest_resource)
|
127
119
|
profiler_status_str = ""
|
128
120
|
::ActiveSupport::Notifications.instrument("http.sgdb",
|
129
|
-
|
121
|
+
:method => :post,
|
122
|
+
:url => rest_resource.resource_path,
|
123
|
+
:status => profiler_status_str) do
|
130
124
|
raise "new object must have setter for id" unless rest_resource.respond_to?(:id=)
|
131
125
|
raise "new object must not have id" if rest_resource.respond_to?(:id) && rest_resource.id != nil
|
132
|
-
hash = rest_resource.to_jhash
|
126
|
+
hash = rest_resource.to_jhash(:create)
|
133
127
|
hash.delete(:id)
|
134
128
|
hash.delete('id')
|
135
129
|
|
136
|
-
insert_nulls!(rest_resource,hash)
|
137
|
-
|
138
130
|
body = JSON.generate(hash)
|
139
131
|
headers = nil
|
140
132
|
response = self.connection().post do |req|
|
data/lib/arrest/version.rb
CHANGED
metadata
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.83.crud
|
5
|
+
prerelease: 7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Axel Tetzlaff
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-09-03 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &26670600 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *26670600
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: faraday
|
27
|
-
requirement: &
|
27
|
+
requirement: &26670100 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.7.5
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *26670100
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &26669600 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '3'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *26669600
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &26669140 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.0.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *26669140
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &26668720 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *26668720
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rdoc
|
71
|
-
requirement: &
|
71
|
+
requirement: &26668240 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *26668240
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
requirement: &
|
82
|
+
requirement: &26667540 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '2'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *26667540
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rr
|
93
|
-
requirement: &
|
93
|
+
requirement: &26667000 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *26667000
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: simplecov
|
104
|
-
requirement: &
|
104
|
+
requirement: &26666380 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *26666380
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: rack
|
115
|
-
requirement: &
|
115
|
+
requirement: &26665680 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,7 +120,7 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *26665680
|
124
124
|
description: Consume a rest API in a AR like fashion
|
125
125
|
email:
|
126
126
|
- axel.tetzlaff@fortytools.com
|
@@ -185,21 +185,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
185
185
|
- - ! '>='
|
186
186
|
- !ruby/object:Gem::Version
|
187
187
|
version: '0'
|
188
|
-
segments:
|
189
|
-
- 0
|
190
|
-
hash: 1138449475091621378
|
191
188
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
189
|
none: false
|
193
190
|
requirements:
|
194
|
-
- - ! '
|
191
|
+
- - ! '>'
|
195
192
|
- !ruby/object:Gem::Version
|
196
|
-
version:
|
197
|
-
segments:
|
198
|
-
- 0
|
199
|
-
hash: 1138449475091621378
|
193
|
+
version: 1.3.1
|
200
194
|
requirements: []
|
201
195
|
rubyforge_project: arrest
|
202
|
-
rubygems_version: 1.8.
|
196
|
+
rubygems_version: 1.8.10
|
203
197
|
signing_key:
|
204
198
|
specification_version: 3
|
205
199
|
summary: Another ruby rest client
|