acfs 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -0
  3. data/README.md +3 -4
  4. data/acfs.gemspec +19 -10
  5. data/lib/acfs.rb +2 -0
  6. data/lib/acfs/adapter/base.rb +6 -8
  7. data/lib/acfs/adapter/typhoeus.rb +26 -7
  8. data/lib/acfs/collection.rb +2 -1
  9. data/lib/acfs/collections/paginatable.rb +2 -1
  10. data/lib/acfs/configuration.rb +14 -7
  11. data/lib/acfs/errors.rb +33 -12
  12. data/lib/acfs/global.rb +12 -2
  13. data/lib/acfs/location.rb +9 -5
  14. data/lib/acfs/middleware/base.rb +5 -1
  15. data/lib/acfs/middleware/json.rb +2 -0
  16. data/lib/acfs/middleware/logger.rb +2 -0
  17. data/lib/acfs/middleware/msgpack.rb +2 -0
  18. data/lib/acfs/middleware/print.rb +2 -0
  19. data/lib/acfs/middleware/serializer.rb +2 -0
  20. data/lib/acfs/operation.rb +5 -3
  21. data/lib/acfs/request.rb +3 -0
  22. data/lib/acfs/request/callbacks.rb +5 -1
  23. data/lib/acfs/resource.rb +2 -0
  24. data/lib/acfs/resource/attributes.rb +3 -2
  25. data/lib/acfs/resource/attributes/base.rb +2 -1
  26. data/lib/acfs/resource/attributes/boolean.rb +2 -0
  27. data/lib/acfs/resource/attributes/date_time.rb +2 -1
  28. data/lib/acfs/resource/attributes/dict.rb +2 -0
  29. data/lib/acfs/resource/attributes/float.rb +5 -3
  30. data/lib/acfs/resource/attributes/integer.rb +2 -0
  31. data/lib/acfs/resource/attributes/list.rb +2 -0
  32. data/lib/acfs/resource/attributes/string.rb +2 -0
  33. data/lib/acfs/resource/attributes/uuid.rb +4 -3
  34. data/lib/acfs/resource/dirty.rb +2 -0
  35. data/lib/acfs/resource/initialization.rb +2 -0
  36. data/lib/acfs/resource/loadable.rb +2 -0
  37. data/lib/acfs/resource/locatable.rb +10 -6
  38. data/lib/acfs/resource/operational.rb +2 -1
  39. data/lib/acfs/resource/persistence.rb +7 -4
  40. data/lib/acfs/resource/query_methods.rb +5 -3
  41. data/lib/acfs/resource/service.rb +3 -1
  42. data/lib/acfs/resource/validation.rb +3 -1
  43. data/lib/acfs/response.rb +2 -0
  44. data/lib/acfs/response/formats.rb +2 -0
  45. data/lib/acfs/response/status.rb +3 -1
  46. data/lib/acfs/rspec.rb +2 -0
  47. data/lib/acfs/runner.rb +6 -1
  48. data/lib/acfs/service.rb +8 -2
  49. data/lib/acfs/service/middleware.rb +2 -0
  50. data/lib/acfs/service/middleware/stack.rb +5 -3
  51. data/lib/acfs/singleton_resource.rb +4 -2
  52. data/lib/acfs/stub.rb +33 -11
  53. data/lib/acfs/util.rb +2 -0
  54. data/lib/acfs/version.rb +3 -1
  55. data/lib/acfs/yard.rb +1 -0
  56. data/spec/acfs/adapter/typhoeus_spec.rb +30 -3
  57. data/spec/acfs/collection_spec.rb +7 -5
  58. data/spec/acfs/configuration_spec.rb +2 -0
  59. data/spec/acfs/global_spec.rb +48 -1
  60. data/spec/acfs/location_spec.rb +25 -0
  61. data/spec/acfs/middleware/json_spec.rb +2 -0
  62. data/spec/acfs/middleware/msgpack_spec.rb +2 -0
  63. data/spec/acfs/operation_spec.rb +2 -0
  64. data/spec/acfs/request/callbacks_spec.rb +2 -0
  65. data/spec/acfs/request_spec.rb +3 -1
  66. data/spec/acfs/resource/attributes/boolean_spec.rb +2 -0
  67. data/spec/acfs/resource/attributes/date_time_spec.rb +2 -0
  68. data/spec/acfs/resource/attributes/dict_spec.rb +4 -2
  69. data/spec/acfs/resource/attributes/float_spec.rb +2 -0
  70. data/spec/acfs/resource/attributes/integer_spec.rb +2 -0
  71. data/spec/acfs/resource/attributes/list_spec.rb +5 -3
  72. data/spec/acfs/resource/attributes/uuid_spec.rb +2 -0
  73. data/spec/acfs/resource/attributes_spec.rb +6 -4
  74. data/spec/acfs/resource/dirty_spec.rb +2 -0
  75. data/spec/acfs/resource/initialization_spec.rb +8 -2
  76. data/spec/acfs/resource/loadable_spec.rb +2 -0
  77. data/spec/acfs/resource/locatable_spec.rb +2 -0
  78. data/spec/acfs/resource/persistance_spec.rb +10 -4
  79. data/spec/acfs/resource/query_methods_spec.rb +25 -18
  80. data/spec/acfs/resource/validation_spec.rb +2 -0
  81. data/spec/acfs/response/formats_spec.rb +3 -1
  82. data/spec/acfs/response/status_spec.rb +2 -0
  83. data/spec/acfs/runner_spec.rb +6 -8
  84. data/spec/acfs/service/middleware_spec.rb +2 -0
  85. data/spec/acfs/service_spec.rb +3 -1
  86. data/spec/acfs/singleton_resource_spec.rb +2 -0
  87. data/spec/acfs/stub_spec.rb +2 -0
  88. data/spec/acfs_spec.rb +2 -0
  89. data/spec/spec_helper.rb +11 -6
  90. data/spec/support/hash.rb +2 -0
  91. data/spec/support/response.rb +2 -0
  92. data/spec/support/service.rb +1 -0
  93. data/spec/support/shared/find_callbacks.rb +2 -0
  94. metadata +12 -11
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs
2
4
  module Middleware
3
5
  # A base middleware that does not modify request or response.
@@ -12,7 +14,9 @@ module Acfs
12
14
  end
13
15
 
14
16
  def call(request)
15
- request.on_complete {|res, nxt| response(res, nxt) } if respond_to? :response
17
+ if respond_to? :response
18
+ request.on_complete {|res, nxt| response(res, nxt) }
19
+ end
16
20
  app.call(request)
17
21
  end
18
22
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'multi_json'
2
4
 
3
5
  module Acfs
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'logger'
2
4
 
3
5
  module Acfs
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'msgpack'
2
4
  require 'action_dispatch'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs
2
4
  module Middleware
3
5
  # Print resquests and response on terminal
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs
2
4
  module Middleware
3
5
  # A base middleware that does not modify request or response.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs
2
4
  # @api private
3
5
  #
@@ -19,7 +21,7 @@ module Acfs
19
21
  @data = (opts[:data] || {}).dup
20
22
 
21
23
  if opts[:url]
22
- @url = opts[:url]
24
+ @url = opts[:url]
23
25
  else
24
26
  @location = resource.location(action: @action).extract_from(@params, @data)
25
27
  @url = location.str
@@ -29,11 +31,11 @@ module Acfs
29
31
  end
30
32
 
31
33
  def single?
32
- [:read, :update, :delete].include? action
34
+ %i[read update delete].include? action
33
35
  end
34
36
 
35
37
  def synchronous?
36
- [:update, :delete, :create].include? action
38
+ %i[update delete create].include? action
37
39
  end
38
40
 
39
41
  def id
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'acfs/request/callbacks'
2
4
 
3
5
  module Acfs
@@ -29,6 +31,7 @@ module Acfs
29
31
  class << self
30
32
  def new(*attrs)
31
33
  return attrs[0] if attrs[0].is_a? self
34
+
32
35
  super
33
36
  end
34
37
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs
2
4
  class Request
3
5
  # Module containing callback handling for Requests.
