api-client 2.0.0.rc2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/api-client/base.rb +24 -34
- data/lib/api-client/class_methods.rb +64 -22
- data/lib/api-client/configuration.rb +15 -0
- data/lib/api-client/dispatcher.rb +8 -5
- data/lib/api-client/dispatcher/net-http.rb +4 -4
- data/lib/api-client/dispatcher/typhoeus.rb +5 -5
- data/lib/api-client/instance_methods.rb +44 -33
- data/lib/api-client/version.rb +1 -1
- data/spec/api-client/base_spec.rb +16 -47
- data/spec/api-client/class_methods_spec.rb +60 -0
- data/spec/api-client/configuration_spec.rb +34 -4
- data/spec/api-client/instance_methods_spec.rb +62 -0
- data/spec/spec_helper.rb +1 -1
- metadata +13 -6
data/lib/api-client/base.rb
CHANGED
@@ -15,6 +15,9 @@ module ApiClient
|
|
15
15
|
extend ApiClient::ClassMethods
|
16
16
|
include ApiClient::InstanceMethods
|
17
17
|
|
18
|
+
# @return [Integer] the id of the object.
|
19
|
+
attr_accessor :id
|
20
|
+
|
18
21
|
# @return [Hash] the request response.
|
19
22
|
attr_accessor :response
|
20
23
|
|
@@ -39,34 +42,34 @@ module ApiClient
|
|
39
42
|
false
|
40
43
|
end
|
41
44
|
|
42
|
-
# Return the path of the object on the api url.
|
45
|
+
# Return the resource path of the object on the api url.
|
43
46
|
#
|
44
|
-
# @return [String] the
|
45
|
-
def self.
|
46
|
-
return self.to_s.gsub('::', '/').downcase.pluralize unless @
|
47
|
-
@
|
47
|
+
# @return [String] the resource path on the api for this object.
|
48
|
+
def self.resource_path
|
49
|
+
return self.to_s.gsub('::', '/').downcase.pluralize unless @resource_path
|
50
|
+
@resource_path
|
48
51
|
end
|
49
52
|
|
50
|
-
# Set the path of the object on the api
|
53
|
+
# Set the resource path of the object on the api.
|
51
54
|
#
|
52
|
-
# @param [String] path string.
|
53
|
-
def self.
|
54
|
-
|
55
|
-
@
|
55
|
+
# @param [String] resource path string.
|
56
|
+
def self.resource_path=(resource_path)
|
57
|
+
resource_path = resource_path[1, resource_path.size - 1] if resource_path[0, 1] == '/'
|
58
|
+
@resource_path = resource_path
|
56
59
|
end
|
57
60
|
|
58
|
-
# Return the
|
61
|
+
# Return the Root node name for this Class.
|
59
62
|
#
|
60
|
-
# @return [String] a string with the
|
61
|
-
def self.
|
62
|
-
@
|
63
|
+
# @return [String] a string with the root node name for this class.
|
64
|
+
def self.root_node
|
65
|
+
@root_node.blank? ? self.to_s.split('::').last.downcase : @root_node
|
63
66
|
end
|
64
67
|
|
65
|
-
# Set a custom
|
68
|
+
# Set a custom root node name instead of the Class name.
|
66
69
|
#
|
67
|
-
# @param [String]
|
68
|
-
def self.
|
69
|
-
@
|
70
|
+
# @param [String] root_node root node name.
|
71
|
+
def self.root_node=(root_node)
|
72
|
+
@root_node = root_node
|
70
73
|
end
|
71
74
|
|
72
75
|
# Set methods to initialize associated objects.
|
@@ -76,12 +79,7 @@ module ApiClient
|
|
76
79
|
associations.each do |association, class_name|
|
77
80
|
class_eval <<-EVAL
|
78
81
|
def #{association.to_s}=(attributes = {})
|
79
|
-
if attributes.instance_of?(Array)
|
80
|
-
return @#{association.to_s} = attributes.map { |attr|
|
81
|
-
attr = remove_root(attr)
|
82
|
-
#{class_name.constantize}.new(attr)
|
83
|
-
}
|
84
|
-
end
|
82
|
+
return @#{association.to_s} = attributes.map { |attr| #{class_name.constantize}.new(attr) } if attributes.instance_of?(Array)
|
85
83
|
@#{association.to_s} = #{class_name.constantize}.new(attributes)
|
86
84
|
end
|
87
85
|
def #{association.to_s}
|
@@ -126,7 +124,7 @@ module ApiClient
|
|
126
124
|
# @param [String] url to get the collection.
|
127
125
|
# @return [Collection] a collection of objects.
|
128
126
|
def self.collection
|
129
|
-
ApiClient::Collection.new(self, self.
|
127
|
+
ApiClient::Collection.new(self, self.resource_path).collection
|
130
128
|
end
|
131
129
|
|
132
130
|
# Set the hash of errors, making keys symbolic.
|
@@ -135,13 +133,5 @@ module ApiClient
|
|
135
133
|
def errors=(errs = {})
|
136
134
|
errors.add_errors(Hash[errs.map{|(key,value)| [key.to_sym,value]}])
|
137
135
|
end
|
138
|
-
|
139
|
-
protected
|
140
|
-
|
141
|
-
def remove_root(attributes = {})
|
142
|
-
attributes = attributes[self.class.remote_object.to_sym] if attributes.key?(self.class.remote_object.to_sym)
|
143
|
-
attributes = attributes[self.class.remote_object.to_s] if attributes.key?(self.class.remote_object.to_s)
|
144
|
-
attributes
|
145
|
-
end
|
146
136
|
end
|
147
|
-
end
|
137
|
+
end
|
@@ -3,38 +3,80 @@ module ApiClient
|
|
3
3
|
module ClassMethods
|
4
4
|
# Initialize an object based on a hash of attributes.
|
5
5
|
#
|
6
|
-
# @param [Hash]
|
6
|
+
# @param [Hash] attributes hash of attributes.
|
7
7
|
# @return [Base] the object initialized.
|
8
|
-
def build(
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
new(params.merge(:response => params))
|
13
|
-
end
|
8
|
+
def build(attributes)
|
9
|
+
hash = remove_root(attributes)
|
10
|
+
hash = hash.merge({ 'response' => attributes })
|
11
|
+
new(hash)
|
14
12
|
end
|
15
13
|
|
16
|
-
# Make
|
14
|
+
# Make a get requisition and initialize an object with the response.
|
17
15
|
#
|
18
|
-
# @param [
|
19
|
-
# @param [
|
20
|
-
# @param [Array] args an array of params.
|
16
|
+
# @param [Integer] id id of the object.
|
17
|
+
# @param [Hash] header hash with the header options.
|
21
18
|
# @return [Base] the object initialized.
|
22
|
-
def
|
23
|
-
url = "#{ApiClient.config.path}#{self.
|
24
|
-
|
25
|
-
response = ApiClient::Dispatcher.send(method_name.to_sym, url, *args)
|
19
|
+
def get(id, header = {})
|
20
|
+
url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
|
21
|
+
response = ApiClient::Dispatcher.get(url, header)
|
26
22
|
params = ApiClient::Parser.response(response, url)
|
27
23
|
build(params)
|
28
24
|
end
|
29
25
|
|
30
|
-
#
|
26
|
+
# Make a post requisition and initialize an object with the response.
|
31
27
|
#
|
32
|
-
# @param [
|
33
|
-
# @param [
|
34
|
-
# @return [
|
35
|
-
def
|
36
|
-
|
37
|
-
|
28
|
+
# @param [Hash] attributes hash with the attributes to send.
|
29
|
+
# @param [Hash] header hash with the header options.
|
30
|
+
# @return [Base] the object initialized.
|
31
|
+
def post(attributes, header = {})
|
32
|
+
url = "#{ApiClient.config.path}#{self.resource_path}/"
|
33
|
+
response = ApiClient::Dispatcher.post(url, attributes, header)
|
34
|
+
params = ApiClient::Parser.response(response, url)
|
35
|
+
build(params)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Make a put requisition and initialize an object with the response.
|
39
|
+
#
|
40
|
+
# @param [Integer] id id of the object.
|
41
|
+
# @param [Hash] attributes hash with the attributes to send.
|
42
|
+
# @param [Hash] header hash with the header options.
|
43
|
+
# @return [Base] the object initialized.
|
44
|
+
def put(id, attributes, header = {})
|
45
|
+
url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
|
46
|
+
response = ApiClient::Dispatcher.put(url, attributes, header)
|
47
|
+
params = ApiClient::Parser.response(response, url)
|
48
|
+
build(params)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Make a patch requisition and initialize an object with the response.
|
52
|
+
#
|
53
|
+
# @param [Integer] id id of the object.
|
54
|
+
# @param [Hash] attributes hash with the attributes to send.
|
55
|
+
# @param [Hash] header hash with the header options.
|
56
|
+
# @return [Base] the object initialized.
|
57
|
+
def patch(id, attributes, header = {})
|
58
|
+
url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
|
59
|
+
response = ApiClient::Dispatcher.patch(url, attributes, header)
|
60
|
+
params = ApiClient::Parser.response(response, url)
|
61
|
+
build(params)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Make a delete requisition and initialize an object with the response.
|
65
|
+
#
|
66
|
+
# @param [Integer] id id of the object.
|
67
|
+
# @param [Hash] header hash with the header options.
|
68
|
+
# @return [Base] the object initialized.
|
69
|
+
def delete(id, header = {})
|
70
|
+
url = "#{ApiClient.config.path}#{self.resource_path}/#{id}"
|
71
|
+
response = ApiClient::Dispatcher.delete(url, header)
|
72
|
+
params = ApiClient::Parser.response(response, url)
|
73
|
+
build(params)
|
74
|
+
end
|
75
|
+
|
76
|
+
def remove_root(attributes = {})
|
77
|
+
attributes = attributes[self.root_node.to_sym] if attributes.key?(self.root_node.to_sym)
|
78
|
+
attributes = attributes[self.root_node.to_s] if attributes.key?(self.root_node.to_s)
|
79
|
+
attributes
|
38
80
|
end
|
39
81
|
end
|
40
82
|
end
|
@@ -16,5 +16,20 @@ module ApiClient
|
|
16
16
|
path = "#{path}/" unless path[path.size - 1, 1] == '/'
|
17
17
|
@path = path
|
18
18
|
end
|
19
|
+
|
20
|
+
# Return the default header for requisitions.
|
21
|
+
#
|
22
|
+
# @return [Hash] the default header.
|
23
|
+
def header
|
24
|
+
return { 'Content-Type' => 'application/json' } unless @header
|
25
|
+
@header
|
26
|
+
end
|
27
|
+
|
28
|
+
# Set the default header for requisitions.
|
29
|
+
#
|
30
|
+
# @param [Hash] header the default header for requitions.
|
31
|
+
def header=(header = {})
|
32
|
+
@header = { 'Content-Type' => 'application/json' }.merge(header)
|
33
|
+
end
|
19
34
|
end
|
20
35
|
end
|
@@ -3,12 +3,13 @@ module ApiClient::Dispatcher
|
|
3
3
|
autoload :Typhoeus, 'api-client/dispatcher/typhoeus'
|
4
4
|
autoload :NetHttp, 'api-client/dispatcher/net-http'
|
5
5
|
|
6
|
-
def self.method_missing(
|
6
|
+
def self.method_missing(method_name, *args)
|
7
7
|
if defined?(::Typhoeus)
|
8
|
-
Typhoeus.send(
|
8
|
+
return Typhoeus.send(method_name, *args) if Typhoeus.respond_to?(method_name)
|
9
9
|
else
|
10
|
-
NetHttp.send(
|
10
|
+
return NetHttp.send(method_name, *args) if NetHttp.respond_to?(method_name)
|
11
11
|
end
|
12
|
+
super
|
12
13
|
end
|
13
14
|
|
14
15
|
# Overwrite respond_to? default behavior
|
@@ -16,8 +17,10 @@ module ApiClient::Dispatcher
|
|
16
17
|
# @param [Symbol] method_name the name of the method.
|
17
18
|
# @param [Boolean] include_private if it does work to private methods as well.
|
18
19
|
# @return [Boolean] if it responds to the method or not.
|
19
|
-
def respond_to_missing?(method_name, include_private = false)
|
20
|
-
|
20
|
+
def self.respond_to_missing?(method_name, include_private = false)
|
21
|
+
if defined?(::Typhoeus)
|
22
|
+
return true if Typhoeus.respond_to?(method_name)
|
23
|
+
end
|
21
24
|
return true if NetHttp.respond_to?(method_name)
|
22
25
|
super
|
23
26
|
end
|
@@ -10,7 +10,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
10
10
|
# @return [HTTP] the response object.
|
11
11
|
def self.get(url, header = {})
|
12
12
|
initialize_connection(url)
|
13
|
-
call { @http.get(@uri.request_uri,
|
13
|
+
call { @http.get(@uri.request_uri, ApiClient.config.header.merge(header)) }
|
14
14
|
end
|
15
15
|
|
16
16
|
# Make a post request and returns it.
|
@@ -21,7 +21,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
21
21
|
# @return [HTTP] the response object.
|
22
22
|
def self.post(url, args, header = {})
|
23
23
|
initialize_connection(url)
|
24
|
-
call { @http.post(@uri.request_uri, args.to_json,
|
24
|
+
call { @http.post(@uri.request_uri, args.to_json, ApiClient.config.header.merge(header)) }
|
25
25
|
end
|
26
26
|
|
27
27
|
# Make a put request and returns it.
|
@@ -32,7 +32,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
32
32
|
# @return [HTTP] the response object.
|
33
33
|
def self.put(url, args, header = {})
|
34
34
|
initialize_connection(url)
|
35
|
-
call { @http.put(@uri.request_uri, args.to_json,
|
35
|
+
call { @http.put(@uri.request_uri, args.to_json, ApiClient.config.header.merge(header)) }
|
36
36
|
end
|
37
37
|
|
38
38
|
# Make a patch request and returns it.
|
@@ -43,7 +43,7 @@ module ApiClient::Dispatcher::NetHttp
|
|
43
43
|
# @return [HTTP] the response object.
|
44
44
|
def self.patch(url, args, header = {})
|
45
45
|
initialize_connection(url)
|
46
|
-
call { @http.patch(@uri.request_uri, args.to_json,
|
46
|
+
call { @http.patch(@uri.request_uri, args.to_json, ApiClient.config.header.merge(header)) }
|
47
47
|
end
|
48
48
|
|
49
49
|
# Make a delete request and returns it.
|
@@ -8,7 +8,7 @@ module ApiClient::Dispatcher::Typhoeus
|
|
8
8
|
# @param [Hash] header attributes of the request.
|
9
9
|
# @return [Typhoeus::Request] the response object.
|
10
10
|
def self.get(url, header = {})
|
11
|
-
::Typhoeus::Request.get(url, :headers => header)
|
11
|
+
::Typhoeus::Request.get(url, :headers => ApiClient.config.header.merge(header))
|
12
12
|
end
|
13
13
|
|
14
14
|
# Make a post request and returns it.
|
@@ -18,7 +18,7 @@ module ApiClient::Dispatcher::Typhoeus
|
|
18
18
|
# @param [Hash] header attributes of the request.
|
19
19
|
# @return [Typhoeus::Request] the response object.
|
20
20
|
def self.post(url, args, header = {})
|
21
|
-
::Typhoeus::Request.post(url, :
|
21
|
+
::Typhoeus::Request.post(url, :body => args, :headers => ApiClient.config.header.merge(header))
|
22
22
|
end
|
23
23
|
|
24
24
|
# Make a put request and returns it.
|
@@ -28,7 +28,7 @@ module ApiClient::Dispatcher::Typhoeus
|
|
28
28
|
# @param [Hash] header attributes of the request.
|
29
29
|
# @return [Typhoeus::Request] the response object.
|
30
30
|
def self.put(url, args, header = {})
|
31
|
-
::Typhoeus::Request.put(url, :
|
31
|
+
::Typhoeus::Request.put(url, :body => args, :headers => ApiClient.config.header.merge(header))
|
32
32
|
end
|
33
33
|
|
34
34
|
# Make a patch request and returns it.
|
@@ -38,7 +38,7 @@ module ApiClient::Dispatcher::Typhoeus
|
|
38
38
|
# @param [Hash] header attributes of the request.
|
39
39
|
# @return [Typhoeus::Request] the response object.
|
40
40
|
def self.patch(url, args, header = {})
|
41
|
-
::Typhoeus::Request.patch(url, :
|
41
|
+
::Typhoeus::Request.patch(url, :body => args, :headers => ApiClient.config.header.merge(header))
|
42
42
|
end
|
43
43
|
|
44
44
|
# Make a delete request and returns it.
|
@@ -47,6 +47,6 @@ module ApiClient::Dispatcher::Typhoeus
|
|
47
47
|
# @param [Hash] header attributes of the request.
|
48
48
|
# @return [Typhoeus::Request] the response object.
|
49
49
|
def self.delete(url, header = {})
|
50
|
-
::Typhoeus::Request.delete(url, :headers => header)
|
50
|
+
::Typhoeus::Request.delete(url, :headers => ApiClient.config.header.merge(header))
|
51
51
|
end
|
52
52
|
end
|
@@ -1,47 +1,58 @@
|
|
1
1
|
module ApiClient
|
2
|
-
# This module handles the logic to make an api call and
|
2
|
+
# This module handles the logic to make an api call and update_attributes the current object with the response.
|
3
3
|
module InstanceMethods
|
4
4
|
# Update an object based on a hash of attributes.
|
5
5
|
#
|
6
6
|
# @param [Hash] params hash of attributes.
|
7
|
-
# @return [Base] the
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
instance_variable_set(response.to_sym, params[self.class.remote_object])
|
14
|
-
else
|
15
|
-
params.each do |key, value|
|
16
|
-
instance_variable_set("@#{key}", value)
|
17
|
-
end
|
18
|
-
instance_variable_set("@response", params)
|
7
|
+
# @return [Base] the update_attributes object.
|
8
|
+
def update_attributes(attributes)
|
9
|
+
hash = remove_root(attributes)
|
10
|
+
hash = hash.merge({ 'response' => attributes })
|
11
|
+
hash.each do |key, value|
|
12
|
+
send("#{key}=", value)
|
19
13
|
end
|
20
14
|
self
|
21
15
|
end
|
22
16
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
# @return [Base] the object updated.
|
29
|
-
def method_missing(method_name, id = nil, *args)
|
30
|
-
url = "#{ApiClient.config.path}#{self.class.path}"
|
31
|
-
"#{url}/#{id}" unless id.nil?
|
32
|
-
response = ApiClient::Dispatcher.send(method_name.to_sym, url, *args)
|
33
|
-
params = ApiClient::Parser.response(response, url)
|
34
|
-
update(params)
|
17
|
+
def get(header = {})
|
18
|
+
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
|
19
|
+
response = ApiClient::Dispatcher.get(url, header)
|
20
|
+
attributes = ApiClient::Parser.response(response, url)
|
21
|
+
update_attributes(attributes)
|
35
22
|
end
|
36
23
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
24
|
+
def post(header = {})
|
25
|
+
url = "#{ApiClient.config.path}#{self.class.resource_path}/"
|
26
|
+
response = ApiClient::Dispatcher.post(url, self.attributes, header)
|
27
|
+
attributes = ApiClient::Parser.response(response, url)
|
28
|
+
update_attributes(attributes)
|
29
|
+
end
|
30
|
+
|
31
|
+
def put(header = {})
|
32
|
+
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
|
33
|
+
response = ApiClient::Dispatcher.put(url, self.attributes, header)
|
34
|
+
attributes = ApiClient::Parser.response(response, url)
|
35
|
+
update_attributes(attributes)
|
36
|
+
end
|
37
|
+
|
38
|
+
def patch(header = {})
|
39
|
+
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
|
40
|
+
response = ApiClient::Dispatcher.post(url, self.attributes, header)
|
41
|
+
attributes = ApiClient::Parser.response(response, url)
|
42
|
+
update_attributes(attributes)
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete(header = {})
|
46
|
+
url = "#{ApiClient.config.path}#{self.class.resource_path}/#{id}"
|
47
|
+
response = ApiClient::Dispatcher.post(url, header)
|
48
|
+
attributes = ApiClient::Parser.response(response, url)
|
49
|
+
update_attributes(attributes)
|
50
|
+
end
|
51
|
+
|
52
|
+
def remove_root(attributes = {})
|
53
|
+
attributes = attributes[self.class.root_node.to_sym] if attributes.key?(self.class.root_node.to_sym)
|
54
|
+
attributes = attributes[self.class.root_node.to_s] if attributes.key?(self.class.root_node.to_s)
|
55
|
+
attributes
|
45
56
|
end
|
46
57
|
end
|
47
58
|
end
|
data/lib/api-client/version.rb
CHANGED
@@ -44,63 +44,63 @@ describe ApiClient::Base do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
describe '.
|
47
|
+
describe '.resource_path' do
|
48
48
|
describe 'when not configured' do
|
49
49
|
it 'should return a name based on the class name' do
|
50
|
-
User.
|
50
|
+
User.resource_path.should == 'users'
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe 'when properly configured' do
|
55
55
|
before :each do
|
56
|
-
User.
|
56
|
+
User.resource_path = 'admins'
|
57
57
|
end
|
58
58
|
|
59
|
-
it 'should return the
|
60
|
-
User.
|
59
|
+
it 'should return the resource_path value' do
|
60
|
+
User.resource_path.should == 'admins'
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
describe '.
|
65
|
+
describe '.resource_path=' do
|
66
66
|
describe "with a string without '/'" do
|
67
67
|
before :each do
|
68
|
-
User.
|
68
|
+
User.resource_path = 'users'
|
69
69
|
end
|
70
70
|
|
71
71
|
it 'should set it as passed' do
|
72
|
-
User.
|
72
|
+
User.resource_path.should == 'users'
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
76
|
describe "with a string with '/'" do
|
77
77
|
before :each do
|
78
|
-
User.
|
78
|
+
User.resource_path = '/users'
|
79
79
|
end
|
80
80
|
|
81
81
|
it "should set it without the '/'" do
|
82
|
-
User.
|
82
|
+
User.resource_path.should == 'users'
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
|
-
describe '.
|
87
|
+
describe '.root_node' do
|
88
88
|
context 'on a class without remote object specification' do
|
89
89
|
it 'should return the class name' do
|
90
|
-
User.
|
90
|
+
User.root_node.should == 'user'
|
91
91
|
end
|
92
92
|
end
|
93
93
|
|
94
94
|
context 'on a class with remote object specification' do
|
95
95
|
it 'should return the class name' do
|
96
|
-
Admin.
|
96
|
+
Admin.root_node.should == 'user'
|
97
97
|
end
|
98
98
|
end
|
99
99
|
end
|
100
100
|
|
101
|
-
describe '.
|
101
|
+
describe '.root_node=' do
|
102
102
|
it 'should set the remote object name' do
|
103
|
-
Admin.
|
103
|
+
Admin.root_node.should == 'user'
|
104
104
|
end
|
105
105
|
end
|
106
106
|
|
@@ -160,35 +160,4 @@ describe ApiClient::Base do
|
|
160
160
|
@user.errors.messages.should == { :a => %w(message), :b => %w(message) }
|
161
161
|
end
|
162
162
|
end
|
163
|
-
|
164
|
-
describe 'requests' do
|
165
|
-
context 'on the class' do
|
166
|
-
before :each do
|
167
|
-
stub_request(:any, 'http://api.example.com/users').to_return(:body => {'a' => 'b'}.to_json)
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'should return a new instance' do
|
171
|
-
User.get.should be_an_instance_of(User)
|
172
|
-
end
|
173
|
-
|
174
|
-
it 'should set the response on the instance' do
|
175
|
-
User.get.response.should == { 'a' => 'b' }
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
context 'on an object' do
|
180
|
-
before :each do
|
181
|
-
stub_request(:any, 'http://api.example.com/users').to_return(:body => {'a' => 'b'}.to_json)
|
182
|
-
@user = User.new
|
183
|
-
end
|
184
|
-
|
185
|
-
it 'should return a new instance' do
|
186
|
-
@user.get.should be_an_instance_of(User)
|
187
|
-
end
|
188
|
-
|
189
|
-
it 'should set the response on the instance' do
|
190
|
-
@user.get.response.should == {'a' => 'b'}
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
163
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApiClient::ClassMethods do
|
4
|
+
before :each do
|
5
|
+
stub_request(:any, 'http://api.example.com/users/').to_return(:body => {'a' => 'b'}.to_json)
|
6
|
+
stub_request(:any, 'http://api.example.com/users/1').to_return(:body => {'a' => 'b'}.to_json)
|
7
|
+
end
|
8
|
+
|
9
|
+
context '.build' do
|
10
|
+
context 'with a root node' do
|
11
|
+
it 'should return a new object' do
|
12
|
+
User.build('user' => { 'a' => 'b'}).should be_an_instance_of(User)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should set the response' do
|
16
|
+
User.build('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'without a root node' do
|
21
|
+
it 'should return a new object' do
|
22
|
+
User.build('a' => 'b').should be_an_instance_of(User)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should set the response' do
|
26
|
+
User.build('a' => 'b').response.should == { 'a' => 'b' }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context '.get' do
|
32
|
+
it 'should return a new object' do
|
33
|
+
User.get(1).should be_an_instance_of(User)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context '.post' do
|
38
|
+
it 'should return a new object' do
|
39
|
+
User.post({}).should be_an_instance_of(User)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context '.put' do
|
44
|
+
it 'should return a new object' do
|
45
|
+
User.put(1, {}).should be_an_instance_of(User)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context '.patch' do
|
50
|
+
it 'should return a new object' do
|
51
|
+
User.patch(1, {}).should be_an_instance_of(User)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context '.delete' do
|
56
|
+
it 'should return a new object' do
|
57
|
+
User.delete(1).should be_an_instance_of(User)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ApiClient::Configuration do
|
4
4
|
describe '#path' do
|
5
|
-
|
5
|
+
context 'when not configured' do
|
6
6
|
before :each do
|
7
7
|
ApiClient.configure do |config|
|
8
8
|
config.path = ''
|
@@ -14,7 +14,7 @@ describe ApiClient::Configuration do
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
context 'when properly configured' do
|
18
18
|
before :each do
|
19
19
|
ApiClient.configure do |config|
|
20
20
|
config.path = 'http://api.example.com'
|
@@ -28,7 +28,7 @@ describe ApiClient::Configuration do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
describe '#path=' do
|
31
|
-
|
31
|
+
context "with a string without '/'" do
|
32
32
|
before :each do
|
33
33
|
ApiClient.config.path = 'http://api.example.com'
|
34
34
|
end
|
@@ -38,7 +38,7 @@ describe ApiClient::Configuration do
|
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
|
-
|
41
|
+
context "with a string with '/'" do
|
42
42
|
before :each do
|
43
43
|
ApiClient.config.path = 'http://api.example.com/'
|
44
44
|
end
|
@@ -48,4 +48,34 @@ describe ApiClient::Configuration do
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
51
|
+
|
52
|
+
describe '#header' do
|
53
|
+
context 'when not configured' do
|
54
|
+
it 'should return a hash with configs for content_type only' do
|
55
|
+
ApiClient.config.header.should == { 'Content-Type' => 'application/json' }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'when configured' do
|
60
|
+
before :each do
|
61
|
+
ApiClient.config.instance_variable_set('@header', { 'key' => 'value' })
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should return a hash with the configured header' do
|
65
|
+
ApiClient.config.header.should == { 'key' => 'value' }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '#header=' do
|
71
|
+
before :each do
|
72
|
+
ApiClient.configure do |config|
|
73
|
+
config.header = { 'Content-Type' => 'application/xml' }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should merge content_type json with the given hash' do
|
78
|
+
ApiClient.config.header.should == { 'Content-Type' => 'application/xml' }
|
79
|
+
end
|
80
|
+
end
|
51
81
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ApiClient::InstanceMethods do
|
4
|
+
let(:user) { User.new(:id => 1) }
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
stub_request(:any, 'http://api.example.com/users/').to_return(:body => {'a' => 'b'}.to_json)
|
8
|
+
stub_request(:any, 'http://api.example.com/users/1').to_return(:body => {'a' => 'b'}.to_json)
|
9
|
+
end
|
10
|
+
|
11
|
+
context '.update_attributes' do
|
12
|
+
context 'with a root node' do
|
13
|
+
it 'should update the object' do
|
14
|
+
user.update_attributes('user' => { 'a' => 'b'}).should be_an_instance_of(User)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should set the response' do
|
18
|
+
user.update_attributes('user' => { 'a' => 'b'}).response.should == { 'user' => { 'a' => 'b' } }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'without a root node' do
|
23
|
+
it 'should update the object' do
|
24
|
+
user.update_attributes('a' => 'b').should be_an_instance_of(User)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should set the response' do
|
28
|
+
user.update_attributes('a' => 'b').response.should == { 'a' => 'b' }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context '.get' do
|
34
|
+
it 'should return a new object' do
|
35
|
+
user.get.should be_an_instance_of(User)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context '.post' do
|
40
|
+
it 'should return a new object' do
|
41
|
+
user.post.should be_an_instance_of(User)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context '.put' do
|
46
|
+
it 'should return a new object' do
|
47
|
+
user.put.should be_an_instance_of(User)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context '.patch' do
|
52
|
+
it 'should return a new object' do
|
53
|
+
user.patch.should be_an_instance_of(User)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context '.delete' do
|
58
|
+
it 'should return a new object' do
|
59
|
+
user.delete.should be_an_instance_of(User)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0
|
5
|
-
prerelease:
|
4
|
+
version: 2.0.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Paulo Henrique Lopes Ribeiro
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -172,10 +172,12 @@ files:
|
|
172
172
|
- lib/api-client/parser.rb
|
173
173
|
- lib/api-client/version.rb
|
174
174
|
- spec/api-client/base_spec.rb
|
175
|
+
- spec/api-client/class_methods_spec.rb
|
175
176
|
- spec/api-client/collection_spec.rb
|
176
177
|
- spec/api-client/configuration_spec.rb
|
177
178
|
- spec/api-client/dispatcher_spec.rb
|
178
179
|
- spec/api-client/errors_spec.rb
|
180
|
+
- spec/api-client/instance_methods_spec.rb
|
179
181
|
- spec/api-client/parser_spec.rb
|
180
182
|
- spec/spec_helper.rb
|
181
183
|
homepage:
|
@@ -192,13 +194,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
192
194
|
version: '0'
|
193
195
|
segments:
|
194
196
|
- 0
|
195
|
-
hash:
|
197
|
+
hash: 4249835467579718948
|
196
198
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
197
199
|
none: false
|
198
200
|
requirements:
|
199
|
-
- - ! '
|
201
|
+
- - ! '>='
|
200
202
|
- !ruby/object:Gem::Version
|
201
|
-
version:
|
203
|
+
version: '0'
|
204
|
+
segments:
|
205
|
+
- 0
|
206
|
+
hash: 4249835467579718948
|
202
207
|
requirements: []
|
203
208
|
rubyforge_project:
|
204
209
|
rubygems_version: 1.8.25
|
@@ -215,10 +220,12 @@ test_files:
|
|
215
220
|
- gemfiles/Gemfile.net_http
|
216
221
|
- gemfiles/Gemfile.typhoeus
|
217
222
|
- spec/api-client/base_spec.rb
|
223
|
+
- spec/api-client/class_methods_spec.rb
|
218
224
|
- spec/api-client/collection_spec.rb
|
219
225
|
- spec/api-client/configuration_spec.rb
|
220
226
|
- spec/api-client/dispatcher_spec.rb
|
221
227
|
- spec/api-client/errors_spec.rb
|
228
|
+
- spec/api-client/instance_methods_spec.rb
|
222
229
|
- spec/api-client/parser_spec.rb
|
223
230
|
- spec/spec_helper.rb
|
224
231
|
has_rdoc:
|