activeresource 2.1.1 → 2.1.2
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.
- data/CHANGELOG +5 -0
- data/Rakefile +1 -1
- data/lib/active_resource/base.rb +4 -9
- data/lib/active_resource/connection.rb +7 -18
- data/lib/active_resource/custom_methods.rb +9 -10
- data/lib/active_resource/formats/json_format.rb +6 -6
- data/lib/active_resource/formats/xml_format.rb +9 -9
- data/lib/active_resource/http_mock.rb +1 -1
- data/lib/active_resource/version.rb +1 -1
- data/test/base/custom_methods_test.rb +2 -1
- data/test/base_test.rb +1 -1
- data/test/format_test.rb +11 -23
- metadata +3 -3
data/CHANGELOG
CHANGED
data/Rakefile
CHANGED
@@ -66,7 +66,7 @@ spec = Gem::Specification.new do |s|
|
|
66
66
|
s.files = s.files + Dir.glob( "#{dir}/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
|
67
67
|
end
|
68
68
|
|
69
|
-
s.add_dependency('activesupport', '= 2.1.
|
69
|
+
s.add_dependency('activesupport', '= 2.1.2' + PKG_BUILD)
|
70
70
|
|
71
71
|
s.require_path = 'lib'
|
72
72
|
s.autorequire = 'active_resource'
|
data/lib/active_resource/base.rb
CHANGED
@@ -843,13 +843,8 @@ module ActiveResource
|
|
843
843
|
#
|
844
844
|
# my_group.to_xml(:skip_instruct => true)
|
845
845
|
# # => <subsidiary_group> [...] </subsidiary_group>
|
846
|
-
def
|
847
|
-
|
848
|
-
when ActiveResource::Formats[:xml]
|
849
|
-
self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options))
|
850
|
-
else
|
851
|
-
self.class.format.encode(attributes, options)
|
852
|
-
end
|
846
|
+
def to_xml(options={})
|
847
|
+
attributes.to_xml({:root => self.class.element_name}.merge(options))
|
853
848
|
end
|
854
849
|
|
855
850
|
# A method to reload the attributes of this object from the remote web service.
|
@@ -934,14 +929,14 @@ module ActiveResource
|
|
934
929
|
|
935
930
|
# Update the resource on the remote service.
|
936
931
|
def update
|
937
|
-
returning connection.put(element_path(prefix_options),
|
932
|
+
returning connection.put(element_path(prefix_options), to_xml, self.class.headers) do |response|
|
938
933
|
load_attributes_from_response(response)
|
939
934
|
end
|
940
935
|
end
|
941
936
|
|
942
937
|
# Create (i.e., save to the remote service) the new resource.
|
943
938
|
def create
|
944
|
-
returning connection.post(collection_path,
|
939
|
+
returning connection.post(collection_path, to_xml, self.class.headers) do |response|
|
945
940
|
self.id = id_from_response(response)
|
946
941
|
load_attributes_from_response(response)
|
947
942
|
end
|
@@ -63,13 +63,6 @@ module ActiveResource
|
|
63
63
|
# This class is used by ActiveResource::Base to interface with REST
|
64
64
|
# services.
|
65
65
|
class Connection
|
66
|
-
|
67
|
-
HTTP_FORMAT_HEADER_NAMES = { :get => 'Accept',
|
68
|
-
:put => 'Content-Type',
|
69
|
-
:post => 'Content-Type',
|
70
|
-
:delete => 'Accept'
|
71
|
-
}
|
72
|
-
|
73
66
|
attr_reader :site, :user, :password, :timeout
|
74
67
|
attr_accessor :format
|
75
68
|
|
@@ -113,25 +106,25 @@ module ActiveResource
|
|
113
106
|
# Execute a GET request.
|
114
107
|
# Used to get (find) resources.
|
115
108
|
def get(path, headers = {})
|
116
|
-
format.decode(request(:get, path, build_request_headers(headers
|
109
|
+
format.decode(request(:get, path, build_request_headers(headers)).body)
|
117
110
|
end
|
118
111
|
|
119
112
|
# Execute a DELETE request (see HTTP protocol documentation if unfamiliar).
|
120
113
|
# Used to delete resources.
|
121
114
|
def delete(path, headers = {})
|
122
|
-
request(:delete, path, build_request_headers(headers
|
115
|
+
request(:delete, path, build_request_headers(headers))
|
123
116
|
end
|
124
117
|
|
125
118
|
# Execute a PUT request (see HTTP protocol documentation if unfamiliar).
|
126
119
|
# Used to update resources.
|
127
120
|
def put(path, body = '', headers = {})
|
128
|
-
request(:put, path, body.to_s, build_request_headers(headers
|
121
|
+
request(:put, path, body.to_s, build_request_headers(headers))
|
129
122
|
end
|
130
123
|
|
131
124
|
# Execute a POST request.
|
132
125
|
# Used to create new resources.
|
133
126
|
def post(path, body = '', headers = {})
|
134
|
-
request(:post, path, body.to_s, build_request_headers(headers
|
127
|
+
request(:post, path, body.to_s, build_request_headers(headers))
|
135
128
|
end
|
136
129
|
|
137
130
|
# Execute a HEAD request.
|
@@ -194,12 +187,12 @@ module ActiveResource
|
|
194
187
|
end
|
195
188
|
|
196
189
|
def default_header
|
197
|
-
@default_header ||= {}
|
190
|
+
@default_header ||= { 'Content-Type' => format.mime_type }
|
198
191
|
end
|
199
192
|
|
200
193
|
# Builds headers for request to remote service.
|
201
|
-
def build_request_headers(headers
|
202
|
-
authorization_header.update(default_header).update(headers)
|
194
|
+
def build_request_headers(headers)
|
195
|
+
authorization_header.update(default_header).update(headers)
|
203
196
|
end
|
204
197
|
|
205
198
|
# Sets authorization header
|
@@ -207,10 +200,6 @@ module ActiveResource
|
|
207
200
|
(@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {})
|
208
201
|
end
|
209
202
|
|
210
|
-
def http_format_header(http_method)
|
211
|
-
{HTTP_FORMAT_HEADER_NAMES[http_method] => format.mime_type}
|
212
|
-
end
|
213
|
-
|
214
203
|
def logger #:nodoc:
|
215
204
|
ActiveResource::Base.logger
|
216
205
|
end
|
@@ -30,7 +30,7 @@ module ActiveResource
|
|
30
30
|
# Person.get(:active) # GET /people/active.xml
|
31
31
|
# # => [{:id => 1, :name => 'Ryan'}, {:id => 2, :name => 'Joe'}]
|
32
32
|
#
|
33
|
-
module CustomMethods
|
33
|
+
module CustomMethods
|
34
34
|
def self.included(base)
|
35
35
|
base.class_eval do
|
36
36
|
extend ActiveResource::CustomMethods::ClassMethods
|
@@ -83,25 +83,24 @@ module ActiveResource
|
|
83
83
|
"#{prefix(prefix_options)}#{collection_name}/#{method_name}.#{format.extension}#{query_string(query_options)}"
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
86
|
+
|
87
87
|
module InstanceMethods
|
88
88
|
def get(method_name, options = {})
|
89
89
|
connection.get(custom_method_element_url(method_name, options), self.class.headers)
|
90
90
|
end
|
91
|
-
|
92
|
-
def post(method_name, options = {}, body =
|
93
|
-
request_body = body.nil? ? encode : body
|
91
|
+
|
92
|
+
def post(method_name, options = {}, body = '')
|
94
93
|
if new?
|
95
|
-
connection.post(custom_method_new_element_url(method_name, options),
|
94
|
+
connection.post(custom_method_new_element_url(method_name, options), (body.nil? ? to_xml : body), self.class.headers)
|
96
95
|
else
|
97
|
-
connection.post(custom_method_element_url(method_name, options),
|
96
|
+
connection.post(custom_method_element_url(method_name, options), body, self.class.headers)
|
98
97
|
end
|
99
98
|
end
|
100
|
-
|
99
|
+
|
101
100
|
def put(method_name, options = {}, body = '')
|
102
101
|
connection.put(custom_method_element_url(method_name, options), body, self.class.headers)
|
103
102
|
end
|
104
|
-
|
103
|
+
|
105
104
|
def delete(method_name, options = {})
|
106
105
|
connection.delete(custom_method_element_url(method_name, options), self.class.headers)
|
107
106
|
end
|
@@ -111,7 +110,7 @@ module ActiveResource
|
|
111
110
|
def custom_method_element_url(method_name, options = {})
|
112
111
|
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}"
|
113
112
|
end
|
114
|
-
|
113
|
+
|
115
114
|
def custom_method_new_element_url(method_name, options = {})
|
116
115
|
"#{self.class.prefix(prefix_options)}#{self.class.collection_name}/new/#{method_name}.#{self.class.format.extension}#{self.class.send!(:query_string, options)}"
|
117
116
|
end
|
@@ -2,22 +2,22 @@ module ActiveResource
|
|
2
2
|
module Formats
|
3
3
|
module JsonFormat
|
4
4
|
extend self
|
5
|
-
|
5
|
+
|
6
6
|
def extension
|
7
7
|
"json"
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def mime_type
|
11
11
|
"application/json"
|
12
12
|
end
|
13
|
-
|
14
|
-
def encode(hash
|
13
|
+
|
14
|
+
def encode(hash)
|
15
15
|
hash.to_json
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def decode(json)
|
19
19
|
ActiveSupport::JSON.decode(json)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
|
-
end
|
23
|
+
end
|
@@ -2,23 +2,23 @@ module ActiveResource
|
|
2
2
|
module Formats
|
3
3
|
module XmlFormat
|
4
4
|
extend self
|
5
|
-
|
5
|
+
|
6
6
|
def extension
|
7
7
|
"xml"
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
def mime_type
|
11
11
|
"application/xml"
|
12
12
|
end
|
13
|
-
|
14
|
-
def encode(hash
|
15
|
-
hash.to_xml
|
13
|
+
|
14
|
+
def encode(hash)
|
15
|
+
hash.to_xml
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def decode(xml)
|
19
19
|
from_xml_data(Hash.from_xml(xml))
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
private
|
23
23
|
# Manipulate from_xml Hash, because xml_simple is not exactly what we
|
24
24
|
# want for Active Resource.
|
@@ -28,7 +28,7 @@ module ActiveResource
|
|
28
28
|
else
|
29
29
|
data
|
30
30
|
end
|
31
|
-
end
|
31
|
+
end
|
32
32
|
end
|
33
33
|
end
|
34
|
-
end
|
34
|
+
end
|
@@ -146,7 +146,7 @@ module ActiveResource
|
|
146
146
|
attr_accessor :path, :method, :body, :headers
|
147
147
|
|
148
148
|
def initialize(method, path, body = nil, headers = {})
|
149
|
-
@method, @path, @body, @headers = method, path, body, headers.
|
149
|
+
@method, @path, @body, @headers = method, path, body, headers.reverse_merge('Content-Type' => 'application/xml')
|
150
150
|
end
|
151
151
|
|
152
152
|
def ==(other_request)
|
@@ -10,7 +10,8 @@ class CustomMethodsTest < Test::Unit::TestCase
|
|
10
10
|
@ryan = { :name => 'Ryan' }.to_xml(:root => 'person')
|
11
11
|
@addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address')
|
12
12
|
@addy_deep = { :id => 1, :street => '12345 Street', :zip => "27519" }.to_xml(:root => 'address')
|
13
|
-
|
13
|
+
@default_request_headers = { 'Content-Type' => 'application/xml' }
|
14
|
+
|
14
15
|
ActiveResource::HttpMock.respond_to do |mock|
|
15
16
|
mock.get "/people/1.xml", {}, @matz
|
16
17
|
mock.get "/people/1/shallow.xml", {}, @matz
|
data/test/base_test.rb
CHANGED
@@ -819,7 +819,7 @@ class BaseTest < Test::Unit::TestCase
|
|
819
819
|
|
820
820
|
def test_to_xml
|
821
821
|
matz = Person.find(1)
|
822
|
-
xml = matz.
|
822
|
+
xml = matz.to_xml
|
823
823
|
assert xml.starts_with?('<?xml version="1.0" encoding="UTF-8"?>')
|
824
824
|
assert xml.include?('<name>Matz</name>')
|
825
825
|
assert xml.include?('<id type="integer">1</id>')
|
data/test/format_test.rb
CHANGED
@@ -5,22 +5,14 @@ class FormatTest < Test::Unit::TestCase
|
|
5
5
|
def setup
|
6
6
|
@matz = { :id => 1, :name => 'Matz' }
|
7
7
|
@david = { :id => 2, :name => 'David' }
|
8
|
-
|
8
|
+
|
9
9
|
@programmers = [ @matz, @david ]
|
10
10
|
end
|
11
|
-
|
12
|
-
def test_http_format_header_name
|
13
|
-
header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get]
|
14
|
-
assert_equal 'Accept', header_name
|
15
|
-
|
16
|
-
headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]]
|
17
|
-
headers_names.each{|header_name| assert_equal 'Content-Type', header_name}
|
18
|
-
end
|
19
|
-
|
11
|
+
|
20
12
|
def test_formats_on_single_element
|
21
13
|
for format in [ :json, :xml ]
|
22
14
|
using_format(Person, format) do
|
23
|
-
ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {
|
15
|
+
ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {}, ActiveResource::Formats[format].encode(@david)
|
24
16
|
assert_equal @david[:name], Person.find(1).name
|
25
17
|
end
|
26
18
|
end
|
@@ -29,7 +21,7 @@ class FormatTest < Test::Unit::TestCase
|
|
29
21
|
def test_formats_on_collection
|
30
22
|
for format in [ :json, :xml ]
|
31
23
|
using_format(Person, format) do
|
32
|
-
ActiveResource::HttpMock.respond_to.get "/people.#{format}", {
|
24
|
+
ActiveResource::HttpMock.respond_to.get "/people.#{format}", {}, ActiveResource::Formats[format].encode(@programmers)
|
33
25
|
remote_programmers = Person.find(:all)
|
34
26
|
assert_equal 2, remote_programmers.size
|
35
27
|
assert remote_programmers.select { |p| p.name == 'David' }
|
@@ -40,7 +32,7 @@ class FormatTest < Test::Unit::TestCase
|
|
40
32
|
def test_formats_on_custom_collection_method
|
41
33
|
for format in [ :json, :xml ]
|
42
34
|
using_format(Person, format) do
|
43
|
-
ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {
|
35
|
+
ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {}, ActiveResource::Formats[format].encode([@david])
|
44
36
|
remote_programmers = Person.get(:retrieve, :name => 'David')
|
45
37
|
assert_equal 1, remote_programmers.size
|
46
38
|
assert_equal @david[:id], remote_programmers[0]['id']
|
@@ -48,13 +40,13 @@ class FormatTest < Test::Unit::TestCase
|
|
48
40
|
end
|
49
41
|
end
|
50
42
|
end
|
51
|
-
|
43
|
+
|
52
44
|
def test_formats_on_custom_element_method
|
53
45
|
for format in [ :json, :xml ]
|
54
46
|
using_format(Person, format) do
|
55
47
|
ActiveResource::HttpMock.respond_to do |mock|
|
56
|
-
mock.get "/people/2.#{format}", {
|
57
|
-
mock.get "/people/2/shallow.#{format}", {
|
48
|
+
mock.get "/people/2.#{format}", {}, ActiveResource::Formats[format].encode(@david)
|
49
|
+
mock.get "/people/2/shallow.#{format}", {}, ActiveResource::Formats[format].encode(@david)
|
58
50
|
end
|
59
51
|
remote_programmer = Person.find(2).get(:shallow)
|
60
52
|
assert_equal @david[:id], remote_programmer['id']
|
@@ -65,24 +57,20 @@ class FormatTest < Test::Unit::TestCase
|
|
65
57
|
for format in [ :json, :xml ]
|
66
58
|
ryan = ActiveResource::Formats[format].encode({ :name => 'Ryan' })
|
67
59
|
using_format(Person, format) do
|
60
|
+
ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {}, ryan, 201, 'Location' => "/people/5.#{format}"
|
68
61
|
remote_ryan = Person.new(:name => 'Ryan')
|
69
|
-
ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"}
|
70
|
-
remote_ryan.save
|
71
|
-
|
72
|
-
remote_ryan = Person.new(:name => 'Ryan')
|
73
|
-
ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"}
|
74
62
|
assert_equal ActiveResource::Response.new(ryan, 201, {'Location' => "/people/5.#{format}"}), remote_ryan.post(:register)
|
75
63
|
end
|
76
64
|
end
|
77
65
|
end
|
78
|
-
|
66
|
+
|
79
67
|
def test_setting_format_before_site
|
80
68
|
resource = Class.new(ActiveResource::Base)
|
81
69
|
resource.format = :json
|
82
70
|
resource.site = 'http://37s.sunrise.i:3000'
|
83
71
|
assert_equal ActiveResource::Formats[:json], resource.connection.format
|
84
72
|
end
|
85
|
-
|
73
|
+
|
86
74
|
private
|
87
75
|
def using_format(klass, mime_type_reference)
|
88
76
|
previous_format = klass.format
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activeresource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
@@ -9,7 +9,7 @@ autorequire: active_resource
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-10-23 00:00:00 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - "="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 2.1.
|
23
|
+
version: 2.1.2
|
24
24
|
version:
|
25
25
|
description: Wraps web resources in model classes that can be manipulated through XML over REST.
|
26
26
|
email: david@loudthinking.com
|