rest_api 0.1.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/.gitignore +18 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +13 -0
- data/LICENSE +22 -0
- data/README.md +367 -0
- data/Rakefile +2 -0
- data/lib/rest_api/api/request_parser.rb +96 -0
- data/lib/rest_api/api.rb +19 -0
- data/lib/rest_api/custom_api_method_call.rb +77 -0
- data/lib/rest_api/exceptions.rb +17 -0
- data/lib/rest_api/request_handler/client.rb +37 -0
- data/lib/rest_api/request_handler.rb +77 -0
- data/lib/rest_api/version.rb +3 -0
- data/lib/rest_api.rb +46 -0
- data/rest_api.gemspec +23 -0
- data/spec/rest_api/api/request_parser_spec.rb +199 -0
- data/spec/rest_api/api_spec.rb +13 -0
- data/spec/rest_api/custom_api_method_spec.rb +90 -0
- data/spec/rest_api/request_handler/client_spec.rb +93 -0
- data/spec/rest_api/request_handler_spec.rb +204 -0
- data/spec/rest_api/rest_api_spec.rb +32 -0
- data/spec/spec_helper.rb +7 -0
- metadata +123 -0
@@ -0,0 +1,77 @@
|
|
1
|
+
require "rest_api/request_handler/client"
|
2
|
+
|
3
|
+
module RestApi
|
4
|
+
module RequestHandler
|
5
|
+
class << self
|
6
|
+
private
|
7
|
+
def client
|
8
|
+
RestApi::RequestHandler::Client
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.make_request request_type, request_url, request_params = nil
|
13
|
+
request_params ||= {}
|
14
|
+
begin
|
15
|
+
case request_type
|
16
|
+
when :put
|
17
|
+
client.put request_url, request_params
|
18
|
+
when :post
|
19
|
+
client.post request_url, request_params
|
20
|
+
when :delete
|
21
|
+
client.delete request_url, request_params
|
22
|
+
when :get
|
23
|
+
client.get request_url, request_params
|
24
|
+
else
|
25
|
+
raise Exception.new("Invalid request method")
|
26
|
+
end
|
27
|
+
rescue RestApi::Exceptions::ParseResponseException => e
|
28
|
+
raise e
|
29
|
+
rescue Exception => e
|
30
|
+
raise RestApi::Exceptions::ApiConnectionException.new(e)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# API CALL
|
35
|
+
# make the request call if the method match with get,put,post or delete
|
36
|
+
# the oder of the resources will be reversed
|
37
|
+
# Example: method_missing :get_casas_from_usuarios will make a GET to <APIURL>/usuarios/casas
|
38
|
+
# ----> request_params must can be a hash with the following keys
|
39
|
+
# 1) :resources_params - contains params for the corresponding resource.
|
40
|
+
# The key of the param must has the name of the correspounding resource
|
41
|
+
# 2) :request_params - params of the header)
|
42
|
+
# ----> request_params must can be an Array of resources params
|
43
|
+
# Example: method_missing :get_casas_from_usuarios, 4 will make a GET to <APIURL>/usuarios/4/casas
|
44
|
+
# If the last item is a hash then it will be considered the :request_params
|
45
|
+
|
46
|
+
def self.method_missing(method_id, *arguments)
|
47
|
+
if request_type = API::RequestParser.get_request_type_from_method(method_id)
|
48
|
+
params = get_params_from_array_arguments arguments
|
49
|
+
request_url = API::RequestParser.get_url_from_method(method_id, params[:resources_params])
|
50
|
+
make_request request_type, request_url, params[:request_params]
|
51
|
+
else
|
52
|
+
super
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.get_params_from_array_arguments arguments
|
57
|
+
params_hash = { }
|
58
|
+
if arguments[0].is_a? Hash
|
59
|
+
if (arguments[0].has_key?(:resources_params) || arguments[0].has_key?(:request_params))
|
60
|
+
params_hash[:resources_params] = arguments[0][:resources_params]
|
61
|
+
params_hash[:request_params] = arguments[0][:request_params]
|
62
|
+
else
|
63
|
+
#the only argument is the request params
|
64
|
+
params_hash[:request_params] = arguments[0]
|
65
|
+
end
|
66
|
+
else
|
67
|
+
if arguments.last
|
68
|
+
params_hash[:request_params] = arguments.pop if arguments.last.is_a? Hash
|
69
|
+
end
|
70
|
+
params_hash[:resources_params] = Array.new arguments
|
71
|
+
end
|
72
|
+
params_hash.delete :request_params if params_hash[:request_params].nil?
|
73
|
+
params_hash
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
data/lib/rest_api.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "rest_api/exceptions"
|
2
|
+
require "rest_api/version"
|
3
|
+
require "rest_api/api"
|
4
|
+
require "rest_api/request_handler"
|
5
|
+
require "rest_api/custom_api_method_call"
|
6
|
+
|
7
|
+
module RestApi
|
8
|
+
|
9
|
+
@@custom_methods = []
|
10
|
+
|
11
|
+
# SETUP
|
12
|
+
def self.setup
|
13
|
+
yield self
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.api_url
|
17
|
+
self.api.url
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.api_url=(value)
|
21
|
+
if value.match(/.+\/$/)
|
22
|
+
self.api.url = value
|
23
|
+
else
|
24
|
+
self.api.url = value + "/"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# api reference
|
29
|
+
def self.api
|
30
|
+
RestApi::API
|
31
|
+
end
|
32
|
+
|
33
|
+
# request reference
|
34
|
+
def self.request
|
35
|
+
RestApi::RequestHandler
|
36
|
+
end
|
37
|
+
|
38
|
+
# request parser reference
|
39
|
+
def self.request_parser
|
40
|
+
RestApi::API::RequestParser
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
end
|
data/rest_api.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rest_api/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Codus Tecnologia"]
|
6
|
+
gem.email = ["vinicius.oyama@codus.com.br"]
|
7
|
+
gem.platform = Gem::Platform::RUBY
|
8
|
+
gem.description = %q{An API client for any application!}
|
9
|
+
gem.summary = %q{An API client for any application!}
|
10
|
+
gem.homepage = "https://github.com/codus/rest_api"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "rest_api"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = RestApi::VERSION
|
18
|
+
|
19
|
+
|
20
|
+
gem.add_dependency('activesupport', '~> 3.2')
|
21
|
+
gem.add_dependency('json', '~> 1.7')
|
22
|
+
gem.add_dependency('rest-client', '~> 1.6')
|
23
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'set'
|
6
|
+
|
7
|
+
describe "RestApi::API - url_parser" do
|
8
|
+
let (:fake_api_url) { "http://www.fakeurl.com/" }
|
9
|
+
before(:each) {
|
10
|
+
RestApi.setup do |config|
|
11
|
+
config.api_url = "http://www.fakeurl.com/"
|
12
|
+
end
|
13
|
+
}
|
14
|
+
|
15
|
+
describe "ensured_resources_names" do
|
16
|
+
it "should return a new set if empty" do
|
17
|
+
RestApi.api::RequestParser.send(:ensured_resources_names).should be_empty
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should preserve the content" do
|
21
|
+
RestApi.api::RequestParser.send(:ensured_resources_names) << :one
|
22
|
+
RestApi.api::RequestParser.send(:ensured_resources_names) << :two
|
23
|
+
|
24
|
+
RestApi.api::RequestParser.send(:ensured_resources_names).length.should be == 2
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "ensure_resource_name" do
|
29
|
+
it "should add the resource name to the set of ensured resources names" do
|
30
|
+
RestApi.api::RequestParser.ensure_resource_name :my_resource
|
31
|
+
RestApi.api::RequestParser.send(:ensured_resources_names).include?("my_resource").should be == true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should accept many arguments" do
|
35
|
+
RestApi.api::RequestParser.ensure_resource_name :my_resource1, :my_resource2, :my_resource13
|
36
|
+
RestApi.api::RequestParser.send(:ensured_resources_names).include?("my_resource1").should be == true
|
37
|
+
RestApi.api::RequestParser.send(:ensured_resources_names).include?("my_resource2").should be == true
|
38
|
+
RestApi.api::RequestParser.send(:ensured_resources_names).include?("my_resource13").should be == true
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should raise ArgumentError when there is no argument" do
|
42
|
+
lambda {
|
43
|
+
RestApi.api::RequestParser.ensure_resource_name
|
44
|
+
}.should raise_exception(ArgumentError)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
describe "reset_ensure_resource_name" do
|
50
|
+
it "should clear the ensured_resources_names set" do
|
51
|
+
RestApi.api::RequestParser.ensure_resource_name :my_resource
|
52
|
+
RestApi.api::RequestParser.ensure_resource_name :my_resource2
|
53
|
+
RestApi.api::RequestParser.reset_ensure_resource_name
|
54
|
+
|
55
|
+
RestApi.api::RequestParser.send(:ensured_resources_names).length.should be == 0
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "get_url_from_method" do
|
60
|
+
it "should return the corret url with params" do
|
61
|
+
RestApi.api::RequestParser.send(:get_url_from_method, :get_casas_from_usuarios, {:usuarios => 3}).should be == "#{fake_api_url}usuarios/3/casas"
|
62
|
+
|
63
|
+
RestApi.api::RequestParser.send(:get_url_from_method, :put_casas_from_usuarios, {:usuarios => 3, :casas => 5}).should be == "#{fake_api_url}usuarios/3/casas/5"
|
64
|
+
|
65
|
+
RestApi.api::RequestParser.send(:get_url_from_method, :get_show_usuarios, {:usuarios => 3}).should be == "#{fake_api_url}usuarios/3/show"
|
66
|
+
RestApi.api::RequestParser.send(:get_url_from_method, :delete_usuarios, {:usuarios => 2}).should be == "#{fake_api_url}usuarios/2"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return the corret url without params" do
|
70
|
+
RestApi.api::RequestParser.send(:get_url_from_method, :get_casas_from_usuarios).should be == "#{fake_api_url}usuarios/casas"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "get_url_from_map" do
|
75
|
+
let(:array_resources_name) { ["casas", "carros", "usuarios"] }
|
76
|
+
let(:resources_map) {
|
77
|
+
map = OpenStruct.new
|
78
|
+
map.casas = "casas"
|
79
|
+
map.carros = "onibus"
|
80
|
+
map.usuarios = "administrador"
|
81
|
+
map
|
82
|
+
}
|
83
|
+
|
84
|
+
it "should return the corret url - with params (hash 1 item)" do
|
85
|
+
RestApi.api::RequestParser.send(:get_url_from_map, array_resources_name, resources_map, {:casas => 4}).should be == "#{fake_api_url}casas/4/onibus/administrador"
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should return the corret url - with params (hash 3 itens)" do
|
89
|
+
RestApi.api::RequestParser.send(:get_url_from_map, array_resources_name, resources_map, {:casas => 4, :carros => 5, :usuarios => 7}).should be == "#{fake_api_url}casas/4/onibus/5/administrador/7"
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should return the corret url - with params (array 1 item)" do
|
93
|
+
RestApi.api::RequestParser.send(:get_url_from_map, array_resources_name, resources_map, [4]).should be == "#{fake_api_url}casas/4/onibus/administrador"
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should return the corret url - with params (array 3 itens)" do
|
97
|
+
RestApi.api::RequestParser.send(:get_url_from_map, array_resources_name, resources_map, [4, 5, 7]).should be == "#{fake_api_url}casas/4/onibus/5/administrador/7"
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should return the corret url -without params" do
|
101
|
+
RestApi.api::RequestParser.send(:get_url_from_map, array_resources_name, resources_map).should be == "#{fake_api_url}casas/onibus/administrador"
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should return the corret url 2 - without params " do
|
105
|
+
map = OpenStruct.new
|
106
|
+
map.test = "test"
|
107
|
+
RestApi.api::RequestParser.send(:get_url_from_map, ["test"], map).should be == "#{fake_api_url}test"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "get_request_type_from_method" do
|
112
|
+
[:put, :post, :delete, :get].each do |tipo_do_request_esperado|
|
113
|
+
it "should return :#{tipo_do_request_esperado} when the method name start with #{tipo_do_request_esperado}" do
|
114
|
+
RestApi.api::RequestParser.send(:get_request_type_from_method, "#{tipo_do_request_esperado.to_s}_resource".to_sym).should be == tipo_do_request_esperado
|
115
|
+
RestApi.api::RequestParser.send(:get_request_type_from_method, "#{tipo_do_request_esperado.to_s}_resource".to_sym).should be == tipo_do_request_esperado
|
116
|
+
RestApi.api::RequestParser.send(:get_request_type_from_method, "#{tipo_do_request_esperado.to_s}_resource".to_sym).should be == tipo_do_request_esperado
|
117
|
+
RestApi.api::RequestParser.send(:get_request_type_from_method, "#{tipo_do_request_esperado.to_s}_resource".to_sym).should be == tipo_do_request_esperado
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "get_url_tokens_from_method" do
|
123
|
+
["resource1", "resource2", "resource3"].each do |resource|
|
124
|
+
it "one resource - resource name: #{resource}" do
|
125
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "get_#{resource}".to_sym).should be == [resource]
|
126
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "put_#{resource}".to_sym).should be == [resource]
|
127
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "delete_#{resource}".to_sym).should be == [resource]
|
128
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "post_#{resource}".to_sym).should be == [resource]
|
129
|
+
end
|
130
|
+
|
131
|
+
["subresource1", "subresource2", "subresource3"].each do |sub_resource|
|
132
|
+
it "two sub resources" do
|
133
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "get_#{resource}_from_#{sub_resource}".to_sym).should be == [sub_resource, resource]
|
134
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "put_#{resource}_from_#{sub_resource}".to_sym).should be == [sub_resource, resource]
|
135
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "delete_#{resource}_from_#{sub_resource}".to_sym).should be == [sub_resource, resource]
|
136
|
+
RestApi.api::RequestParser.send(:get_url_tokens_from_method, "post_#{resource}_from_#{sub_resource}".to_sym).should be == [sub_resource, resource]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should kept the ensured resources names - one resource" do
|
142
|
+
RestApi.api::RequestParser.stub(:ensured_resources_names).and_return(Set.new(["public_users", "some_resource"]))
|
143
|
+
RestApi.api::RequestParser.get_url_tokens_from_method("get_public_users".to_sym).should be == ["public_users"]
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should kept the ensured resources names - two resources" do
|
147
|
+
RestApi.api::RequestParser.stub(:ensured_resources_names).and_return(Set.new(["public_users", "some_resources"]))
|
148
|
+
RestApi.api::RequestParser.get_url_tokens_from_method("get_public_users_from_some_resources_in_admins_from_system".to_sym).sort.should be == ["public_users", "some_resources", "admins", "system"].sort
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe "insert_resources_params_in_tokens_array" do
|
153
|
+
it "should insert the resource param as string after resource name - one subresource" do
|
154
|
+
tokens_array = ["usuarios", "eventos"]
|
155
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, {:usuarios => 3}).should be == ["usuarios", "3", "eventos"]
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should insert the resource param as string after resource name - four subresources" do
|
159
|
+
tokens_array = ["usuarios", "eventos", "carros", "festas"]
|
160
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, {:usuarios => 5, :carros => 234}).should be == ["usuarios", "5", "eventos", "carros", "234", "festas"]
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should ignore if no params" do
|
164
|
+
tokens_array = ["usuarios", "eventos", "carros", "festas"]
|
165
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, {}).should be == ["usuarios", "eventos", "carros", "festas"]
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should accept params as an array - same number of resources" do
|
169
|
+
tokens_array = ["usuarios", "eventos", "carros", "festas"]
|
170
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, [1,2,3,4]).should be == ["usuarios", "1", "eventos", "2", "carros", "3", "festas", "4"]
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should accept params as an array - differnet number of resources" do
|
174
|
+
tokens_array = ["usuarios", "eventos", "carros", "festas"]
|
175
|
+
RestApi.api::RequestParser.send(:insert_resources_params_in_tokens_array, tokens_array, [1,2,4]).should be == ["usuarios", "1", "eventos", "2", "carros", "4", "festas"]
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "pluralize_resource" do
|
180
|
+
it "should not insert s after digit" do
|
181
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "3").should be == "3"
|
182
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "4533").should be == "4533"
|
183
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "resource3").should be == "resource3"
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should not insert s after s" do
|
187
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "kiss").should be == "kiss"
|
188
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "asas").should be == "asas"
|
189
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "foos").should be == "foos"
|
190
|
+
end
|
191
|
+
|
192
|
+
|
193
|
+
it "should not insert after anything except digit or s" do
|
194
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "usuario").should be == "usuarios"
|
195
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "peixe").should be == "peixes"
|
196
|
+
RestApi.api::RequestParser.send(:pluralize_resource, "carro").should be == "carros"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe "RestApi.api" do
|
6
|
+
let (:fake_api_url) { "http://www.fakeurl.com/" }
|
7
|
+
before(:each) {
|
8
|
+
RestApi.unmap_resources
|
9
|
+
RestApi.setup do |config|
|
10
|
+
config.api_url = "http://www.fakeurl.com/"
|
11
|
+
end
|
12
|
+
}
|
13
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'fakeweb'
|
5
|
+
|
6
|
+
describe "RestApi" do
|
7
|
+
let (:fake_api_url) { "http://www.fakeurl.com/" }
|
8
|
+
before(:each) {
|
9
|
+
RestApi.unmap_resources
|
10
|
+
RestApi.setup do |config|
|
11
|
+
config.api_url = "http://www.fakeurl.com/"
|
12
|
+
end
|
13
|
+
FakeWeb.allow_net_connect = false
|
14
|
+
}
|
15
|
+
|
16
|
+
describe "unmap_resources" do
|
17
|
+
it "should remove all methods" do
|
18
|
+
RestApi.map_custom_api_method :put, :casa_in_usuarios
|
19
|
+
RestApi.unmap_resources
|
20
|
+
RestApi.request.respond_to?(:put_casa_in_usuarios).should be == false
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "add_custom_api_method" do
|
25
|
+
it "should add a method to the api module" do
|
26
|
+
RestApi.map_custom_api_method :put, :casa_in_usuarios
|
27
|
+
RestApi.request.respond_to?(:put_casa_in_usuarios).should be == true
|
28
|
+
RestApi.mapped_methods.include?(:put_casa_in_usuarios).should be == true
|
29
|
+
end
|
30
|
+
|
31
|
+
context "Method add - widthout arg" do
|
32
|
+
it "should return the url of the added method - mapped resources" do
|
33
|
+
RestApi.map_custom_api_method :put, :casas_in_usuarios do |map|
|
34
|
+
map.casas = "casinha"
|
35
|
+
map.usuarios = "usuarionsinhos"
|
36
|
+
end
|
37
|
+
RestApi::RequestHandler.should_receive(:make_request).with(:put, "#{fake_api_url}usuarionsinhos/casinha", {:usuarios_id => 4}).and_return({ "status" => "ok" })
|
38
|
+
RestApi.request.put_casas_in_usuarios(:usuarios_id => 4).should be == { "status" => "ok" }
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return the url of the added method - no mapped resources" do
|
42
|
+
RestApi.map_custom_api_method :put, :casas_in_usuarios do |map|
|
43
|
+
map.casas = "casinha"
|
44
|
+
map.usuarios = "usuarionsinhos"
|
45
|
+
end
|
46
|
+
RestApi::RequestHandler.should_receive(:make_request).with(:put, "#{fake_api_url}usuarionsinhos/4/casinha", {:casinh_id => 8}).and_return({ "status" => "ok" })
|
47
|
+
RestApi.request.put_casas_in_usuarios(4, :casinh_id => 8).should be == { "status" => "ok" }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "add_restful_api_methods" do
|
53
|
+
before(:each) {
|
54
|
+
RestApi.setup do |config|
|
55
|
+
config.api_url = "http://www.fakeurl.com/"
|
56
|
+
config.add_restful_api_methods :users
|
57
|
+
end
|
58
|
+
}
|
59
|
+
|
60
|
+
it "should add a get method to the api request module" do
|
61
|
+
RestApi.request.respond_to?(:get_users).should be == true
|
62
|
+
RestApi.mapped_methods.include?(:get_users).should be == true
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should add a put method to the api request module" do
|
66
|
+
RestApi.request.respond_to?(:put_users).should be == true
|
67
|
+
RestApi.mapped_methods.include?(:put_users).should be == true
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
it "should add a post method to the api request module" do
|
72
|
+
RestApi.request.respond_to?(:post_users).should be == true
|
73
|
+
RestApi.mapped_methods.include?(:post_users).should be == true
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
it "should add a delete method to the api request module" do
|
78
|
+
RestApi.request.respond_to?(:delete_users).should be == true
|
79
|
+
RestApi.mapped_methods.include?(:delete_users).should be == true
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should accept a block to map the resources" do
|
83
|
+
RestApi.add_restful_api_methods :person_in_places do |map|
|
84
|
+
map.person = "people"
|
85
|
+
end
|
86
|
+
RestApi::RequestHandler.should_receive(:make_request).with(:get, "http://www.fakeurl.com/places/2/people", nil).and_return("")
|
87
|
+
RestApi.request.get_person_in_places(2)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'rest_client'
|
5
|
+
require 'fakeweb'
|
6
|
+
|
7
|
+
describe "RestApi::RequestHandler - client" do
|
8
|
+
before :all do
|
9
|
+
FakeWeb.allow_net_connect = false
|
10
|
+
FakeWeb.register_uri(:get, "http://www.teste.com.br", :body => "{ \"casa\": \"teste\", \"rua\": \"teste2\"}")
|
11
|
+
FakeWeb.register_uri(:post, "http://www.teste.com.br/resource2", :body => "{ \"casa2\": \"teste\", \"rua\": \"teste2\"}")
|
12
|
+
FakeWeb.register_uri(:put, "http://www.teste.com.br/resource3", :body => "{ \"casa3\": \"teste\", \"rua3\": \"teste2\"}")
|
13
|
+
FakeWeb.register_uri(:delete, "http://www.teste.com.br/user/3", :body => "{ \"casa6\": \"teste\", \"rua6\": \"teste2\"}")
|
14
|
+
end
|
15
|
+
|
16
|
+
context "rest_client" do
|
17
|
+
it "should call the url with querystring" do
|
18
|
+
FakeWeb.register_uri(:get, "http://www.teste.com.br/user/3?name=value", :body => "{ \"casa6\": \"teste\", \"rua6\": \"teste2\"}")
|
19
|
+
RestClient.get "http://www.teste.com.br/user/3", :params => {:name => "value"}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should raise an ParseResponseException if the response is invalid when response is invalid" do
|
24
|
+
RestClient.should_receive(:get).with("http://www.teste.com.br", :params => {}, :accept=>:json).and_return("")
|
25
|
+
lambda {
|
26
|
+
RestApi::RequestHandler::Client.send(:get, "http://www.teste.com.br")
|
27
|
+
}.should raise_exception(RestApi::Exceptions::ParseResponseException)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "get" do
|
31
|
+
it "should call RestClient get" do
|
32
|
+
RestClient.should_receive(:get).with("http://www.teste.com.br", :params => {}, :accept=>:json).and_return("{ \"casa\": \"teste\", \"rua\": \"teste2\"}")
|
33
|
+
RestApi::RequestHandler::Client.send(:get, "http://www.teste.com.br")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should call parse_response" do
|
37
|
+
RestApi::RequestHandler::Client.should_receive(:parse_response)
|
38
|
+
RestApi::RequestHandler::Client.send(:get, "http://www.teste.com.br/")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should return a parsed json hash" do
|
42
|
+
RestApi::RequestHandler::Client.send(:get, "http://www.teste.com.br/").should be == {"casa"=>"teste", "rua"=>"teste2"}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "post" do
|
47
|
+
it "should call RestClient post" do
|
48
|
+
RestClient.should_receive(:post).with("http://www.teste.com.br/resource2", { :param => true }, :accept=>:json).and_return("{ \"casa\": \"teste\", \"rua\": \"teste2\"}")
|
49
|
+
RestApi::RequestHandler::Client.send(:post, "http://www.teste.com.br/resource2", { :param => true })
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should call parse_response" do
|
53
|
+
RestApi::RequestHandler::Client.should_receive(:parse_response)
|
54
|
+
RestApi::RequestHandler::Client.send(:post, "http://www.teste.com.br/resource2")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should return a parsed json hash" do
|
58
|
+
RestApi::RequestHandler::Client.send(:post, "http://www.teste.com.br/resource2").should be == {"casa2"=>"teste", "rua"=>"teste2"}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "put" do
|
63
|
+
it "should call RestClient post" do
|
64
|
+
RestClient.should_receive(:put).with("http://www.teste.com.br/resource3", { :param => 3 }, :accept=>:json).and_return("{ \"casa\": \"teste\", \"rua\": \"teste2\"}")
|
65
|
+
RestApi::RequestHandler::Client.send(:put, "http://www.teste.com.br/resource3", { :param => 3 })
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should call parse_response" do
|
69
|
+
RestApi::RequestHandler::Client.should_receive(:parse_response)
|
70
|
+
RestApi::RequestHandler::Client.send(:put, "http://www.teste.com.br/resource3")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should return a parsed json hash" do
|
74
|
+
RestApi::RequestHandler::Client.send(:put, "http://www.teste.com.br/resource3").should be == {"casa3"=>"teste", "rua3"=>"teste2"}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "delete" do
|
79
|
+
it "should call RestClient post" do
|
80
|
+
RestClient.should_receive(:delete).with("http://www.teste.com.br/user/3", :params => {}, :accept=>:json).and_return("{ \"casa\": \"teste\", \"rua\": \"teste2\"}")
|
81
|
+
RestApi::RequestHandler::Client.send(:delete, "http://www.teste.com.br/user/3")
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should call parse_response" do
|
85
|
+
RestApi::RequestHandler::Client.should_receive(:parse_response)
|
86
|
+
RestApi::RequestHandler::Client.send(:delete, "http://www.teste.com.br/user/3")
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should return a parsed json hash" do
|
90
|
+
RestApi::RequestHandler::Client.send(:delete, "http://www.teste.com.br/user/3").should be == {"casa6"=>"teste", "rua6"=>"teste2"}
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|