Class: Acfs::Stub

Inherits:
Object
  • Object
show all
Defined in:
lib/acfs/stub.rb

Overview

Global handler for stubbing resources.

Constant Summary collapse

ACTIONS =
%i[read create update delete list].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Stub

Returns a new instance of Stub.



13
14
15
16
17
18
19
20
21
22
# File 'lib/acfs/stub.rb', line 13

def initialize(opts)
  @opts = opts

  @opts[:with].stringify_keys! if @opts[:with].is_a? Hash
  @opts[:return].stringify_keys! if @opts[:return].is_a? Hash

  if @opts[:return].is_a?(Array) # rubocop:disable Style/GuardClause
    @opts[:return].map! {|h| h.stringify_keys! if h.is_a? Hash }
  end
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



11
12
13
# File 'lib/acfs/stub.rb', line 11

def opts
  @opts
end

Class Method Details

.allow_requests=(allow) ⇒ Object



118
119
120
# File 'lib/acfs/stub.rb', line 118

def allow_requests=(allow)
  @allow_requests = allow ? true : false
end

.allow_requests?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/acfs/stub.rb', line 122

def allow_requests?
  @allow_requests ||= false
end

.clear(klass = nil) ⇒ Object

Clear all stubs.



140
141
142
# File 'lib/acfs/stub.rb', line 140

def clear(klass = nil)
  klass.nil? ? stubs.clear : stubs[klass].try(:clear)
end

.disableObject



134
135
136
# File 'lib/acfs/stub.rb', line 134

def disable
  @enabled = false
end

.enableObject



130
131
132
# File 'lib/acfs/stub.rb', line 130

def enable
  @enabled = true
end

.enabled?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/acfs/stub.rb', line 126

def enabled?
  @enabled ||= false
end

.resource(klass, action, opts = {}, &_block) ⇒ Object

Stub a resource with given handler block. An already created handler for same resource class will be overridden.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/acfs/stub.rb', line 105

def resource(klass, action, opts = {}, &_block)
  action = action.to_sym
  unless ACTIONS.include? action
    raise ArgumentError.new "Unknown action `#{action}`."
  end

  Stub.new(opts).tap do |stub|
    stubs[klass]         ||= {}
    stubs[klass][action] ||= []
    stubs[klass][action] << stub
  end
end

.stub_for(operation) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/acfs/stub.rb', line 148

def stub_for(operation)
  return false unless (classes = stubs[operation.resource])
  return false unless (stubs = classes[operation.action])

  accepted_stubs = stubs.select {|stub| stub.accept?(operation) }

  if accepted_stubs.size > 1
    raise AmbiguousStubError.new(stubs: accepted_stubs, operation: operation)
  end

  accepted_stubs.first
end

.stubbed(operation) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/acfs/stub.rb', line 161

def stubbed(operation)
  stub = stub_for(operation)
  unless stub
    return false if allow_requests?

    raise RealRequestsNotAllowedError.new <<~ERROR
      No stub found for `#{operation.action}' on `#{operation.resource.name}' \
      with params `#{operation.full_params.inspect}', data `#{operation.data.inspect}' \
      and id `#{operation.id}'.

      Available stubs:
      #{pretty_print}
    ERROR
  end

  stub.call(operation)
  true
end

.stubsObject



144
145
146
# File 'lib/acfs/stub.rb', line 144

def stubs
  @stubs ||= {}
end

Instance Method Details

#accept?(operation) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/acfs/stub.rb', line 24

def accept?(operation)
  return opts[:with].call(operation) if opts[:with].respond_to?(:call)

  params = operation.full_params.stringify_keys
  data   = operation.data.stringify_keys
  with   = opts[:with]

  return true if with.nil?

  case opts.fetch(:match, :inclusion)
    when :legacy
      return true if with.empty? && params.empty? && data.empty?
      if with.compact == params.compact
        return true
      end
      if with.compact == data.compact
        return true
      end

      false
    when :inclusion
      with.each_pair.all? do |k, v|
        (params.key?(k) && params[k] == v) || (data.key?(k) && data[k] == v)
      end
  end
end

#call(operation) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/acfs/stub.rb', line 61

def call(operation)
  calls << operation

  err  = opts[:raise]
  data = opts[:return]

  if err
    raise_error(operation, err, opts[:return])
  elsif data
    data = data.call(operation) if data.respond_to?(:call)

    response = Acfs::Response.new(
      operation.request,
      headers: opts[:headers] || {},
      status: opts[:status] || 200,
      data: data || {},
    )

    operation.call(data, response)
  else
    raise ArgumentError.new 'Unsupported stub.'
  end
end

#called?(count = nil) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
# File 'lib/acfs/stub.rb', line 55

def called?(count = nil)
  count = count.count if count.respond_to?(:count)

  count.nil? ? calls.any? : calls.size == count
end

#callsObject



51
52
53
# File 'lib/acfs/stub.rb', line 51

def calls
  @calls ||= []
end