restful_resource 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/restful_resource/base.rb +22 -8
- data/lib/restful_resource/version.rb +1 -1
- data/spec/restful_resource/base_spec.rb +16 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa53e7f3d911a813da5c422ed9caab2cb57814b2
|
4
|
+
data.tar.gz: 8b94707bb2dc21e8d76cf56571a0aadee616fa9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 734519cb8a122bc2e5fa5ae0a9f5dda9a4f34cd29831f0289431c968e3482e3ce086e11755f40bc7a9f67d8dc481c5848a6b8206aaba543a72081313de4cda4d
|
7
|
+
data.tar.gz: 02df5ab226f3368ac23ef39fdb7e23f948dc7e1ab311db4510d4015bdfb8859c5c761f9409794b393a0c5ddd01c58422bbbff867cbe48d038f58ac28d5f1499f
|
@@ -4,20 +4,33 @@ module RestfulResource
|
|
4
4
|
@url = url
|
5
5
|
end
|
6
6
|
|
7
|
-
def self.
|
7
|
+
def self.processed_url_and_params(params={})
|
8
8
|
url = @url
|
9
|
-
params.
|
10
|
-
|
9
|
+
other_params = params.clone
|
10
|
+
missing_params = []
|
11
|
+
|
12
|
+
url_params = url.scan(/:([A-Za-z][^\/]*)/).flatten
|
13
|
+
url_params.each do |key|
|
14
|
+
value = other_params.delete(key.to_sym)
|
15
|
+
if value.nil?
|
16
|
+
missing_params << key
|
17
|
+
else
|
18
|
+
url = url.gsub(':'+key.to_s, value.to_s)
|
19
|
+
end
|
11
20
|
end
|
12
21
|
|
13
|
-
|
14
|
-
|
15
|
-
raise ParameterMissingError.new(url_params)
|
22
|
+
if missing_params.any?
|
23
|
+
raise ParameterMissingError.new(missing_params)
|
16
24
|
end
|
17
25
|
|
18
|
-
url
|
26
|
+
[url, other_params]
|
19
27
|
end
|
20
28
|
|
29
|
+
def self.url(params={})
|
30
|
+
processed_url_and_params(params).first
|
31
|
+
end
|
32
|
+
|
33
|
+
|
21
34
|
def self.has_one(nested_resource_type)
|
22
35
|
klass = nested_resource_type.to_s.camelize.safe_constantize
|
23
36
|
klass = OpenStruct if (klass.nil? || klass.superclass != RestResource)
|
@@ -81,7 +94,8 @@ module RestfulResource
|
|
81
94
|
end
|
82
95
|
|
83
96
|
def self.all(params = {})
|
84
|
-
|
97
|
+
url, other_params = processed_url_and_params(params)
|
98
|
+
response = RestClient.get("#{url}", params: other_params)
|
85
99
|
paginate_response(response)
|
86
100
|
end
|
87
101
|
|
@@ -53,10 +53,7 @@ describe RestfulResource::Base do
|
|
53
53
|
|
54
54
|
context "#all" do
|
55
55
|
it "should provide a paginated result if response contains rest pagination headers" do
|
56
|
-
response =
|
57
|
-
allow(response).to receive(:headers) {
|
58
|
-
{:links => '<http://api.carwow.co.uk/teams.json?page=6>;rel="last",<http://api.carwow.co.uk/teams.json?page=2>;rel="next"'}
|
59
|
-
}
|
56
|
+
response = response_with_page_information()
|
60
57
|
stub_get('http://api.carwow.co.uk/teams', response)
|
61
58
|
|
62
59
|
teams = Team.all
|
@@ -75,12 +72,27 @@ describe RestfulResource::Base do
|
|
75
72
|
expect(example.surname).to eq 'Santoro'
|
76
73
|
end
|
77
74
|
|
75
|
+
it "should use some params for the url and other for the query string" do
|
76
|
+
stub_get('http://api.carwow.co.uk/teams/15/players', response_with_page_information,
|
77
|
+
{name_like: 'Ars'})
|
78
|
+
|
79
|
+
players = Player.all(team_id: 15, name_like: 'Ars')
|
80
|
+
end
|
81
|
+
|
78
82
|
private
|
79
83
|
def stub_get(url, fake_response, params = {})
|
80
84
|
expect(RestClient).to receive(:get).
|
81
85
|
with(url, params: params).
|
82
86
|
and_return(fake_response)
|
83
87
|
end
|
88
|
+
|
89
|
+
def response_with_page_information
|
90
|
+
response = [{ id: 1, name: 'Arsenal'}, { id: 2, name: 'Chelsea' }].to_json
|
91
|
+
allow(response).to receive(:headers) {
|
92
|
+
{:links => '<http://api.carwow.co.uk/teams.json?page=6>;rel="last",<http://api.carwow.co.uk/teams.json?page=2>;rel="next"'}
|
93
|
+
}
|
94
|
+
response
|
95
|
+
end
|
84
96
|
end
|
85
97
|
|
86
98
|
class Team < RestfulResource::Base
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Santoro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|