resource_kit 0.0.3 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8bc66e1b067e94ec5db09d088e37cce2fb5a82f8
4
- data.tar.gz: bb016cd88ad60b8d4e4579097baeeec5cd2aab27
3
+ metadata.gz: ee001caaa4f22885773d1f6e6ae076ec900762e2
4
+ data.tar.gz: b373c40127c0a4d7312ecea86ae634acd1ccbe85
5
5
  SHA512:
6
- metadata.gz: 5e2d1b07c54d0a11b6af5a1449965451d7496de420b97084c46e9c96967042364ac3534e4668e2665da284d904900012d4add6a1307e4a8c0588860f3b85883d
7
- data.tar.gz: f3ec8aeee8be01c5aaa3341abddce355c5267a8d1f2e1bbad79dadc8a2cb102b45dcb4e4cb7a206b2b8cd3de60f12fcb46105078f1060b6cdeb7de7cc50072d6
6
+ metadata.gz: dfcc677710aef8a376ac1d47e795b1267f2131ddd969f70352421b5b80788e7099a73dc04cba6282f5fe828ddc2f52ccc19e7856f0fbe591c8463381f4f89eb4
7
+ data.tar.gz: 987624fc56bb90b02ae917c91b8802fcc54697497b1776695fecd915d8b4e3f38dc73a98318bed5cebcc73b08c5c824c8a24f21e993324c8a2757277e0e17534
data/README.md CHANGED
@@ -90,13 +90,12 @@ create = resource.create(Droplet.new)
90
90
 
91
91
  Things we've thought about but just haven't implemented are:
92
92
 
93
- * `action :find, 'PUT droplets/:id/restart'`
94
93
  * Pagination capabilities
95
94
 
96
95
 
97
96
  ## Contributing
98
97
 
