rapid_api 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rapid_api/action_controller/errors.rb +1 -1
- data/lib/rapid_api/action_controller/resource_actions.rb +21 -6
- data/lib/rapid_api/action_controller/scope.rb +7 -4
- data/lib/rapid_api/model_adapters/active_record_adapter.rb +9 -8
- data/lib/rapid_api/serializer_adapters/ams_adapter.rb +22 -4
- data/lib/rapid_api/services/base_service.rb +63 -0
- data/lib/rapid_api/services/service_response.rb +16 -0
- data/lib/rapid_api/services.rb +8 -0
- data/lib/rapid_api/version.rb +1 -1
- data/lib/rapid_api.rb +1 -0
- data/test/unit/rapid_api/action_controller/errors_test.rb +1 -1
- data/test/unit/rapid_api/action_controller/resource_actions_test.rb +2 -2
- data/test/unit/rapid_api/serializer_adapters/ams_adapter_test.rb +2 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd5b7f38b622bb70a7153cdb907e8261f9b0bce7160e63be0952d30b41b31217
|
4
|
+
data.tar.gz: cd08b0e83ac2fb977fdae26a3dc103c6b7ee839f63a48c24fc7a943bb510cb8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ab1900d7a216b238989bd3d66e0b5d3356537627b99dfc5082799b8ca48f771b81693ff50dab6ad4fb19c216fe56d73cdbe8f6f48b43b6cb685f15e4aff5c42e
|
7
|
+
data.tar.gz: bd176ec865a92ebf48f860baed9c478bdf3dc7b35a2e8574442f98c1ac2e04932ba69ff437f911eb3f3c8b13c7956840baddebddab844ff79ec89af43623a635
|
@@ -56,13 +56,27 @@ module RapidApi
|
|
56
56
|
def destroy
|
57
57
|
id = _adapted_serializer.deserialize_id(params, _params_key)
|
58
58
|
query_result = _adapted_model.destroy id, scope
|
59
|
-
if query_result.
|
60
|
-
|
59
|
+
if query_result.found?
|
60
|
+
if query_result.has_errors?
|
61
|
+
not_processable! query_result.errors
|
62
|
+
else
|
63
|
+
head :no_content
|
64
|
+
end
|
61
65
|
else
|
62
|
-
|
66
|
+
not_found!
|
63
67
|
end
|
64
68
|
end
|
65
69
|
|
70
|
+
def find_member
|
71
|
+
id = _adapted_serializer.deserialize_id(params, _params_key)
|
72
|
+
query_result = _adapted_model.find id, scope
|
73
|
+
@member = query_result
|
74
|
+
end
|
75
|
+
|
76
|
+
def render_member_ok(member)
|
77
|
+
render json: _adapted_serializer.serialize(member) , status: response_code_for(:ok)
|
78
|
+
end
|
79
|
+
|
66
80
|
private
|
67
81
|
|
68
82
|
def _adapted_model
|
@@ -115,10 +129,11 @@ module RapidApi
|
|
115
129
|
end
|
116
130
|
|
117
131
|
def serializer
|
118
|
-
|
119
|
-
|
132
|
+
module_name = self.name.to_s.deconstantize
|
133
|
+
serializer_name = "#{model_class_name}Serializer"
|
134
|
+
serializer_name = "#{module_name}::#{serializer_name}" unless module_name.empty?
|
120
135
|
@serializer ||= begin
|
121
|
-
|
136
|
+
serializer_name.constantize
|
122
137
|
rescue NameError
|
123
138
|
end
|
124
139
|
end
|
@@ -10,15 +10,18 @@ module RapidApi
|
|
10
10
|
@scope ||= {}
|
11
11
|
end
|
12
12
|
|
13
|
-
protected
|
14
|
-
|
15
13
|
def define_scope
|
16
|
-
|
14
|
+
tmp_scope_array = [*_scope_array]
|
17
15
|
self.class.scope_params.each_with_index do |key, index|
|
18
|
-
scope[key] =
|
16
|
+
scope[key] = tmp_scope_array[index]
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
20
|
+
protected
|
21
|
+
|
22
|
+
def _scope_array
|
23
|
+
[]
|
24
|
+
end
|
22
25
|
|
23
26
|
module ClassMethods
|
24
27
|
attr_accessor :scope_by_proc, :scope_params
|
@@ -7,16 +7,13 @@ module RapidApi
|
|
7
7
|
QueryResult.new data: member
|
8
8
|
end
|
9
9
|
|
10
|
-
def find_all(params=
|
11
|
-
params ||= {}
|
12
|
-
scope ||= {}
|
10
|
+
def find_all(params={}, scope={})
|
13
11
|
scoped_params = params.merge scope
|
14
12
|
collection = klass.where(scoped_params)
|
15
13
|
QueryResult.new data: collection
|
16
14
|
end
|
17
15
|
|
18
|
-
def create(params, scope=
|
19
|
-
scope ||= {}
|
16
|
+
def create(params, scope={})
|
20
17
|
create_params = params.merge scope
|
21
18
|
member = klass.create create_params
|
22
19
|
_query_result_for_member member
|
@@ -24,20 +21,24 @@ module RapidApi
|
|
24
21
|
|
25
22
|
def update(id, params, scope=nil)
|
26
23
|
member = _find_member_with_scope(id, scope)
|
27
|
-
member.
|
24
|
+
if member.present?
|
25
|
+
member.update_attributes params
|
26
|
+
end
|
28
27
|
_query_result_for_member member
|
29
28
|
end
|
30
29
|
|
31
30
|
def destroy(id, scope=nil)
|
32
31
|
member = _find_member_with_scope(id, scope)
|
33
|
-
member.
|
32
|
+
if member.present?
|
33
|
+
member.destroy
|
34
|
+
end
|
34
35
|
_query_result_for_member member
|
35
36
|
end
|
36
37
|
|
37
38
|
private
|
38
39
|
|
39
40
|
def _query_result_for_member(member)
|
40
|
-
QueryResult.new data: member, errors: member.errors
|
41
|
+
QueryResult.new data: member, errors: member.try(:errors)
|
41
42
|
end
|
42
43
|
|
43
44
|
def _find_member_with_scope(id, scope)
|
@@ -9,19 +9,37 @@ module RapidApi
|
|
9
9
|
|
10
10
|
def serialize_collection(collection)
|
11
11
|
collection_serializer = ActiveModel::Serializer::CollectionSerializer.new collection, {
|
12
|
-
|
12
|
+
serializer: klass
|
13
13
|
}
|
14
14
|
ActiveModelSerializers::Adapter.create(collection_serializer).to_json
|
15
15
|
end
|
16
16
|
|
17
17
|
def serialize_errors(query_result)
|
18
18
|
serializer = ActiveModel::Serializer::ErrorSerializer.new(query_result)
|
19
|
-
ActiveModelSerializers
|
19
|
+
if ActiveModelSerializers.config.adapter == :attributes
|
20
|
+
{ errors: serializer.as_json }
|
21
|
+
else
|
22
|
+
ActiveModelSerializers::Adapter.create(serializer).to_json
|
23
|
+
end
|
20
24
|
end
|
21
25
|
|
22
|
-
# NOTE: Need to add support for :attributes serialization
|
23
26
|
def deserialize_attributes(params, root_key)
|
24
|
-
|
27
|
+
#TODO: test attributes deserialization
|
28
|
+
if ActiveModelSerializers.config.adapter == :attributes
|
29
|
+
params.require(root_key)
|
30
|
+
else
|
31
|
+
attributes = params
|
32
|
+
if params[:data][:attributes].present?
|
33
|
+
attributes = params.require(:data).require(:attributes)
|
34
|
+
end
|
35
|
+
if params[:data][:relationships].present?
|
36
|
+
relationships = params.require(:data).require(:relationships)
|
37
|
+
relationships.keys.each do |attribute|
|
38
|
+
attributes["#{attribute}_id"] = relationships[attribute][:data].try(:[], :id)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
attributes
|
42
|
+
end
|
25
43
|
end
|
26
44
|
|
27
45
|
def deserialize_id(params, root_key)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module RapidApi
|
2
|
+
module Services
|
3
|
+
class RapidApi::Services::BaseService
|
4
|
+
|
5
|
+
attr_accessor :params,
|
6
|
+
:response,
|
7
|
+
:bang_mode
|
8
|
+
|
9
|
+
def self.method_missing(meth, *args, &block)
|
10
|
+
if meth[-1] == '!'
|
11
|
+
meth = meth.to_s.split('!')[0].to_sym
|
12
|
+
bang_mode = true
|
13
|
+
else
|
14
|
+
bang_mode = false
|
15
|
+
end
|
16
|
+
if instance_methods.include? meth
|
17
|
+
service = self.new(*args)
|
18
|
+
service.bang_mode = bang_mode
|
19
|
+
service.send(meth)
|
20
|
+
if bang_mode
|
21
|
+
return service.response.value
|
22
|
+
else
|
23
|
+
return service.response
|
24
|
+
end
|
25
|
+
else
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(params={})
|
31
|
+
@params = params
|
32
|
+
@response = ServiceResponse.new
|
33
|
+
after_initialize
|
34
|
+
end
|
35
|
+
|
36
|
+
def after_initialize
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_error(key, message)
|
40
|
+
raise "#{key}: #{message}" if bang_mode
|
41
|
+
|
42
|
+
error_key = response.errors[key]
|
43
|
+
error_key ||= []
|
44
|
+
error_key << message
|
45
|
+
end
|
46
|
+
|
47
|
+
def return_value(value)
|
48
|
+
response.value = value
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
|
53
|
+
def _transfer_errors_from_active_record(model)
|
54
|
+
if model.errors.any?
|
55
|
+
model.errors.keys.each do |k|
|
56
|
+
response.errors[k]= model.errors[k]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/rapid_api/version.rb
CHANGED
data/lib/rapid_api.rb
CHANGED
@@ -30,7 +30,7 @@ class ActionControllerErrorsTest < ActionController::TestCase
|
|
30
30
|
def test_unauthenticated_error
|
31
31
|
get :unauthenticated_action
|
32
32
|
assert_response :unauthorized
|
33
|
-
assert_equal ['Not
|
33
|
+
assert_equal ['Not Authorized'], JSON.parse(@controller.response.body)['errors']
|
34
34
|
end
|
35
35
|
|
36
36
|
def test_server_error
|
@@ -150,7 +150,7 @@ module ActionController
|
|
150
150
|
def test_destroy
|
151
151
|
BricksController.adapted_model = Minitest::Mock.new
|
152
152
|
BricksController.serializer = TestSerializerAdapter
|
153
|
-
query_result = RapidApi::ModelAdapters::QueryResult.new()
|
153
|
+
query_result = RapidApi::ModelAdapters::QueryResult.new(data: 'data')
|
154
154
|
BricksController.adapted_model.expect :destroy, query_result, ["1", {color: 'blue'}]
|
155
155
|
post :destroy, params: {id: 1}
|
156
156
|
assert_response :no_content
|
@@ -160,7 +160,7 @@ module ActionController
|
|
160
160
|
def test_destroy_errors
|
161
161
|
BricksController.adapted_model = Minitest::Mock.new
|
162
162
|
BricksController.serializer = TestSerializerAdapter
|
163
|
-
query_result = RapidApi::ModelAdapters::QueryResult.new(errors: 'dummy error')
|
163
|
+
query_result = RapidApi::ModelAdapters::QueryResult.new(data: 'data', errors: 'dummy error')
|
164
164
|
BricksController.adapted_model.expect :destroy, query_result, ["1", {color: 'blue'}]
|
165
165
|
post :destroy, params: {id: 1}
|
166
166
|
assert_response :unprocessable_entity
|
@@ -13,9 +13,9 @@ module RapidApi
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_serialize
|
16
|
-
brick = Brick.
|
16
|
+
brick = Brick.new color: 'yellow', weight: 10, material: 'paper'
|
17
17
|
serialized_brick = @adapter.serialize brick
|
18
|
-
assert_equal "{\"data\":{\"
|
18
|
+
assert_equal "{\"data\":{\"type\":\"bricks\",\"attributes\":{\"color\":\"yellow\",\"weight\":\"10.0\",\"material\":\"paper\"}}}", serialized_brick
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_serialize_collection
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rapid_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- briandavidwetzel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -125,6 +125,9 @@ files:
|
|
125
125
|
- lib/rapid_api/serializer_adapters.rb
|
126
126
|
- lib/rapid_api/serializer_adapters/abstract.rb
|
127
127
|
- lib/rapid_api/serializer_adapters/ams_adapter.rb
|
128
|
+
- lib/rapid_api/services.rb
|
129
|
+
- lib/rapid_api/services/base_service.rb
|
130
|
+
- lib/rapid_api/services/service_response.rb
|
128
131
|
- lib/rapid_api/version.rb
|
129
132
|
- test/integration/ams_ar_test.rb
|
130
133
|
- test/support/code_climate.rb
|