@@ -43,7 +45,9 @@ module Acfs
43
45
  private
44
46
 
45
47
  def call_callback(res, index)
46
- callbacks[index].call res, proc {|bres| call_callback bres, index + 1 } if index < callbacks.size
48
+ if index < callbacks.size
49
+ callbacks[index].call res, proc {|bres| call_callback bres, index + 1 }
50
+ end
47
51
  end
48
52
  end
49
53
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model'
2
4
 
3
5
  # @api public
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  #
3
5
  # = Acfs Attributes
@@ -176,9 +178,8 @@ class Acfs::Resource
176
178
  attributes[name.to_s] = value
177
179
  end
178
180
 
179
- #
180
181
  module ClassMethods
181
- ATTR_CLASS_BASE = '::Acfs::Resource::Attributes'.freeze
182
+ ATTR_CLASS_BASE = '::Acfs::Resource::Attributes'
182
183
 
183
184
  #
184
185
  # @api public
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
- #
3
4
  class Base
4
5
  attr_reader :default
5
6
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -10,7 +12,6 @@ module Acfs::Resource::Attributes
10
12
  # end
11
13
  #
12
14
  class DateTime < Base
13
-
14
15
  # @api public
15
16
  #
16
17
  # Cast given object to DateTime.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -21,9 +23,9 @@ module Acfs::Resource::Attributes
21
23
 
22
24
  case value
23
25
  when ::Float then value
24
- when "Infinity" then ::Float::INFINITY
25
- when "-Infinity" then -::Float::INFINITY
26
- when "NaN" then ::Float::NAN
26
+ when 'Infinity' then ::Float::INFINITY
27
+ when '-Infinity' then -::Float::INFINITY
28
+ when 'NaN' then ::Float::NAN
27
29
  else Float(value)
28
30
  end
29
31
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Acfs::Resource::Attributes
2
4
  # @api public
3
5
  #
@@ -9,8 +11,7 @@ module Acfs::Resource::Attributes
9
11
  # end
10
12
  #
11
13
  class UUID < Base
12
- #
13
- UUID_REGEXP = /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i
14
+ UUID_REGEXP = /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i.freeze
14
15
 
15
16
  # @api public
16
17
  #
@@ -37,7 +38,7 @@ module Acfs::Resource::Attributes
37
38
  elsif value.to_s =~ UUID_REGEXP
38
39
  value
39
40
  else
40
- raise TypeError.new "Invalid UUID: `#{value.to_s}'"
41
+ raise TypeError.new "Invalid UUID: `#{value}'"
41
42
  end
42
43
  end
43
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  #
3
5
  # Thin wrapper around ActiveModel::Dirty
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  #
3
5
  # Initialization drop-in for pre-4.0 ActiveModel.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  # Provides method to check for loading state of resources.
3
5
  # A resource that is created but not yet fetched will be loaded
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  # Provide methods for generation URLs for resources.
3
5
  #
@@ -11,7 +13,6 @@ class Acfs::Resource
11
13
  module Locatable
12
14
  extend ActiveSupport::Concern
13
15
 
14
- #
15
16
  module ClassMethods
16
17
  # @overload url(suffix)
17
18
  # @deprecated
@@ -40,7 +41,10 @@ class Acfs::Resource
40
41
  # @return [String] Generated URL.
41
42
  #
42
43
  def url(suffix = nil, opts = {})
43
- opts, suffix = suffix, nil if suffix.is_a? Hash
44
+ if suffix.is_a? Hash
45
+ opts = suffix
46
+ suffix = nil
47
+ end
44
48
 
45
49
  opts[:action] = :list if suffix
46
50
 
@@ -85,9 +89,9 @@ class Acfs::Resource
85
89
  def location_default_path(action, path)
86
90
  case action
87
91
  when :list, :create
88
- return path
92
+ path
89
93
  when :read, :update, :delete
90
- return "#{path}/:id"
94
+ "#{path}/:id"
91
95
  end
92
96
  end
93
97
  end
@@ -109,8 +113,8 @@ class Acfs::Resource
109
113
  return nil if need_primary_key? && !primary_key?
110
114
 
