acfs 1.3.2 → 1.3.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.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. metadata +8 -141
  3. data/CHANGELOG.md +0 -325
  4. data/LICENSE +0 -22
  5. data/README.md +0 -334
  6. data/acfs.gemspec +0 -37
  7. data/lib/acfs.rb +0 -49
  8. data/lib/acfs/adapter/base.rb +0 -26
  9. data/lib/acfs/adapter/typhoeus.rb +0 -60
  10. data/lib/acfs/collection.rb +0 -27
  11. data/lib/acfs/collections/paginatable.rb +0 -75
  12. data/lib/acfs/configuration.rb +0 -113
  13. data/lib/acfs/errors.rb +0 -106
  14. data/lib/acfs/global.rb +0 -99
  15. data/lib/acfs/location.rb +0 -78
  16. data/lib/acfs/middleware/base.rb +0 -20
  17. data/lib/acfs/middleware/json.rb +0 -27
  18. data/lib/acfs/middleware/logger.rb +0 -23
  19. data/lib/acfs/middleware/msgpack.rb +0 -30
  20. data/lib/acfs/middleware/print.rb +0 -21
  21. data/lib/acfs/middleware/serializer.rb +0 -39
  22. data/lib/acfs/operation.rb +0 -81
  23. data/lib/acfs/request.rb +0 -36
  24. data/lib/acfs/request/callbacks.rb +0 -50
  25. data/lib/acfs/resource.rb +0 -37
  26. data/lib/acfs/resource/attributes.rb +0 -268
  27. data/lib/acfs/resource/attributes/base.rb +0 -28
  28. data/lib/acfs/resource/attributes/boolean.rb +0 -37
  29. data/lib/acfs/resource/attributes/date_time.rb +0 -31
  30. data/lib/acfs/resource/attributes/dict.rb +0 -37
  31. data/lib/acfs/resource/attributes/float.rb +0 -31
  32. data/lib/acfs/resource/attributes/integer.rb +0 -27
  33. data/lib/acfs/resource/attributes/list.rb +0 -34
  34. data/lib/acfs/resource/attributes/string.rb +0 -24
  35. data/lib/acfs/resource/attributes/uuid.rb +0 -47
  36. data/lib/acfs/resource/dirty.rb +0 -35
  37. data/lib/acfs/resource/initialization.rb +0 -29
  38. data/lib/acfs/resource/loadable.rb +0 -33
  39. data/lib/acfs/resource/locatable.rb +0 -128
  40. data/lib/acfs/resource/operational.rb +0 -22
  41. data/lib/acfs/resource/persistence.rb +0 -257
  42. data/lib/acfs/resource/query_methods.rb +0 -264
  43. data/lib/acfs/resource/service.rb +0 -42
  44. data/lib/acfs/resource/validation.rb +0 -37
  45. data/lib/acfs/response.rb +0 -28
  46. data/lib/acfs/response/formats.rb +0 -25
  47. data/lib/acfs/response/status.rb +0 -31
  48. data/lib/acfs/rspec.rb +0 -11
  49. data/lib/acfs/runner.rb +0 -97
  50. data/lib/acfs/service.rb +0 -91
  51. data/lib/acfs/service/middleware.rb +0 -56
  52. data/lib/acfs/service/middleware/stack.rb +0 -63
  53. data/lib/acfs/singleton_resource.rb +0 -83
  54. data/lib/acfs/stub.rb +0 -172
  55. data/lib/acfs/util.rb +0 -20
  56. data/lib/acfs/version.rb +0 -14
  57. data/lib/acfs/yard.rb +0 -5
  58. data/spec/acfs/adapter/typhoeus_spec.rb +0 -28
  59. data/spec/acfs/collection_spec.rb +0 -155
  60. data/spec/acfs/configuration_spec.rb +0 -51
  61. data/spec/acfs/global_spec.rb +0 -137
  62. data/spec/acfs/location_spec.rb +0 -23
  63. data/spec/acfs/middleware/json_spec.rb +0 -63
  64. data/spec/acfs/middleware/msgpack_spec.rb +0 -60
  65. data/spec/acfs/operation_spec.rb +0 -10
  66. data/spec/acfs/request/callbacks_spec.rb +0 -46
  67. data/spec/acfs/request_spec.rb +0 -77
  68. data/spec/acfs/resource/attributes/boolean_spec.rb +0 -56
  69. data/spec/acfs/resource/attributes/date_time_spec.rb +0 -49
  70. data/spec/acfs/resource/attributes/dict_spec.rb +0 -75
  71. data/spec/acfs/resource/attributes/float_spec.rb +0 -59
  72. data/spec/acfs/resource/attributes/integer_spec.rb +0 -34
  73. data/spec/acfs/resource/attributes/list_spec.rb +0 -58
  74. data/spec/acfs/resource/attributes/uuid_spec.rb +0 -40
  75. data/spec/acfs/resource/attributes_spec.rb +0 -179
  76. data/spec/acfs/resource/dirty_spec.rb +0 -47
  77. data/spec/acfs/resource/initialization_spec.rb +0 -30
  78. data/spec/acfs/resource/loadable_spec.rb +0 -20
  79. data/spec/acfs/resource/locatable_spec.rb +0 -116
  80. data/spec/acfs/resource/persistance_spec.rb +0 -316
  81. data/spec/acfs/resource/query_methods_spec.rb +0 -541
  82. data/spec/acfs/resource/validation_spec.rb +0 -127
  83. data/spec/acfs/response/formats_spec.rb +0 -50
  84. data/spec/acfs/response/status_spec.rb +0 -69
  85. data/spec/acfs/runner_spec.rb +0 -97
  86. data/spec/acfs/service/middleware_spec.rb +0 -33
  87. data/spec/acfs/service_spec.rb +0 -46
  88. data/spec/acfs/singleton_resource_spec.rb +0 -15
  89. data/spec/acfs/stub_spec.rb +0 -343
  90. data/spec/acfs_spec.rb +0 -203
  91. data/spec/fixtures/config.yml +0 -14
  92. data/spec/spec_helper.rb +0 -41
  93. data/spec/support/hash.rb +0 -9
  94. data/spec/support/response.rb +0 -10
  95. data/spec/support/service.rb +0 -91
  96. data/spec/support/shared/find_callbacks.rb +0 -48
@@ -1,264 +0,0 @@
1
- class Acfs::Resource
2
- # Methods providing the query interface for finding resouces.
3
- #
4
- # @example
5
- # class MyUser < Acfs::Resource
6
- # end
7
- #
8
- # MyUser.find(5) # Find single resource
9
- # MyUser.all # Full or partial collection of
10
- # # resources
11
- # Comment.where(user: user.id) # Collection with additional parameter
12
- # # to filter resources
13
- #
14
- module QueryMethods
15
- extend ActiveSupport::Concern
16
-
17
- #
18
- module ClassMethods
19
- # @api public
20
- #
21
- # @overload find(id, opts = {})
22
- # Find a single resource by given ID.
23
- #
24
- # @example
25
- # user = User.find(5) # Will query `http://base.url/users/5`
26
- #
27
- # @param id [Fixnum] Resource IDs to fetch from remote service.
28
- # @param params [Hash] Additional options.
29
- # @option opts [Hash] :params Additional parameters added to
30
- # request. `:id` will be overridden with given ID.
31
- #
32
- # @yield [resource] Callback block to be executed after resource
33
- # was fetched successfully.
34
- # @yieldparam resource [self] Fetched resources.
35
- #
36
- # @return [self] Resource object if only one ID was given.
37
- #
38
- # @overload find(ids, opts = {})
39
- # Load collection of specified resources by given IDs.
40
- #
41
- # @example
42
- # User.find([1, 2, 5]) # Will return collection and will request
43
- # # `http://base.url/users/1`,
44
- # # `http://base.url/users/2`
45
- # # and `http://base.url/users/5` parallel
46
- #
47
- # @param ids [Array<Integer>] List of resource IDs.
48
- # @param opts [Hash] Additional options.
49
- # @option opts [Hash] :params Additional parameters added to
50
- # request. `:id` will be overridden with individual resource ID.
51
- #
52
- # @yield [collection] Callback block to be executed after collection
53
- # was fetched successfully.
54
- # @yieldparam resource [Collection] Collection with fetched resources.
55
- #
56
- # @return [Collection] Collection of requested resources.
57
- #
58
- def find(id_or_ids, opts = {}, &block)
59
- if id_or_ids.respond_to? :each
60
- find_multiple id_or_ids, opts, &block
61
- else
62
- find_single id_or_ids, opts, &block
63
- end
64
- end
65
-
66
- # @api public
67
- #
68
- # Try to load all resources.
69
- #
70
- # @param params [Hash] Request parameters that will be send to
71
- # remote service.
72
- #
73
- # @yield [collection] Callback block to be executed when resource
74
- # collection was loaded successfully.
75
- # @yieldparam collection [Collection] Collection of fetched resources.
76
- #
77
- # @return [Collection] Collection of requested resources.
78
- #
79
- def all(params = {}, opts = {}, &block)
80
- collection = ::Acfs::Collection.new self
81
- collection.__callbacks__ << block if block
82
-
83
- operation :list, opts.merge(params: params) do |data, response|
84
- data.each {|obj| collection << create_resource(obj) }
85
- collection.process_response response
86
- collection.loaded!
87
- collection.__invoke__
88
- end
89
-
90
- collection
91
- end
92
- alias_method :where, :all
93
-
94
- # @api public
95
- #
96
- # Try to load first resource. Return nil if no object can be loaded.
97
- #
98
- # @param params [Hash] Request parameters that will be send
99
- # to remote service.
100
- #
101
- # @yield [resource] Callback block to be executed after
102
- # resource was fetched (even if nil).
103
- # @yieldparam resource [self] Fetched resource, nil
104
- # if empty list is returned
105
- #
106
- # @return [self] Resource object, nil if empty list is returned
107
- #
108
- def find_by(params, &block)
109
- Acfs::Util::ResourceDelegator.new(new).tap do |m|
110
- m.__callbacks__ << block unless block.nil?
111
- operation :list, params: params do |data|
112
- if data.empty?
113
- m.__setobj__ nil
114
- else
115
- m.__setobj__ create_resource(data.first, origin: m.__getobj__)
116
- end
117
- m.__invoke__
118
- end
119
- end
120
- end
121
-
122
- # @api public
123
- #
124
- # Try to load first resource. Raise Acfs::ResourceNotFound
125
- # exception if no object can be loaded.
126
- #
127
- # @param params [Hash] Request parameters that will be send
128
- # to remote service.
129
- #
130
- # @yield [resource] Callback block to be executed after
131
- # resource was fetched successfully.
132
- # @yieldparam resource [self] Fetched resource, nil
133
- # if empty list is returned
134
- #
135
- # @return [self] Resource object, nil if empty list is returned
136
- #
137
- def find_by!(params, &block)
138
- find_by params do |m|
139
- if m.nil?
140
- raise Acfs::ResourceNotFound.new message: 'Received erroneous ' \
141
- "response: no `#{name}` with params #{params} found"
142
- end
143
- block.call m unless block.nil?
144
- end
145
- end
146
-
147
- # @api public
148
- #
149
- # Iterates over all pages returned by index action.
150
- #
151
- # Server must return a paginated resource.
152
- #
153
- # @example
154
- # User.each_page do |page|
155
- # p page.size
156
- # end
157
- # Acfs.run
158
- # # => 50
159
- # # => 50
160
- # # => 42
161
- #
162
- # @param opts [Hash] Options passed to {#where}.
163
- #
164
- # @yield [collection] Callback that will be invoked for each page.
165
- # @yieldparam collection [Collection] Paginated collection.
166
- #
167
- # @return [Collection] First page.
168
- #
169
- def each_page(opts = {})
170
- cb = proc do |collection|
171
- yield collection
172
- collection.next_page(&cb)
173
- end
174
- where opts, &cb
175
- end
176
-
177
- # @api public
178
- #
179
- # Iterates over all items of all pages returned by index action.
180
- #
181
- # Server must return a paginated resource.
182
- #
183
- # @example
184
- # index = 0
185
- # User.each_item do |page|
186
- # index += 1
187
- # end
188
- # Acfs.run
189
- # print index
190
- # # => 142
191
- #
192
- # @param opts [Hash] Options passed to {#each_page}.
193
- #
194
- # @yield [item] Callback that will be invoked for each item.
195
- # @yieldparam item [self] Resource.
196
- # @yieldparam collection [Acfs::Collection] Collection.
197
- #
198
- def each_item(opts = {})
199
- each_page(opts) do |collection|
200
- collection.each do |item|
201
- yield item, collection
202
- end
203
- end
204
- end
205
-
206
- private
207
-
208
- def find_single(id, opts, &block)
209
- model = Acfs::Util::ResourceDelegator.new new
210
-
211
- opts[:params] ||= {}
212
- opts[:params].merge! id: id unless id.nil?
213
-
214
- model.__callbacks__ << block unless block.nil?
215
-
216
- operation :read, opts do |data|
217
- model.__setobj__ create_resource data, origin: model.__getobj__
218
- model.__invoke__
219
- end
220
-
221
- model
222
- end
223
-
224
- def find_multiple(ids, opts, &block)
225
- ::Acfs::Collection.new(self).tap do |collection|
226
- collection.__callbacks__ << block unless block.nil?
227
-
228
- counter = 0
229
- ids.each_with_index do |id, index|
230
- find_single id, opts do |resource|
231
- collection[index] = resource
232
- if (counter += 1) == ids.size
233
- collection.loaded!
234
- collection.__invoke__
235
- end
236
- end
237
- end
238
- end
239
- end
240
-
241
- def create_resource(data, opts = {})
242
- type = data.delete 'type'
243
- klass = resource_class_lookup(type)
244
- (opts[:origin].is_a?(klass) ? opts[:origin] : klass.new).tap do |m|
245
- m.write_attributes data, opts
246
- m.loaded!
247
- end
248
- end
249
-
250
- def resource_class_lookup(type)
251
- return self if type.nil?
252
- klass = type.camelize.constantize
253
-
254
- unless klass <= self
255
- raise Acfs::ResourceTypeError.new type_name: type, base_class: self
256
- end
257
-
258
- klass
259
- rescue NameError, NoMethodError
260
- raise Acfs::ResourceTypeError.new type_name: type, base_class: self
261
- end
262
- end
263
- end
264
- end
@@ -1,42 +0,0 @@
1
- class Acfs::Resource
2
- # Included by Acfs::Model. Allows to configure the service
3
- # a resource belongs to.
4
- #
5
- module Service
6
- extend ActiveSupport::Concern
7
-
8
- #
9
- module ClassMethods
10
- # @api public
11
- #
12
- # @overload service()
13
- # Return service instance.
14
- #
15
- # @return [Service] Service class instance.
16
- #
17
- # @overload service(klass, options = {})
18
- # Link to service this model belongs to. Connection
19
- # settings like base URL are fetched from service.
20
- # Return assigned service if no arguments are given.
21
- #
22
- # @example
23
- # class AccountService < Acfs::Client
24
- # self.base_url = 'http://acc.serv.org'
25
- # end
26
- #
27
- # class MyUser < Acfs::Resource
28
- # service AccountService
29
- # end
30
- # MyUser.find 5 # Will fetch `http://acc.serv.org/users/5`
31
- #
32
- # @param klass [Class] Service class derived from {Acfs::Service}.
33
- # @param options [Object] Option delegated to
34
- # service class initializer.
35
- #
36
- def service(klass = nil, options = {})
37
- return (@service = klass.new options) if klass
38
- @service || superclass.service
39
- end
40
- end
41
- end
42
- end
@@ -1,37 +0,0 @@
1
- class Acfs::Resource
2
- #
3
- module Validation
4
- def valid?(*args)
5
- super
6
- remote_errors.each {|f, e| errors.add f, e }
7
- errors.empty?
8
- end
9
-
10
- def remote_errors
11
- @remote_errors ||= ActiveModel::Errors.new self
12
- end
13
-
14
- def remote_errors=(errors)
15
- if errors.respond_to?(:each_pair)
16
- errors.each_pair do |field, errs|
17
- Array(errs).each do |err|
18
- self.errors.add field.to_sym, err
19
- remote_errors.add field.to_sym, err
20
- end
21
- end
22
- else
23
- Array(errors).each do |err|
24
- self.errors.add :base, err
25
- remote_errors.add :base, err
26
- end
27
- end
28
- end
29
-
30
- def save!(*_)
31
- unless valid?(new? ? :create : :save)
32
- raise ::Acfs::InvalidResource.new resource: self, errors: errors.to_a
33
- end
34
- super
35
- end
36
- end
37
- end
@@ -1,28 +0,0 @@
1
- require 'acfs/response/formats'
2
- require 'acfs/response/status'
3
- require 'active_support/core_ext/module/delegation'
4
-
5
- module Acfs
6
- # This represents a response. In addition to an standard HTTP
7
- # it has a field `data` for storing the encoded body.
8
- #
9
- class Response
10
- attr_accessor :data
11
- attr_reader :headers, :body, :request, :status
12
-
13
- include Response::Formats
14
- include Response::Status
15
-
16
- # delegate :status, :status_message, :success?, :modified?, :timed_out?,
17
- # :response_body, :response_headers, :response_code, :headers,
18
- # to: :response
19
-
20
- def initialize(request, data = {})
21
- @request = request
22
- @status = data[:status] || 0
23
- @headers = data[:headers] || {}
24
- @body = data[:body] || ''
25
- @data = data[:data] || nil
26
- end
27
- end
28
- end
@@ -1,25 +0,0 @@
1
- require 'action_dispatch'
2
-
3
- module Acfs
4
- class Response
5
- # Quick accessors for format handling.
6
- module Formats
7
- def content_type
8
- @content_type ||= read_content_type
9
- end
10
-
11
- def json?
12
- content_type == Mime[:json]
13
- end
14
-
15
- private
16
-
17
- def read_content_type
18
- return 'text/plain' unless headers && headers['Content-Type']
19
-
20
- content_type = headers['Content-Type'].split(/;\s*\w+="?\w+"?/).first
21
- Mime::Type.parse(content_type).first
22
- end
23
- end
24
- end
25
- end
@@ -1,31 +0,0 @@
1
- module Acfs
2
- class Response
3
- # Method to fetch information about response status.
4
- #
5
- module Status
6
- # Return response status code. Will return zero if
7
- # request was not executed or failed on client side.
8
- #
9
- def status_code
10
- return @status.to_i if defined? :@status
11
- # return response.response_code unless response.nil?
12
- # 0
13
- end
14
- alias_method :code, :status_code
15
-
16
- # Return true if response was successful indicated by
17
- # response status code.
18
- #
19
- def success?
20
- code >= 200 && code < 300
21
- end
22
-
23
- # Return true unless response status code indicates that
24
- # resource was not modified according to send precondition headers.
25
- #
26
- def modified?
27
- code != 304
28
- end
29
- end
30
- end
31
- end
@@ -1,11 +0,0 @@
1
- require 'acfs'
2
-
3
- RSpec.configure do |config|
4
- config.before(:each) do
5
- Acfs::Stub.enable
6
- end
7
-
8
- config.after(:each) do
9
- Acfs.reset
10
- end
11
- end
@@ -1,97 +0,0 @@
1
- require 'acfs/service/middleware'
2
-
3
- module Acfs
4
- # @api private
5
- #
6
- class Runner
7
- include Service::Middleware
8
- attr_reader :adapter
9
-
10
- def initialize(adapter)
11
- @adapter = adapter
12
- @running = false
13
- end
14
-
15
- # Process an operation. Synchronous operations will be run
16
- # and parallel operations will be queued.
17
- #
18
- def process(op)
19
- ::ActiveSupport::Notifications.instrument 'acfs.operation.before_process', operation: op
20
- op.synchronous? ? run(op) : enqueue(op)
21
- end
22
-
23
- # Run operation right now skipping queue.
24
- #
25
- def run(op)
26
- ::ActiveSupport::Notifications.instrument 'acfs.runner.sync_run', operation: op do
27
- op_request(op) {|req| adapter.run req }
28
- end
29
- end
30
-
31
- # List of current queued operations.
32
- #
33
- def queue
34
- @queue ||= []
35
- end
36
-
37
- # Enqueue operation to be run later.
38
- #
39
- def enqueue(op)
40
- ::ActiveSupport::Notifications.instrument 'acfs.runner.enqueue', operation: op do
41
- if running?
42
- op_request(op) {|req| adapter.queue req }
43
- else
44
- queue << op
45
- end
46
- end
47
- end
48
-
49
- # Return true if queued operations are currently processed.
50
- #
51
- def running?
52
- @running
53
- end
54
-
55
- # Start processing queued operations.
56
- #
57
- def start
58
- return if running?
59
-
60
- enqueue_operations
61
- start_all
62
- rescue
63
- queue.clear
64
- raise
65
- end
66
-
67
- def clear
68
- queue.clear
69
- adapter.abort
70
- @running = false
71
- end
72
-
73
- private
74
-
75
- def start_all
76
- @running = true
77
- adapter.start
78
- ensure
79
- @running = false
80
- end
81
-
82
- def enqueue_operations
83
- while (op = queue.shift)
84
- op_request(op) {|req| adapter.queue req }
85
- end
86
- end
87
-
88
- def op_request(op)
89
- return if Acfs::Stub.enabled? && Acfs::Stub.stubbed(op)
90
- req = op.service.prepare op.request
91
- return unless req.is_a? Acfs::Request
92
- req = prepare req
93
- return unless req.is_a? Acfs::Request
94
- yield req
95
- end
96
- end
97
- end