Class: Acfs::Stub

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

Overview

Global handler for stubbing resources.

Constant Summary

ACTIONS =
[:read, :create, :update, :delete, :list]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Stub

Returns a new instance of Stub



11
12
13
14
15
16
17
# File 'lib/acfs/stub.rb', line 11

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
  @opts[:return].map! {|h| h.stringify_keys! if h.is_a? Hash } if @opts[:return].is_a? Array
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts



9
10
11
# File 'lib/acfs/stub.rb', line 9

def opts
  @opts
end

Class Method Details

.allow_requests=(allow) ⇒ Object



95
96
97
# File 'lib/acfs/stub.rb', line 95

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

.allow_requests?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/acfs/stub.rb', line 99

def allow_requests?
  @allow_requests ||= false
end

.clear(klass = nil) ⇒ Object

Clear all stubs.



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

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

.disableObject



111
112
113
# File 'lib/acfs/stub.rb', line 111

def disable
  @enabled = false
end

.enableObject



107
108
109
# File 'lib/acfs/stub.rb', line 107

def enable
  @enabled = true
end

.enabled?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/acfs/stub.rb', line 103

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.



84
85
86
87
88
89
90
91
92
93
# File 'lib/acfs/stub.rb', line 84

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

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

.stub_for(op) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/acfs/stub.rb', line 125

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

  accepted_stubs = stubs.select {|stub| stub.accept? op }

  raise AmbiguousStubError.new stubs: accepted_stubs, operation: op if accepted_stubs.size > 1

  accepted_stubs.first
end

.stubbed(op) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/acfs/stub.rb', line 136

def stubbed(op)
  stub = stub_for op
  unless stub
    return false if allow_requests?
    raise RealRequestsNotAllowedError.new <<-MSG.strip.gsub(/^[ ]{12}/, '')
      No stub found for `#{op.action}' on `#{op.resource.name}' with params `#{op.full_params.inspect}', data `#{op.data.inspect}' and id `#{op.id}'.

      Available stubs:
      #{pretty_print}
    MSG
  end

  stub.call op
  true
end

.stubsObject



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

def stubs
  @stubs ||= {}
end

Instance Method Details

#accept?(op) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acfs/stub.rb', line 19

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

  params = op.full_params.stringify_keys
  data   = op.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?
      return true if with.reject {|_, v| v.nil? } == params.reject {|_, v| v.nil? }
      return true if with.reject {|_, v| v.nil? } == data.reject {|_, v| v.nil? }
      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(op) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/acfs/stub.rb', line 50

def call(op)
  calls << op

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

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

    response = Acfs::Response.new op.request,
      headers: opts[:headers] || {},
      status:  opts[:status] || 200,
      data:    data || {}
    op.call data, response
  else
    raise ArgumentError.new 'Unsupported stub.'
  end
end

#called?(count = nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/acfs/stub.rb', line 45

def called?(count = nil)
  count = count.count if count.respond_to? :count # For `5.times` Enumerators
  count.nil? ? calls.any? : calls.size == count
end

#callsObject



41
42
43
# File 'lib/acfs/stub.rb', line 41

def calls
  @calls ||= []
end