111
115
  self.class.service
112
- .location(self.class, opts.reverse_merge(action: :read))
113
- .build(attributes).str
116
+ .location(self.class, opts.reverse_merge(action: :read))
117
+ .build(attributes).str
114
118
  end
115
119
 
116
120
  # @api private
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  # @api private
3
5
  #
@@ -11,7 +13,6 @@ class Acfs::Resource
11
13
  extend ActiveSupport::Concern
12
14
  delegate :operation, to: :'self.class'
13
15
 
14
- #
15
16
  module ClassMethods
16
17
  # Invoke CRUD operation.
17
18
  def operation(action, opts = {}, &block)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  #
3
5
  # Allow to track the persistence state of a model.
@@ -40,7 +42,7 @@ class Acfs::Resource
40
42
  def new?
41
43
  !loaded?
42
44
  end
43
- alias_method :new_record?, :new?
45
+ alias new_record? new?
44
46
 
45
47
  # @api public
46
48
  #
@@ -86,9 +88,9 @@ class Acfs::Resource
86
88
  operation((new? ? :create : :update), opts) do |data|
87
89
  update_with data
88
90
  end
89
- rescue ::Acfs::InvalidResource => err
90
- self.remote_errors = err.errors
91
- raise err
91
+ rescue ::Acfs::InvalidResource => e
92
+ self.remote_errors = e.errors
93
+ raise e
92
94
  end
93
95
 
94
96
  # @api public
@@ -251,6 +253,7 @@ class Acfs::Resource
251
253
 
252
254
  def check_loaded!(opts = {})
253
255
  return if loaded? || opts[:force]
256
+
254
257
  raise ::Acfs::ResourceNotLoaded.new resource: self
255
258
  end
256
259
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  # Methods providing the query interface for finding resouces.
3
5
  #
@@ -14,7 +16,6 @@ class Acfs::Resource
14
16
  module QueryMethods
15
17
  extend ActiveSupport::Concern
16
18
 
17
- #
18
19
  module ClassMethods
19
20
  # @api public
20
21
  #
@@ -89,7 +90,7 @@ class Acfs::Resource
89
90
 
90
91
  collection
91
92
  end
92
- alias_method :where, :all
93
+ alias where all
93
94
 
94
95
  # @api public
95
96
  #
@@ -140,7 +141,7 @@ class Acfs::Resource
140
141
  raise Acfs::ResourceNotFound.new message: 'Received erroneous ' \
141
142
  "response: no `#{name}` with params #{params} found"
142
143
  end
143
- block.call m unless block.nil?
144
+ block&.call m
144
145
  end
145
146
  end
146
147
 
@@ -249,6 +250,7 @@ class Acfs::Resource
249
250
 
250
251
  def resource_class_lookup(type)
251
252
  return self if type.nil?
253
+
252
254
  klass = type.camelize.constantize
253
255
 
254
256
  unless klass <= self
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
4
  # Included by Acfs::Model. Allows to configure the service
3
5
  # a resource belongs to.
@@ -5,7 +7,6 @@ class Acfs::Resource
5
7
  module Service
6
8
  extend ActiveSupport::Concern
7
9
 
8
- #
9
10
  module ClassMethods
10
11
  # @api public
11
12
  #
@@ -35,6 +36,7 @@ class Acfs::Resource
35
36
  #
36
37
  def service(klass = nil, options = {})
37
38
  return (@service = klass.new options) if klass
39
+
38
40
  @service || superclass.service
39
41
  end
40
42
  end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Acfs::Resource
2
- #
3
4
  module Validation
4
5
  def valid?(*args)
5
6
  super
@@ -31,6 +32,7 @@ class Acfs::Resource
31
32
  unless valid?(new? ? :create : :save)
32
33
  raise ::Acfs::InvalidResource.new resource: self, errors: errors.to_a
33
34
  end
35
+
34
36
  super
35
37
  end
36
38
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'acfs/response/formats'
2
4
  require 'acfs/response/status'
3
5
  require 'active_support/core_ext/module/delegation'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'action_dispatch'
2
4
 
3
5
  module Acfs