99
- 1. Fork it ( https://github.com/[my-github-username]/resource_kit/fork )
98
+ 1. Fork it ( https://github.com/digitaloceancloud/resource_kit/fork )
100
99
  2. Create your feature branch (`git checkout -b my-new-feature`)
101
100
  3. Commit your changes (`git commit -am 'Add some feature'`)
102
101
  4. Push to the branch (`git push origin my-new-feature`)
data/lib/resource_kit.rb CHANGED
@@ -4,6 +4,7 @@ require 'faraday'
4
4
 
5
5
  module ResourceKit
6
6
  ALLOWED_VERBS = [:get, :post, :put, :delete, :head, :patch, :options]
7
+ ActionConnection = Struct.new(:action, :connection)
7
8
 
8
9
  autoload :Resource, 'resource_kit/resource'
9
10
  autoload :ResourceCollection, 'resource_kit/resource_collection'
@@ -6,6 +6,7 @@ module ResourceKit
6
6
  @name = name
7
7
  @verb = (verb && verb.downcase.to_sym) || :get
8
8
  @path = path
9
+ @query_keys = []
9
10
  end
10
11
 
11
12
  def verb(v = nil)
@@ -18,6 +19,11 @@ module ResourceKit
18
19
  @path
19
20
  end
20
21
 
22
+ def query_keys(*keys)
23
+ return @query_keys if keys.empty?
24
+ @query_keys += keys
25
+ end
26
+
21
27
  def handlers
22
28
  @handlers ||= {}
23
29
  end
@@ -1,23 +1,19 @@
1
1
  module ResourceKit
2
2
  class ActionInvoker
3
- def self.call(action, connection, *args)
4
- raise ArgumentError, "Verb '#{action.verb}' is not allowed" unless action.verb.in?(ALLOWED_VERBS)
5
- options = args.last.kind_of?(Hash) ? args.last : {}
6
- resolver = EndpointResolver.new(path: action.path)
3
+ attr_reader :action, :connection, :args, :options
7
4
 
8
- if action.body and action.verb.in?([:post, :put, :patch])
9
- # This request is going to have a response body. Handle it.
10
- response = connection.send(action.verb, resolver.resolve(options)) do |request|
11
- request.body = construct_body(*args, action)
12
- end
13
- else
14
- response = connection.send(action.verb, resolver.resolve(options))
15
- end
5
+ def initialize(action, connection, *args)
6
+ @action = action
7
+ @connection = connection
8
+ @args = args
9
+ @options = args.last.kind_of?(Hash) ? args.last : {}
10
+ end
16
11
 
17
- handle_response(response, action)
12
+ def self.call(action, connection, *args)
13
+ new(action, connection, *args).handle_response
18
14
  end
19
15
 
20
- def self.handle_response(response, action)
16
+ def handle_response
21
17
  if handler = action.handlers[response.status]
22
18
  handler.call(response)
23
19
  else
@@ -25,8 +21,27 @@ module ResourceKit
25
21
  end
26
22
  end
27
23
 
28
- def self.construct_body(*args, action)
24
+ def construct_body
29
25
  action.body.call(*args[0..(action.body.arity - 1)])
30
26
  end
27
+
28
+ def response
29
+ return @response if @response
30
+
31
+ raise ArgumentError, "Verb '#{action.verb}' is not allowed" unless action.verb.in?(ALLOWED_VERBS)
32
+
33
+ if action.body and action.verb.in?([:post, :put, :patch])
34
+ # This request is going to have a response body. Handle it.
35
+ @response = connection.send(action.verb, resolver.resolve(options)) do |request|
36
+ request.body = construct_body
37
+ end
38
+ else
39
+ @response = connection.send(action.verb, resolver.resolve(options))
40
+ end
41
+ end
42
+
43
+ def resolver
44
+ EndpointResolver.new(path: action.path, query_param_keys: action.query_keys)
45
+ end
31
46
  end
32
47
  end
@@ -6,7 +6,7 @@ module ResourceKit
6
6
 
7
7
  def initialize(options = {})
8
8
  @path = options[:path]
9
- @query_param_keys = options[:query_param_keys] || {}
9
+ @query_param_keys = options[:query_param_keys] || []
10
10
  end
11
11
 
12
12
  def resolve(values = {})
@@ -1,6 +1,6 @@
1
1
  module ResourceKit
2
2
  class Resource
3
- class_attribute :namespace
3
+ class_attribute :_resources
4
4
 
5
5
  attr_reader :connection
6
6
 
@@ -9,11 +9,19 @@ module ResourceKit
9
9
  end
10
10
 
11
11
  def self.resources(&block)
12
- @resources ||= ResourceCollection.new
13
- @resources.instance_eval(&block) if block_given?
12
+ self._resources ||= ResourceCollection.new
13
+ self._resources.instance_eval(&block) if block_given?
14
14
 
15
- MethodFactory.construct(self, @resources)
16
- @resources
15
+ MethodFactory.construct(self, self._resources)
16
+ self._resources
17
+ end
18
+
19
+ def action(name)
20
+ _resources.find_action(name)
21
+ end
22
+
23
+ def action_and_connection(action_name)
24
+ ActionConnection.new(action(action_name), connection)
17
25
  end
18
26
  end
19
27
  end
@@ -1,3 +1,3 @@
1
1
  module ResourceKit
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -8,6 +8,7 @@ RSpec.describe ResourceKit::ActionInvoker do
8
8
  stub.get('/users/bad_page') { |env| [404, {}, 'not found'] }
9
9
  stub.get('/users/12') { |env| [200, {}, 'user 12'] }
10
10
  stub.post('/users') { |env| [200, {}, env[:body]] }
11
+ stub.get('/paged') { |env| [200, {}, env[:url].to_s] }
11
12
  end
12
13
  end
13
14
  let(:action) { ResourceKit::Action.new(:find) }
@@ -70,5 +71,17 @@ RSpec.describe ResourceKit::ActionInvoker do
70
71
  expect(result).to eq('echo me another')
71
72
  end
72
73
  end
74
+
75
+ context 'for requests with query params' do
76
+ it 'appends the query parameters to the endpoint' do
77
+ action.query_keys :per_page, :page
78
+ action.path '/paged'
79
+
80
+ result = ResourceKit::ActionInvoker.call(action, connection, page: 3, per_page: 300)
81
+ addressed = Addressable::URI.parse(result)
82
+
83
+ expect(addressed.query_values).to include('per_page' => '300', 'page' => '3')
84
+ end
85
+ end
73
86
  end
74
87
  end
@@ -52,4 +52,11 @@ RSpec.describe ResourceKit::Action do
52
52
  expect(action.body).to be(handler)
53
53
  end
54
54
  end
55
+
56
+ describe '#query_keys' do
57
+ it 'allows setting known query parameters that we should append to the URL' do
58
+ action.query_keys :per_page, :page
59
+ expect(action.query_keys).to include(:per_page, :page)
60
+ end
61
+ end
55
62
  end
@@ -36,4 +36,20 @@ RSpec.describe ResourceKit::Resource do
36
36
  expect(instance.connection).to be(faraday)
37
37
  end
38
38
  end
39
+
40
+ describe '#action' do
41
+ it 'returns the action for the name passed' do
42
+ faraday = Faraday.new(url: 'http://lol.com')
43
+
44
+ class DummyResource < described_class
45
+ resources do
46
+ action :find, 'GET /hello'
47
+ end
48
+ end
49
+
50
+ instance = DummyResource.new(faraday)
51
+
52
+ expect(instance.action(:find)).to be_kind_of(ResourceKit::Action)
53
+ end
54
+ end
39
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resource_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Ross
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-04 00:00:00.000000000 Z
12
+ date: 2014-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday