acfs 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- metadata +8 -141
- data/CHANGELOG.md +0 -325
- data/LICENSE +0 -22
- data/README.md +0 -334
- data/acfs.gemspec +0 -37
- data/lib/acfs.rb +0 -49
- data/lib/acfs/adapter/base.rb +0 -26
- data/lib/acfs/adapter/typhoeus.rb +0 -60
- data/lib/acfs/collection.rb +0 -27
- data/lib/acfs/collections/paginatable.rb +0 -75
- data/lib/acfs/configuration.rb +0 -113
- data/lib/acfs/errors.rb +0 -106
- data/lib/acfs/global.rb +0 -99
- data/lib/acfs/location.rb +0 -78
- data/lib/acfs/middleware/base.rb +0 -20
- data/lib/acfs/middleware/json.rb +0 -27
- data/lib/acfs/middleware/logger.rb +0 -23
- data/lib/acfs/middleware/msgpack.rb +0 -30
- data/lib/acfs/middleware/print.rb +0 -21
- data/lib/acfs/middleware/serializer.rb +0 -39
- data/lib/acfs/operation.rb +0 -81
- data/lib/acfs/request.rb +0 -36
- data/lib/acfs/request/callbacks.rb +0 -50
- data/lib/acfs/resource.rb +0 -37
- data/lib/acfs/resource/attributes.rb +0 -268
- data/lib/acfs/resource/attributes/base.rb +0 -28
- data/lib/acfs/resource/attributes/boolean.rb +0 -37
- data/lib/acfs/resource/attributes/date_time.rb +0 -31
- data/lib/acfs/resource/attributes/dict.rb +0 -37
- data/lib/acfs/resource/attributes/float.rb +0 -31
- data/lib/acfs/resource/attributes/integer.rb +0 -27
- data/lib/acfs/resource/attributes/list.rb +0 -34
- data/lib/acfs/resource/attributes/string.rb +0 -24
- data/lib/acfs/resource/attributes/uuid.rb +0 -47
- data/lib/acfs/resource/dirty.rb +0 -35
- data/lib/acfs/resource/initialization.rb +0 -29
- data/lib/acfs/resource/loadable.rb +0 -33
- data/lib/acfs/resource/locatable.rb +0 -128
- data/lib/acfs/resource/operational.rb +0 -22
- data/lib/acfs/resource/persistence.rb +0 -257
- data/lib/acfs/resource/query_methods.rb +0 -264
- data/lib/acfs/resource/service.rb +0 -42
- data/lib/acfs/resource/validation.rb +0 -37
- data/lib/acfs/response.rb +0 -28
- data/lib/acfs/response/formats.rb +0 -25
- data/lib/acfs/response/status.rb +0 -31
- data/lib/acfs/rspec.rb +0 -11
- data/lib/acfs/runner.rb +0 -97
- data/lib/acfs/service.rb +0 -91
- data/lib/acfs/service/middleware.rb +0 -56
- data/lib/acfs/service/middleware/stack.rb +0 -63
- data/lib/acfs/singleton_resource.rb +0 -83
- data/lib/acfs/stub.rb +0 -172
- data/lib/acfs/util.rb +0 -20
- data/lib/acfs/version.rb +0 -14
- data/lib/acfs/yard.rb +0 -5
- data/spec/acfs/adapter/typhoeus_spec.rb +0 -28
- data/spec/acfs/collection_spec.rb +0 -155
- data/spec/acfs/configuration_spec.rb +0 -51
- data/spec/acfs/global_spec.rb +0 -137
- data/spec/acfs/location_spec.rb +0 -23
- data/spec/acfs/middleware/json_spec.rb +0 -63
- data/spec/acfs/middleware/msgpack_spec.rb +0 -60
- data/spec/acfs/operation_spec.rb +0 -10
- data/spec/acfs/request/callbacks_spec.rb +0 -46
- data/spec/acfs/request_spec.rb +0 -77
- data/spec/acfs/resource/attributes/boolean_spec.rb +0 -56
- data/spec/acfs/resource/attributes/date_time_spec.rb +0 -49
- data/spec/acfs/resource/attributes/dict_spec.rb +0 -75
- data/spec/acfs/resource/attributes/float_spec.rb +0 -59
- data/spec/acfs/resource/attributes/integer_spec.rb +0 -34
- data/spec/acfs/resource/attributes/list_spec.rb +0 -58
- data/spec/acfs/resource/attributes/uuid_spec.rb +0 -40
- data/spec/acfs/resource/attributes_spec.rb +0 -179
- data/spec/acfs/resource/dirty_spec.rb +0 -47
- data/spec/acfs/resource/initialization_spec.rb +0 -30
- data/spec/acfs/resource/loadable_spec.rb +0 -20
- data/spec/acfs/resource/locatable_spec.rb +0 -116
- data/spec/acfs/resource/persistance_spec.rb +0 -316
- data/spec/acfs/resource/query_methods_spec.rb +0 -541
- data/spec/acfs/resource/validation_spec.rb +0 -127
- data/spec/acfs/response/formats_spec.rb +0 -50
- data/spec/acfs/response/status_spec.rb +0 -69
- data/spec/acfs/runner_spec.rb +0 -97
- data/spec/acfs/service/middleware_spec.rb +0 -33
- data/spec/acfs/service_spec.rb +0 -46
- data/spec/acfs/singleton_resource_spec.rb +0 -15
- data/spec/acfs/stub_spec.rb +0 -343
- data/spec/acfs_spec.rb +0 -203
- data/spec/fixtures/config.yml +0 -14
- data/spec/spec_helper.rb +0 -41
- data/spec/support/hash.rb +0 -9
- data/spec/support/response.rb +0 -10
- data/spec/support/service.rb +0 -91
- data/spec/support/shared/find_callbacks.rb +0 -48
data/lib/acfs/location.rb
DELETED
@@ -1,78 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
# @api private
|
3
|
-
#
|
4
|
-
# Describes a URL with placeholders.
|
5
|
-
#
|
6
|
-
class Location
|
7
|
-
attr_reader :arguments, :raw, :struct, :args
|
8
|
-
|
9
|
-
REGEXP = /^:([A-z][A-z0-9_]*)$/
|
10
|
-
|
11
|
-
def initialize(uri, args = {})
|
12
|
-
@raw = URI.parse uri
|
13
|
-
@args = args
|
14
|
-
@struct = raw.path.split('/').reject(&:empty?).map {|s| s =~ REGEXP ? Regexp.last_match[1].to_sym : s }
|
15
|
-
@arguments = struct.select {|s| s.is_a?(Symbol) }
|
16
|
-
end
|
17
|
-
|
18
|
-
def build(args = {})
|
19
|
-
unless args.is_a?(Hash)
|
20
|
-
raise ArgumentError.new "URI path arguments must be a hash, `#{args.inspect}' given."
|
21
|
-
end
|
22
|
-
|
23
|
-
self.class.new raw.to_s, args.merge(self.args)
|
24
|
-
end
|
25
|
-
|
26
|
-
def extract_from(*args)
|
27
|
-
args = {}.tap do |collect|
|
28
|
-
arguments.each {|key| collect[key] = extract_arg key, args }
|
29
|
-
end
|
30
|
-
|
31
|
-
build args
|
32
|
-
end
|
33
|
-
|
34
|
-
def str
|
35
|
-
uri = raw.dup
|
36
|
-
uri.path = '/' + struct.map {|s| lookup_arg(s, args) }.join('/')
|
37
|
-
uri.to_s
|
38
|
-
end
|
39
|
-
|
40
|
-
def raw_uri
|
41
|
-
raw.to_s
|
42
|
-
end
|
43
|
-
alias_method :to_s, :raw_uri
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def extract_arg(key, hashes)
|
48
|
-
hashes.each_with_index do |hash, index|
|
49
|
-
return (index == 0 ? hash.delete(key) : hash.fetch(key)) if hash.key?(key)
|
50
|
-
end
|
51
|
-
|
52
|
-
nil
|
53
|
-
end
|
54
|
-
|
55
|
-
def lookup_arg(arg, args)
|
56
|
-
arg.is_a?(Symbol) ? lookup_replacement(arg, args) : arg
|
57
|
-
end
|
58
|
-
|
59
|
-
def lookup_replacement(sym, args)
|
60
|
-
value = get_replacement(sym, args).to_s
|
61
|
-
return ::URI.encode_www_form_component(value) unless value.empty?
|
62
|
-
|
63
|
-
raise ArgumentError.new "Cannot replace path argument `#{sym}' with empty string."
|
64
|
-
end
|
65
|
-
|
66
|
-
def get_replacement(sym, args)
|
67
|
-
args.fetch(sym.to_s) do
|
68
|
-
args.fetch(sym) do
|
69
|
-
if args[:raise].nil? || args[:raise]
|
70
|
-
raise ArgumentError.new "URI path argument `#{sym}' missing on `#{self}'. Given: `#{args}.inspect'"
|
71
|
-
else
|
72
|
-
":#{sym}"
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
data/lib/acfs/middleware/base.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
module Middleware
|
3
|
-
# A base middleware that does not modify request or response.
|
4
|
-
# Can be used as super class for custom middleware implementations.
|
5
|
-
#
|
6
|
-
class Base
|
7
|
-
attr_reader :app, :options
|
8
|
-
|
9
|
-
def initialize(app, options = {})
|
10
|
-
@app = app
|
11
|
-
@options = options
|
12
|
-
end
|
13
|
-
|
14
|
-
def call(request)
|
15
|
-
request.on_complete {|res, nxt| response(res, nxt) } if respond_to? :response
|
16
|
-
app.call(request)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/lib/acfs/middleware/json.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'multi_json'
|
2
|
-
|
3
|
-
module Acfs
|
4
|
-
module Middleware
|
5
|
-
# A middleware to encore request data using JSON.
|
6
|
-
#
|
7
|
-
class JSON < Serializer
|
8
|
-
def mime
|
9
|
-
::Mime[:json]
|
10
|
-
end
|
11
|
-
|
12
|
-
def encode(data)
|
13
|
-
::MultiJson.dump data
|
14
|
-
end
|
15
|
-
|
16
|
-
def decode(body)
|
17
|
-
::MultiJson.load body
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# @deprecated
|
22
|
-
JsonDecoder = JSON
|
23
|
-
|
24
|
-
# @deprecated
|
25
|
-
JsonEncoder = JSON
|
26
|
-
end
|
27
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'logger'
|
2
|
-
|
3
|
-
module Acfs
|
4
|
-
module Middleware
|
5
|
-
# Log requests and responses.
|
6
|
-
#
|
7
|
-
class Logger < Base
|
8
|
-
def initialize(app, options = {})
|
9
|
-
super
|
10
|
-
@logger = options[:logger] if options[:logger]
|
11
|
-
end
|
12
|
-
|
13
|
-
def response(res, nxt)
|
14
|
-
logger.info "[ACFS] #{res.request.method.to_s.upcase} #{res.request.url} -> #{res.status}"
|
15
|
-
nxt.call res
|
16
|
-
end
|
17
|
-
|
18
|
-
def logger
|
19
|
-
@logger ||= ::Logger.new STDOUT
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'msgpack'
|
2
|
-
require 'action_dispatch'
|
3
|
-
|
4
|
-
module Acfs
|
5
|
-
module Middleware
|
6
|
-
class MessagePack < Serializer
|
7
|
-
unless defined?(::Mime::MSGPACK)
|
8
|
-
::Mime::Type.register 'application/x-msgpack', :msgpack
|
9
|
-
end
|
10
|
-
|
11
|
-
def mime
|
12
|
-
::Mime[:msgpack]
|
13
|
-
end
|
14
|
-
|
15
|
-
def encode(data)
|
16
|
-
::MessagePack.pack data
|
17
|
-
end
|
18
|
-
|
19
|
-
def decode(body)
|
20
|
-
::MessagePack.unpack body
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
# @deprecated
|
25
|
-
MessagePackEncoder = MessagePack
|
26
|
-
|
27
|
-
# @deprecated
|
28
|
-
MessagePackDecoder = MessagePack
|
29
|
-
end
|
30
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
module Middleware
|
3
|
-
# Print resquests and response on terminal
|
4
|
-
#
|
5
|
-
class Print < Base
|
6
|
-
def call(req)
|
7
|
-
puts '-' * 80
|
8
|
-
puts req.inspect
|
9
|
-
puts '-' * 80
|
10
|
-
|
11
|
-
super
|
12
|
-
end
|
13
|
-
|
14
|
-
def response(res)
|
15
|
-
puts '-' * 80
|
16
|
-
puts res.inspect
|
17
|
-
puts '-' * 80
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
module Middleware
|
3
|
-
# A base middleware that does not modify request or response.
|
4
|
-
# Can be used as super class for custom middleware implementations.
|
5
|
-
#
|
6
|
-
class Serializer < Base
|
7
|
-
def encode(_data)
|
8
|
-
raise NotImplementedError
|
9
|
-
end
|
10
|
-
|
11
|
-
def decode(_data)
|
12
|
-
raise NotImplementedError
|
13
|
-
end
|
14
|
-
|
15
|
-
def mime
|
16
|
-
raise NotImplementedError
|
17
|
-
end
|
18
|
-
|
19
|
-
def call(request)
|
20
|
-
unless request.headers['Content-Type']
|
21
|
-
request.body = encode request.data
|
22
|
-
request.headers['Content-Type'] = mime
|
23
|
-
end
|
24
|
-
|
25
|
-
accept = request.headers['Accept'].to_s.split(',')
|
26
|
-
accept << "#{mime};q=#{options.fetch(:q, 1)}"
|
27
|
-
request.headers['Accept'] = accept.join(',')
|
28
|
-
|
29
|
-
request.on_complete do |response, nxt|
|
30
|
-
response.data = decode response.body if mime == response.content_type
|
31
|
-
|
32
|
-
nxt.call response
|
33
|
-
end
|
34
|
-
|
35
|
-
app.call(request)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
data/lib/acfs/operation.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
# @api private
|
3
|
-
#
|
4
|
-
# Describes a CRUD operation. Handle request creation and response
|
5
|
-
# processing as well as error handling and stubbing.
|
6
|
-
#
|
7
|
-
class Operation
|
8
|
-
attr_reader :action, :params, :resource, :data, :callback, :location, :url
|
9
|
-
delegate :service, to: :resource
|
10
|
-
delegate :call, to: :callback
|
11
|
-
|
12
|
-
def initialize(resource, action, opts = {}, &block)
|
13
|
-
@resource = resource
|
14
|
-
@action = action.to_sym
|
15
|
-
|
16
|
-
# Operations can be delayed so dup params and data to avoid
|
17
|
-
# later in-place changes by modifying passed hash
|
18
|
-
@params = (opts[:params] || {}).dup
|
19
|
-
@data = (opts[:data] || {}).dup
|
20
|
-
|
21
|
-
if opts[:url]
|
22
|
-
@url = opts[:url]
|
23
|
-
else
|
24
|
-
@location = resource.location(action: @action).extract_from(@params, @data)
|
25
|
-
@url = location.str
|
26
|
-
end
|
27
|
-
|
28
|
-
@callback = block
|
29
|
-
end
|
30
|
-
|
31
|
-
def single?
|
32
|
-
[:read, :update, :delete].include? action
|
33
|
-
end
|
34
|
-
|
35
|
-
def synchronous?
|
36
|
-
[:update, :delete, :create].include? action
|
37
|
-
end
|
38
|
-
|
39
|
-
def id
|
40
|
-
# TODO
|
41
|
-
@id ||= params.delete(:id) || data[:id]
|
42
|
-
end
|
43
|
-
|
44
|
-
def full_params
|
45
|
-
(id ? params.merge(id: id) : params).merge location_args
|
46
|
-
end
|
47
|
-
|
48
|
-
def location_args
|
49
|
-
location ? location.args : {}
|
50
|
-
end
|
51
|
-
|
52
|
-
def method
|
53
|
-
{read: :get, list: :get, update: :put, create: :post, delete: :delete}[action]
|
54
|
-
end
|
55
|
-
|
56
|
-
def request
|
57
|
-
request = ::Acfs::Request.new url, method: method, params: params,
|
58
|
-
data: data, operation: self
|
59
|
-
request.on_complete do |response|
|
60
|
-
::ActiveSupport::Notifications.instrument 'acfs.operation.complete',
|
61
|
-
operation: self,
|
62
|
-
response: response
|
63
|
-
|
64
|
-
handle_failure response unless response.success?
|
65
|
-
callback.call response.data, response
|
66
|
-
end
|
67
|
-
request
|
68
|
-
end
|
69
|
-
|
70
|
-
def handle_failure(response)
|
71
|
-
case response.code
|
72
|
-
when 404
|
73
|
-
raise ::Acfs::ResourceNotFound.new response: response
|
74
|
-
when 422
|
75
|
-
raise ::Acfs::InvalidResource.new response: response, errors: response.data.try(:[], 'errors')
|
76
|
-
else
|
77
|
-
raise ::Acfs::ErroneousResponse.new response: response
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
data/lib/acfs/request.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require 'acfs/request/callbacks'
|
2
|
-
|
3
|
-
module Acfs
|
4
|
-
# Encapsulate all data required to make up a request to the
|
5
|
-
# underlaying http library.
|
6
|
-
#
|
7
|
-
class Request
|
8
|
-
attr_accessor :body, :format
|
9
|
-
attr_reader :url, :headers, :params, :data, :method, :operation
|
10
|
-
|
11
|
-
include Request::Callbacks
|
12
|
-
|
13
|
-
def initialize(url, options = {}, &block)
|
14
|
-
@url = URI.parse(url.to_s).tap do |_url|
|
15
|
-
@data = options.delete(:data) || nil
|
16
|
-
@format = options.delete(:format) || :json
|
17
|
-
@headers = options.delete(:headers) || {}
|
18
|
-
@params = options.delete(:params) || {}
|
19
|
-
@method = options.delete(:method) || :get
|
20
|
-
end.to_s
|
21
|
-
@operation = options.delete(:operation) || nil
|
22
|
-
on_complete(&block) if block_given?
|
23
|
-
end
|
24
|
-
|
25
|
-
def data?
|
26
|
-
!data.nil?
|
27
|
-
end
|
28
|
-
|
29
|
-
class << self
|
30
|
-
def new(*attrs)
|
31
|
-
return attrs[0] if attrs[0].is_a? self
|
32
|
-
super
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module Acfs
|
2
|
-
class Request
|
3
|
-
# Module containing callback handling for Requests.
|
4
|
-
# Current the only callback type is `on_complete`:
|
5
|
-
#
|
6
|
-
# request = Request.new 'URL'
|
7
|
-
# request.on_complete { |response| ... }
|
8
|
-
#
|
9
|
-
module Callbacks
|
10
|
-
# Add a new `on_complete` callback for this request.
|
11
|
-
#
|
12
|
-
# @example Set on_complete.
|
13
|
-
# request.on_complete { |response| print response.body }
|
14
|
-
#
|
15
|
-
# @param [ Block ] block The callback block to execute.
|
16
|
-
#
|
17
|
-
# @yield [ Acfs::Response ]
|
18
|
-
#
|
19
|
-
# @return [ Acfs::Request ] The request itself.
|
20
|
-
#
|
21
|
-
def on_complete(&block)
|
22
|
-
callbacks.insert 0, block if block_given?
|
23
|
-
self
|
24
|
-
end
|
25
|
-
|
26
|
-
# Return array of all callbacks.
|
27
|
-
#
|
28
|
-
# @return [ Array<Block> ] All callbacks.
|
29
|
-
#
|
30
|
-
def callbacks
|
31
|
-
@callbacks ||= []
|
32
|
-
end
|
33
|
-
|
34
|
-
# Trigger all callback for given response.
|
35
|
-
#
|
36
|
-
# @return [ Acfs::Request ] The request itself.
|
37
|
-
#
|
38
|
-
def complete!(response)
|
39
|
-
call_callback response, 0
|
40
|
-
self
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def call_callback(res, index)
|
46
|
-
callbacks[index].call res, proc {|bres| call_callback bres, index + 1 } if index < callbacks.size
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
data/lib/acfs/resource.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'active_model'
|
2
|
-
|
3
|
-
# @api public
|
4
|
-
#
|
5
|
-
class Acfs::Resource
|
6
|
-
require 'acfs/resource/initialization'
|
7
|
-
require 'acfs/resource/attributes'
|
8
|
-
require 'acfs/resource/dirty'
|
9
|
-
require 'acfs/resource/loadable'
|
10
|
-
require 'acfs/resource/locatable'
|
11
|
-
require 'acfs/resource/operational'
|
12
|
-
require 'acfs/resource/persistence'
|
13
|
-
require 'acfs/resource/query_methods'
|
14
|
-
require 'acfs/resource/service'
|
15
|
-
require 'acfs/resource/validation'
|
16
|
-
|
17
|
-
if ActiveModel::VERSION::MAJOR >= 4
|
18
|
-
include ActiveModel::Model
|
19
|
-
else
|
20
|
-
extend ActiveModel::Naming
|
21
|
-
extend ActiveModel::Translation
|
22
|
-
include ActiveModel::Conversion
|
23
|
-
include ActiveModel::Validations
|
24
|
-
end
|
25
|
-
|
26
|
-
include Initialization
|
27
|
-
|
28
|
-
include Attributes
|
29
|
-
include Loadable
|
30
|
-
include Persistence
|
31
|
-
include Locatable
|
32
|
-
include Operational
|
33
|
-
include QueryMethods
|
34
|
-
include Service
|
35
|
-
include Dirty
|
36
|
-
include Validation
|
37
|
-
end
|