resource_kit 0.0.1 → 0.0.3
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.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/resource_kit.rb +2 -0
- data/lib/resource_kit/action.rb +3 -1
- data/lib/resource_kit/action_invoker.rb +5 -11
- data/lib/resource_kit/resource_collection.rb +26 -2
- data/lib/resource_kit/version.rb +1 -1
- data/resource_kit.gemspec +3 -3
- data/spec/lib/resource_kit/action_invoker_spec.rb +7 -6
- data/spec/lib/resource_kit/resource_collection_spec.rb +28 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bc66e1b067e94ec5db09d088e37cce2fb5a82f8
|
4
|
+
data.tar.gz: bb016cd88ad60b8d4e4579097baeeec5cd2aab27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e2d1b07c54d0a11b6af5a1449965451d7496de420b97084c46e9c96967042364ac3534e4668e2665da284d904900012d4add6a1307e4a8c0588860f3b85883d
|
7
|
+
data.tar.gz: f3ec8aeee8be01c5aaa3341abddce355c5267a8d1f2e1bbad79dadc8a2cb102b45dcb4e4cb7a206b2b8cd3de60f12fcb46105078f1060b6cdeb7de7cc50072d6
|
data/README.md
CHANGED
@@ -56,6 +56,18 @@ class DropletResource < ResourceKit::Resource
|
|
56
56
|
end
|
57
57
|
```
|
58
58
|
|
59
|
+
You also have the option to use a shorter version to describe actions like this:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class DropletResource < ResourceKit::Resource
|
63
|
+
resources do
|
64
|
+
action :all, 'GET /v2/droplets' do
|
65
|
+
handler(:ok) { |response| DropletMapping.extract_collection(response.body, :read) }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
59
71
|
Now that we've described our resources. We can instantiate our class with a connection object. ResourceKit relies on the interface that Faraday provides. For example:
|
60
72
|
|
61
73
|
```ruby
|
data/lib/resource_kit.rb
CHANGED
data/lib/resource_kit/action.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
module ResourceKit
|
2
2
|
class ActionInvoker
|
3
|
-
ALLOWED_VERBS = [:get, :post, :put, :delete, :head, :patch, :options]
|
4
|
-
|
5
3
|
def self.call(action, connection, *args)
|
6
4
|
raise ArgumentError, "Verb '#{action.verb}' is not allowed" unless action.verb.in?(ALLOWED_VERBS)
|
7
|
-
options = args.
|
5
|
+
options = args.last.kind_of?(Hash) ? args.last : {}
|
8
6
|
resolver = EndpointResolver.new(path: action.path)
|
9
7
|
|
10
|
-
if
|
8
|
+
if action.body and action.verb.in?([:post, :put, :patch])
|
11
9
|
# This request is going to have a response body. Handle it.
|
12
10
|
response = connection.send(action.verb, resolver.resolve(options)) do |request|
|
13
|
-
request.body = construct_body(args
|
11
|
+
request.body = construct_body(*args, action)
|
14
12
|
end
|
15
13
|
else
|
16
14
|
response = connection.send(action.verb, resolver.resolve(options))
|
@@ -27,12 +25,8 @@ module ResourceKit
|
|
27
25
|
end
|
28
26
|
end
|
29
27
|
|
30
|
-
def self.construct_body(
|
31
|
-
|
32
|
-
action.body.call(object)
|
33
|
-
else
|
34
|
-
object.to_s
|
35
|
-
end
|
28
|
+
def self.construct_body(*args, action)
|
29
|
+
action.body.call(*args[0..(action.body.arity - 1)])
|
36
30
|
end
|
37
31
|
end
|
38
32
|
end
|
@@ -7,16 +7,40 @@ module ResourceKit
|
|
7
7
|
@collection = []
|
8
8
|
end
|
9
9
|
|
10
|
-
def action(name, &block)
|
11
|
-
action = Action.new(name)
|
10
|
+
def action(name, verb_and_path = nil, &block)
|
11
|
+
action = Action.new(name, *parse_verb_and_path(verb_and_path))
|
12
|
+
action.handlers.merge!(default_handlers.dup)
|
12
13
|
action.instance_eval(&block) if block_given?
|
13
14
|
action.tap {|a| self << a }
|
14
15
|
end
|
15
16
|
|
17
|
+
def default_handler(*response_codes, &block)
|
18
|
+
response_codes.each do |code|
|
19
|
+
unless code.is_a?(Fixnum)
|
20
|
+
code = StatusCodeMapper.code_for(code)
|
21
|
+
end
|
22
|
+
default_handlers[code] = block
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def default_handlers
|
27
|
+
@default_handlers ||= {}
|
28
|
+
end
|
29
|
+
|
16
30
|
def find_action(name)
|
17
31
|
find do |action|
|
18
32
|
action.name == name
|
19
33
|
end
|
20
34
|
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def parse_verb_and_path(verb_and_path)
|
39
|
+
return [] unless verb_and_path
|
40
|
+
regex = /(?<verb>GET|POST|HEAD|PUT|PATCH|DELETE|OPTIONS)?\s*(?<path>.+)?/i
|
41
|
+
matched = verb_and_path.match(regex)
|
42
|
+
|
43
|
+
return matched[:verb], matched[:path]
|
44
|
+
end
|
21
45
|
end
|
22
46
|
end
|
data/lib/resource_kit/version.rb
CHANGED
data/resource_kit.gemspec
CHANGED
@@ -6,11 +6,11 @@ require 'resource_kit/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "resource_kit"
|
8
8
|
spec.version = ResourceKit::VERSION
|
9
|
-
spec.authors = ["Robert Ross"]
|
10
|
-
spec.email = ["rross@digitalocean.com"]
|
9
|
+
spec.authors = ["Robert Ross", "Ivan Vanderbyl"]
|
10
|
+
spec.email = ["rross@digitalocean.com", "ivan@digitalocean.com"]
|
11
11
|
spec.summary = %q{Resource Kit provides tools to aid in making API Clients. Such as URL resolving, Request / Response layer, and more.}
|
12
12
|
spec.description = ''
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/digitaloceancloud/resource_kit"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -52,21 +52,22 @@ RSpec.describe ResourceKit::ActionInvoker do
|
|
52
52
|
end
|
53
53
|
|
54
54
|
context 'for requests with bodies' do
|
55
|
-
it '
|
55
|
+
it 'uses the body handler when present' do
|
56
56
|
action.verb :post
|
57
57
|
action.path '/users'
|
58
|
+
action.body {|object| 'i am a banana' }
|
58
59
|
|
59
60
|
result = ResourceKit::ActionInvoker.call(action, connection, 'echo me')
|
60
|
-
expect(result).to eq('
|
61
|
+
expect(result).to eq('i am a banana')
|
61
62
|
end
|
62
63
|
|
63
|
-
it 'uses the body handler when present' do
|
64
|
+
it 'uses the body handler with multiple arity when present' do
|
64
65
|
action.verb :post
|
65
66
|
action.path '/users'
|
66
|
-
action.body {|
|
67
|
+
action.body {|first, second| first + second }
|
67
68
|
|
68
|
-
result = ResourceKit::ActionInvoker.call(action, connection, 'echo me')
|
69
|
-
expect(result).to eq('
|
69
|
+
result = ResourceKit::ActionInvoker.call(action, connection, 'echo me', ' another')
|
70
|
+
expect(result).to eq('echo me another')
|
70
71
|
end
|
71
72
|
end
|
72
73
|
end
|
@@ -3,6 +3,16 @@ require 'spec_helper'
|
|
3
3
|
RSpec.describe ResourceKit::ResourceCollection do
|
4
4
|
subject(:collection) { ResourceKit::ResourceCollection.new }
|
5
5
|
|
6
|
+
describe '#default_handler' do
|
7
|
+
it 'adds the passed black to a hash of handlers on the resource collection' do
|
8
|
+
handler_block = Proc.new {|b| 'whut whut' }
|
9
|
+
collection.default_handler(:ok, :no_content, &handler_block)
|
10
|
+
|
11
|
+
expect(collection.default_handlers[200]).to eq(handler_block)
|
12
|
+
expect(collection.default_handlers[204]).to eq(handler_block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
6
16
|
describe '#action' do
|
7
17
|
it 'yields an action to the block' do
|
8
18
|
expect {|b| collection.action(:all, &b) }.to yield_with_args(instance_of(ResourceKit::Action))
|
@@ -12,6 +22,24 @@ RSpec.describe ResourceKit::ResourceCollection do
|
|
12
22
|
action = collection.action :all
|
13
23
|
expect(collection).to include(action)
|
14
24
|
end
|
25
|
+
|
26
|
+
it "accepts a second argument of VERB /resource" do
|
27
|
+
action = collection.action :all, 'GET /v2/droplets'
|
28
|
+
expect(action.verb).to eq :get
|
29
|
+
expect(action.path).to eq '/v2/droplets'
|
30
|
+
expect(action.name).to eq :all
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when default handlers have been specified on the collection' do
|
34
|
+
let(:handler) { Proc.new {|response| 'sure' } }
|
35
|
+
|
36
|
+
before { collection.default_handler(:ok, &handler) }
|
37
|
+
|
38
|
+
it 'prepends the default handlers to the test' do
|
39
|
+
action = collection.action(:all)
|
40
|
+
expect(action.handlers[200]).to eq(handler)
|
41
|
+
end
|
42
|
+
end
|
15
43
|
end
|
16
44
|
|
17
45
|
describe '#find_action' do
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resource_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Ross
|
8
|
+
- Ivan Vanderbyl
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
12
|
+
date: 2014-08-04 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: faraday
|
@@ -125,6 +126,7 @@ dependencies:
|
|
125
126
|
description: ''
|
126
127
|
email:
|
127
128
|
- rross@digitalocean.com
|
129
|
+
- ivan@digitalocean.com
|
128
130
|
executables: []
|
129
131
|
extensions: []
|
130
132
|
extra_rdoc_files: []
|
@@ -154,7 +156,7 @@ files:
|
|
154
156
|
- spec/lib/resource_kit/resource_spec.rb
|
155
157
|
- spec/lib/resource_kit/status_code_mapper_spec.rb
|
156
158
|
- spec/spec_helper.rb
|
157
|
-
homepage:
|
159
|
+
homepage: https://github.com/digitaloceancloud/resource_kit
|
158
160
|
licenses:
|
159
161
|
- MIT
|
160
162
|
metadata: {}
|