activeresource 5.0.0 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activeresource might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/MIT-LICENSE +20 -0
- data/README.rdoc +48 -10
- data/lib/active_resource.rb +7 -5
- data/lib/active_resource/active_job_serializer.rb +26 -0
- data/lib/active_resource/associations.rb +10 -10
- data/lib/active_resource/associations/builder/association.rb +5 -4
- data/lib/active_resource/associations/builder/belongs_to.rb +4 -2
- data/lib/active_resource/associations/builder/has_many.rb +3 -1
- data/lib/active_resource/associations/builder/has_one.rb +4 -2
- data/lib/active_resource/base.rb +141 -76
- data/lib/active_resource/callbacks.rb +3 -1
- data/lib/active_resource/collection.rb +5 -3
- data/lib/active_resource/connection.rb +70 -75
- data/lib/active_resource/custom_methods.rb +8 -6
- data/lib/active_resource/exceptions.rb +5 -3
- data/lib/active_resource/formats.rb +4 -2
- data/lib/active_resource/formats/json_format.rb +4 -1
- data/lib/active_resource/formats/xml_format.rb +4 -2
- data/lib/active_resource/http_mock.rb +18 -20
- data/lib/active_resource/log_subscriber.rb +14 -3
- data/lib/active_resource/railtie.rb +13 -3
- data/lib/active_resource/reflection.rb +10 -9
- data/lib/active_resource/schema.rb +5 -3
- data/lib/active_resource/singleton.rb +25 -26
- data/lib/active_resource/threadsafe_attributes.rb +30 -29
- data/lib/active_resource/validations.rb +12 -10
- data/lib/active_resource/version.rb +4 -2
- data/lib/activeresource.rb +3 -1
- metadata +9 -7
@@ -1,9 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveResource
|
2
4
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
3
5
|
def request(event)
|
4
6
|
result = event.payload[:result]
|
5
|
-
|
6
|
-
|
7
|
+
|
8
|
+
# When result is nil, the connection could not even be initiated
|
9
|
+
# with the server, so we log an internal synthetic error response (523).
|
10
|
+
code = result.try(:code) || 523 # matches CloudFlare's convention
|
11
|
+
message = result.try(:message) || "ActiveResource connection error"
|
12
|
+
body = result.try(:body) || ""
|
13
|
+
|
14
|
+
log_level_method = code.to_i < 400 ? :info : :error
|
15
|
+
|
16
|
+
send log_level_method, "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}"
|
17
|
+
send log_level_method, "--> %d %s %d (%.1fms)" % [code, message, body.to_s.length, event.duration]
|
7
18
|
end
|
8
19
|
|
9
20
|
def logger
|
@@ -12,4 +23,4 @@ module ActiveResource
|
|
12
23
|
end
|
13
24
|
end
|
14
25
|
|
15
|
-
ActiveResource::LogSubscriber.attach_to :active_resource
|
26
|
+
ActiveResource::LogSubscriber.attach_to :active_resource
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "active_resource"
|
2
4
|
require "rails"
|
3
5
|
|
@@ -6,10 +8,18 @@ module ActiveResource
|
|
6
8
|
config.active_resource = ActiveSupport::OrderedOptions.new
|
7
9
|
|
8
10
|
initializer "active_resource.set_configs" do |app|
|
9
|
-
|
10
|
-
|
11
|
+
ActiveSupport.on_load(:active_resource) do
|
12
|
+
app.config.active_resource.each do |k, v|
|
13
|
+
send "#{k}=", v
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
initializer "active_resource.add_active_job_serializer" do |app|
|
19
|
+
if defined? app.config.active_job.custom_serializers
|
20
|
+
require "active_resource/active_job_serializer"
|
21
|
+
app.config.active_job.custom_serializers << ActiveResource::ActiveJobSerializer
|
11
22
|
end
|
12
23
|
end
|
13
24
|
end
|
14
25
|
end
|
15
|
-
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/class/attribute"
|
4
|
+
require "active_support/core_ext/module/deprecation"
|
3
5
|
|
4
6
|
module ActiveResource
|
5
7
|
# = Active Resource reflection
|
@@ -25,7 +27,6 @@ module ActiveResource
|
|
25
27
|
|
26
28
|
|
27
29
|
class AssociationReflection
|
28
|
-
|
29
30
|
def initialize(macro, name, options)
|
30
31
|
@macro, @name, @options = macro, name, options
|
31
32
|
end
|
@@ -65,13 +66,13 @@ module ActiveResource
|
|
65
66
|
end
|
66
67
|
|
67
68
|
private
|
68
|
-
|
69
|
-
|
70
|
-
|
69
|
+
def derive_class_name
|
70
|
+
(options[:class_name] ? options[:class_name].to_s.camelize : name.to_s.classify)
|
71
|
+
end
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
73
|
+
def derive_foreign_key
|
74
|
+
options[:foreign_key] ? options[:foreign_key].to_s : "#{name.to_s.downcase}_id"
|
75
|
+
end
|
75
76
|
end
|
76
77
|
end
|
77
78
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveResource # :nodoc:
|
2
4
|
class Schema # :nodoc:
|
3
5
|
# attributes can be known to be one of these types. They are easy to
|
@@ -29,8 +31,8 @@ module ActiveResource # :nodoc:
|
|
29
31
|
|
30
32
|
the_type = type.to_s
|
31
33
|
# TODO: add defaults
|
32
|
-
#the_attr = [type.to_s]
|
33
|
-
#the_attr << options[:default] if options.has_key? :default
|
34
|
+
# the_attr = [type.to_s]
|
35
|
+
# the_attr << options[:default] if options.has_key? :default
|
34
36
|
@attrs[name.to_s] = the_type
|
35
37
|
self
|
36
38
|
end
|
@@ -45,7 +47,7 @@ module ActiveResource # :nodoc:
|
|
45
47
|
# attr_names.each { |name| attribute(name, 'string', options) }
|
46
48
|
# end
|
47
49
|
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
48
|
-
def #{attr_type
|
50
|
+
def #{attr_type}(*args)
|
49
51
|
options = args.extract_options!
|
50
52
|
attr_names = args
|
51
53
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveResource
|
2
4
|
module Singleton
|
3
5
|
extend ActiveSupport::Concern
|
@@ -60,20 +62,19 @@ module ActiveResource
|
|
60
62
|
#
|
61
63
|
# Inventory.find
|
62
64
|
# # => raises ResourceNotFound
|
63
|
-
def find(options={})
|
65
|
+
def find(options = {})
|
64
66
|
find_singleton(options)
|
65
67
|
end
|
66
68
|
|
67
69
|
private
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
70
|
+
# Find singleton resource
|
71
|
+
def find_singleton(options)
|
72
|
+
prefix_options, query_options = split_options(options[:params])
|
73
|
+
|
74
|
+
path = singleton_path(prefix_options, query_options)
|
75
|
+
resp = self.format.decode(self.connection.get(path, self.headers).body)
|
76
|
+
instantiate_record(resp, prefix_options)
|
77
|
+
end
|
77
78
|
end
|
78
79
|
# Deletes the resource from the remote service.
|
79
80
|
#
|
@@ -88,27 +89,25 @@ module ActiveResource
|
|
88
89
|
|
89
90
|
protected
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
92
|
+
# Update the resource on the remote service
|
93
|
+
def update
|
94
|
+
connection.put(singleton_path(prefix_options), encode, self.class.headers).tap do |response|
|
95
|
+
load_attributes_from_response(response)
|
96
|
+
end
|
95
97
|
end
|
96
|
-
end
|
97
98
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
99
|
+
# Create (i.e. \save to the remote service) the \new resource.
|
100
|
+
def create
|
101
|
+
connection.post(singleton_path, encode, self.class.headers).tap do |response|
|
102
|
+
self.id = id_from_response(response)
|
103
|
+
load_attributes_from_response(response)
|
104
|
+
end
|
103
105
|
end
|
104
|
-
end
|
105
106
|
|
106
107
|
private
|
107
108
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
109
|
+
def singleton_path(options = nil)
|
110
|
+
self.class.singleton_path(options || prefix_options)
|
111
|
+
end
|
112
112
|
end
|
113
|
-
|
114
113
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/object/duplicable"
|
2
4
|
|
3
5
|
module ThreadsafeAttributes
|
4
6
|
def self.included(klass)
|
@@ -27,39 +29,38 @@ module ThreadsafeAttributes
|
|
27
29
|
|
28
30
|
private
|
29
31
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
32
|
+
def get_threadsafe_attribute(name, main_thread)
|
33
|
+
if threadsafe_attribute_defined_by_thread?(name, Thread.current)
|
34
|
+
get_threadsafe_attribute_by_thread(name, Thread.current)
|
35
|
+
elsif threadsafe_attribute_defined_by_thread?(name, main_thread)
|
36
|
+
value = get_threadsafe_attribute_by_thread(name, main_thread)
|
37
|
+
value = value.dup if value.duplicable?
|
38
|
+
set_threadsafe_attribute_by_thread(name, value, Thread.current)
|
39
|
+
value
|
40
|
+
end
|
38
41
|
end
|
39
|
-
end
|
40
42
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
43
|
+
def set_threadsafe_attribute(name, value, main_thread)
|
44
|
+
set_threadsafe_attribute_by_thread(name, value, Thread.current)
|
45
|
+
unless threadsafe_attribute_defined_by_thread?(name, main_thread)
|
46
|
+
set_threadsafe_attribute_by_thread(name, value, main_thread)
|
47
|
+
end
|
45
48
|
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def threadsafe_attribute_defined?(name, main_thread)
|
49
|
-
threadsafe_attribute_defined_by_thread?(name, Thread.current) || ((Thread.current != main_thread) && threadsafe_attribute_defined_by_thread?(name, main_thread))
|
50
|
-
end
|
51
49
|
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
def threadsafe_attribute_defined?(name, main_thread)
|
51
|
+
threadsafe_attribute_defined_by_thread?(name, Thread.current) || ((Thread.current != main_thread) && threadsafe_attribute_defined_by_thread?(name, main_thread))
|
52
|
+
end
|
55
53
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
54
|
+
def get_threadsafe_attribute_by_thread(name, thread)
|
55
|
+
thread.thread_variable_get "active.resource.#{name}.#{self.object_id}"
|
56
|
+
end
|
60
57
|
|
61
|
-
|
62
|
-
|
63
|
-
|
58
|
+
def set_threadsafe_attribute_by_thread(name, value, thread)
|
59
|
+
thread.thread_variable_set "active.resource.#{name}.#{self.object_id}.defined", true
|
60
|
+
thread.thread_variable_set "active.resource.#{name}.#{self.object_id}", value
|
61
|
+
end
|
64
62
|
|
63
|
+
def threadsafe_attribute_defined_by_thread?(name, thread)
|
64
|
+
thread.thread_variable_get "active.resource.#{name}.#{self.object_id}.defined"
|
65
|
+
end
|
65
66
|
end
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/core_ext/array/wrap"
|
4
|
+
require "active_support/core_ext/object/blank"
|
3
5
|
|
4
6
|
module ActiveResource
|
5
7
|
class ResourceInvalid < ClientError #:nodoc:
|
@@ -33,11 +35,11 @@ module ActiveResource
|
|
33
35
|
def from_hash(messages, save_cache = false)
|
34
36
|
clear unless save_cache
|
35
37
|
|
36
|
-
messages.each do |(key,errors)|
|
38
|
+
messages.each do |(key, errors)|
|
37
39
|
errors.each do |error|
|
38
40
|
if @base.known_attributes.include?(key)
|
39
41
|
add key, error
|
40
|
-
elsif key ==
|
42
|
+
elsif key == "base"
|
41
43
|
self[:base] << error
|
42
44
|
else
|
43
45
|
# reporting an error on an attribute not in attributes
|
@@ -51,11 +53,11 @@ module ActiveResource
|
|
51
53
|
# Grabs errors from a json response.
|
52
54
|
def from_json(json, save_cache = false)
|
53
55
|
decoded = ActiveSupport::JSON.decode(json) || {} rescue {}
|
54
|
-
if decoded.kind_of?(Hash) && (decoded.has_key?(
|
55
|
-
errors = decoded[
|
56
|
+
if decoded.kind_of?(Hash) && (decoded.has_key?("errors") || decoded.empty?)
|
57
|
+
errors = decoded["errors"] || {}
|
56
58
|
if errors.kind_of?(Array)
|
57
59
|
# 3.2.1-style with array of strings
|
58
|
-
ActiveSupport::Deprecation.warn(
|
60
|
+
ActiveSupport::Deprecation.warn("Returning errors as an array of strings is deprecated.")
|
59
61
|
from_array errors, save_cache
|
60
62
|
else
|
61
63
|
# 3.2.2+ style
|
@@ -70,7 +72,7 @@ module ActiveResource
|
|
70
72
|
|
71
73
|
# Grabs errors from an XML response.
|
72
74
|
def from_xml(xml, save_cache = false)
|
73
|
-
array = Array.wrap(Hash.from_xml(xml)[
|
75
|
+
array = Array.wrap(Hash.from_xml(xml)["errors"]["error"]) rescue []
|
74
76
|
from_array array, save_cache
|
75
77
|
end
|
76
78
|
end
|
@@ -106,7 +108,7 @@ module ActiveResource
|
|
106
108
|
|
107
109
|
# Validate a resource and save (POST) it to the remote web service.
|
108
110
|
# If any local validations fail - the save (POST) will not be attempted.
|
109
|
-
def save_with_validation(options={})
|
111
|
+
def save_with_validation(options = {})
|
110
112
|
perform_validation = options[:validate] != false
|
111
113
|
|
112
114
|
# clear the remote validations so they don't interfere with the local
|
@@ -131,7 +133,7 @@ module ActiveResource
|
|
131
133
|
|
132
134
|
# Loads the set of remote errors into the object's Errors based on the
|
133
135
|
# content-type of the error-block received.
|
134
|
-
def load_remote_errors(remote_errors, save_cache = false
|
136
|
+
def load_remote_errors(remote_errors, save_cache = false) #:nodoc:
|
135
137
|
case self.class.format
|
136
138
|
when ActiveResource::Formats[:xml]
|
137
139
|
errors.from_xml(remote_errors.response.body, save_cache)
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveResource
|
2
4
|
module VERSION #:nodoc:
|
3
5
|
MAJOR = 5
|
4
|
-
MINOR =
|
6
|
+
MINOR = 1
|
5
7
|
TINY = 0
|
6
8
|
PRE = nil
|
7
9
|
|
8
|
-
STRING = [MAJOR, MINOR, TINY, PRE].compact.join(
|
10
|
+
STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
|
9
11
|
end
|
10
12
|
end
|
data/lib/activeresource.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeresource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-11-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '5.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,7 @@ dependencies:
|
|
29
29
|
version: '5.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: activemodel
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
version: '5.0'
|
40
40
|
- - "<"
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '
|
42
|
+
version: '7'
|
43
43
|
type: :runtime
|
44
44
|
prerelease: false
|
45
45
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -49,7 +49,7 @@ dependencies:
|
|
49
49
|
version: '5.0'
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version: '
|
52
|
+
version: '7'
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: activemodel-serializers-xml
|
55
55
|
requirement: !ruby/object:Gem::Requirement
|
@@ -100,8 +100,10 @@ extensions: []
|
|
100
100
|
extra_rdoc_files:
|
101
101
|
- README.rdoc
|
102
102
|
files:
|
103
|
+
- MIT-LICENSE
|
103
104
|
- README.rdoc
|
104
105
|
- lib/active_resource.rb
|
106
|
+
- lib/active_resource/active_job_serializer.rb
|
105
107
|
- lib/active_resource/associations.rb
|
106
108
|
- lib/active_resource/associations/builder/association.rb
|
107
109
|
- lib/active_resource/associations/builder/belongs_to.rb
|
@@ -149,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
151
|
version: '0'
|
150
152
|
requirements: []
|
151
153
|
rubyforge_project:
|
152
|
-
rubygems_version: 2.
|
154
|
+
rubygems_version: 2.7.6
|
153
155
|
signing_key:
|
154
156
|
specification_version: 4
|
155
157
|
summary: REST modeling framework (part of Rails).
|