api-resource 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/api-resource/resource.rb +7 -3
- data/lib/api-resource/version.rb +1 -1
- data/spec/blog_api.rb +20 -0
- data/spec/resource_path_spec.rb +34 -0
- data/spec/resource_spec.rb +2 -21
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d337a40cfc027bdc85680315d1b0c87557a7e1b
|
4
|
+
data.tar.gz: cff63a9be56dd173e1a4df49be49d6397efc10e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcf0645d2a1e55149238a85dae91fcc374bc6406e0034dfc0ab3258a62568012f8871500e28b8c30fbeb6acbbfe020c678efdc630d757c5eece7b3255972e505
|
7
|
+
data.tar.gz: d27781a96ca4e611aef5bcab3ae42747564210648b7708d1e8262a0acb204f9b91f3c2daa89558b22719ebab5467ab7f9b716c5ce4dd1528c78238823fa9a882
|
@@ -162,7 +162,7 @@ module ApiResource
|
|
162
162
|
def self.method_missing(m, *args, &_)
|
163
163
|
case (m)
|
164
164
|
when /^by_(.+)/
|
165
|
-
resource_path =
|
165
|
+
resource_path = build_path($1.pluralize, args[0], self.resource_path)
|
166
166
|
Class.new(self) do
|
167
167
|
self.resource_path = resource_path
|
168
168
|
end
|
@@ -174,7 +174,7 @@ module ApiResource
|
|
174
174
|
def method_missing(m, *args, &_)
|
175
175
|
case m
|
176
176
|
when /^by_(.+)/
|
177
|
-
self.resource_path = self.class.
|
177
|
+
self.resource_path = self.class.build_path($1.pluralize, args[0], resource_path)
|
178
178
|
self
|
179
179
|
else
|
180
180
|
super
|
@@ -240,7 +240,11 @@ module ApiResource
|
|
240
240
|
|
241
241
|
def self.build_url(*paths)
|
242
242
|
raise RuntimeError, 'Empty base_url' if base_url.blank?
|
243
|
-
URI::parse(base_url).merge(paths
|
243
|
+
URI::parse(base_url).merge(build_path(*paths)).to_s
|
244
|
+
end
|
245
|
+
|
246
|
+
def self.build_path(*paths)
|
247
|
+
paths.map { |p| URI.encode p.to_s.gsub(%r{\A/*(.*?)/*\z}, '\1') }.join('/')
|
244
248
|
end
|
245
249
|
|
246
250
|
def self.log=(param)
|
data/lib/api-resource/version.rb
CHANGED
data/spec/blog_api.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module BlogApi
|
2
|
+
class Resource < ApiResource::Resource
|
3
|
+
self.base_url = 'http://api.example.com'
|
4
|
+
end
|
5
|
+
|
6
|
+
class Category < Resource
|
7
|
+
attr_accessor :id, :name
|
8
|
+
end
|
9
|
+
|
10
|
+
class Blog < Resource
|
11
|
+
attr_accessor :id, :title
|
12
|
+
|
13
|
+
def do_stuff_method
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Tag < Resource
|
18
|
+
attr_accessor :id, :name
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'blog_api'
|
2
|
+
|
3
|
+
RSpec.describe ApiResource::Resource do
|
4
|
+
|
5
|
+
class MyResource < ApiResource::Resource; end
|
6
|
+
|
7
|
+
let(:resource_class) { MyResource }
|
8
|
+
|
9
|
+
let(:resource_name) { MyResource.to_s.tableize }
|
10
|
+
|
11
|
+
it 'return tabeleized class name' do
|
12
|
+
expect(resource_class.resource_path).to eq(resource_name)
|
13
|
+
end
|
14
|
+
|
15
|
+
context '::by_resource' do
|
16
|
+
it 'prepends parent resource path' do
|
17
|
+
expect(resource_class.by_foo_bar('44').resource_path).to eq("foo_bars/44/#{ resource_name }")
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'prepends multiple parent resource paths' do
|
21
|
+
expect(resource_class.by_bar(15).by_foo(88).resource_path).to eq("foos/88/bars/15/#{ resource_name }")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context '#by_resource' do
|
26
|
+
it 'prepends parent resource path' do
|
27
|
+
expect(resource_class.new.by_foo_bar('44').resource_path).to eq("foo_bars/44/#{ resource_name }")
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'prepends multiple parent resource paths' do
|
31
|
+
expect(resource_class.new.by_bar(15).by_foo(88).resource_path).to eq("foos/88/bars/15/#{ resource_name }")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/spec/resource_spec.rb
CHANGED
@@ -1,25 +1,6 @@
|
|
1
|
-
|
2
|
-
module BlogApi
|
3
|
-
class Resource < ApiResource::Resource
|
4
|
-
self.base_url = 'http://api.example.com'
|
5
|
-
end
|
6
|
-
|
7
|
-
class Category < Resource
|
8
|
-
attr_accessor :id, :name
|
9
|
-
end
|
10
|
-
|
11
|
-
class Blog < Resource
|
12
|
-
attr_accessor :id, :title
|
13
|
-
|
14
|
-
def do_stuff_method
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class Tag < Resource
|
19
|
-
attr_accessor :id, :name
|
20
|
-
end
|
21
|
-
end
|
1
|
+
require_relative 'blog_api'
|
22
2
|
|
3
|
+
RSpec.describe ApiResource::Resource do
|
23
4
|
before do
|
24
5
|
@expected_resources = { data: [{ id: 1257, title: 'Hello' },
|
25
6
|
{ id: 33, title: 'World' },
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api-resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chaker Nakhli
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple-hmac
|
@@ -135,6 +135,8 @@ files:
|
|
135
135
|
- lib/api-resource.rb
|
136
136
|
- lib/api-resource/resource.rb
|
137
137
|
- lib/api-resource/version.rb
|
138
|
+
- spec/blog_api.rb
|
139
|
+
- spec/resource_path_spec.rb
|
138
140
|
- spec/resource_spec.rb
|
139
141
|
- spec/spec_helper.rb
|
140
142
|
homepage: http://www.wizypay.com
|
@@ -163,5 +165,7 @@ signing_key:
|
|
163
165
|
specification_version: 4
|
164
166
|
summary: A lightweight REST API access library.
|
165
167
|
test_files:
|
168
|
+
- spec/blog_api.rb
|
169
|
+
- spec/resource_path_spec.rb
|
166
170
|
- spec/resource_spec.rb
|
167
171
|
- spec/spec_helper.rb
|