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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6271c0e0f56abd845d9067c8bc2e55597a532fcd
4
- data.tar.gz: 4898d65f961b2be6121c782efd92fbfd2f153660
3
+ metadata.gz: 8bc66e1b067e94ec5db09d088e37cce2fb5a82f8
4
+ data.tar.gz: bb016cd88ad60b8d4e4579097baeeec5cd2aab27
5
5
  SHA512:
6
- metadata.gz: 1bb8f7fea234fabc6d235eb4a9d3f1829d685b72dad2fe737986fee2dd2a432f9b59d91563fbcde239dbcfe802ec2df4614543b22fd9838ab241163d49b102ea
7
- data.tar.gz: e78e93b61f9301dea5c86db6c3aa219794ff33fab4ad97da122a393d3ee3ecde093ddc34f751c80c8fd3f79405b860a7254b8026dff4571473ee6307889b0d0f
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
@@ -3,6 +3,8 @@ require 'active_support/core_ext'
3
3
  require 'faraday'
4
4
 
5
5
  module ResourceKit
6
+ ALLOWED_VERBS = [:get, :post, :put, :delete, :head, :patch, :options]
7
+
6
8
  autoload :Resource, 'resource_kit/resource'
7
9
  autoload :ResourceCollection, 'resource_kit/resource_collection'
8
10
 
@@ -2,8 +2,10 @@ module ResourceKit
2
2
  class Action
3
3
  attr_reader :name
4
4
 
5
- def initialize(name)
5
+ def initialize(name, verb = nil, path = nil)
6
6
  @name = name
7
+ @verb = (verb && verb.downcase.to_sym) || :get
8
+ @path = path
7
9
  end
8
10
 
9
11
  def verb(v = nil)
@@ -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.extract_options!
5
+ options = args.last.kind_of?(Hash) ? args.last : {}
8
6
  resolver = EndpointResolver.new(path: action.path)
9
7
 
10
- if args.size > 0 and action.verb.in?([:post, :put, :patch])
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.first, action)
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(object, action)
31
- if action.body
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
@@ -1,3 +1,3 @@
1
1
  module ResourceKit
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
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 'includes the contents of the body in the request' do
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('echo me')
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 {|object| 'i am a banana' }
67
+ action.body {|first, second| first + second }
67
68
 
68
- result = ResourceKit::ActionInvoker.call(action, connection, 'echo me')
69
- expect(result).to eq('i am a banana')
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.1
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-02 00:00:00.000000000 Z
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: {}