Class: Endpoint::Stub

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

Overview

Represents a stubbed endpoint that creates, updates, destroys, and stores data based on http requests.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, options) ⇒ Stub

Returns a new instance of Stub.



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

def initialize(model, options)
  @defaults = options[:defaults] || {}

  @model = model
  @site = URI "#{model.site}/#{model.name.split('::').last.underscore.pluralize}"

  @responses = {}

  @records = []
end

Class Attribute Details

.stubsObject (readonly)

Returns the value of attribute stubs.



12
13
14
# File 'lib/endpoint/stub.rb', line 12

def stubs
  @stubs
end

Instance Attribute Details

#defaultsObject (readonly)

Returns the value of attribute defaults.



81
82
83
# File 'lib/endpoint/stub.rb', line 81

def defaults
  @defaults
end

#modelObject (readonly)

Returns the value of attribute model.



82
83
84
# File 'lib/endpoint/stub.rb', line 82

def model
  @model
end

#recordsObject

Returns the value of attribute records.



84
85
86
# File 'lib/endpoint/stub.rb', line 84

def records
  @records
end

#siteObject (readonly)

Returns the value of attribute site.



83
84
85
# File 'lib/endpoint/stub.rb', line 83

def site
  @site
end

Class Method Details

.[](model) ⇒ Object

Gets or creates a stub for the given model. i.e. Endpoint::Stub



63
64
65
# File 'lib/endpoint/stub.rb', line 63

def [](model)
  create_for model or get_for model
end

.clear!Object

Clears all endpoint stubs.



56
57
58
# File 'lib/endpoint/stub.rb', line 56

def clear!
  @stubs = {}
end

.clear_all_records!Object



50
51
52
# File 'lib/endpoint/stub.rb', line 50

def clear_all_records!
  @stubs.values.each(&:clear_records!)
end

.clear_for(model) ⇒ Object

Removes fake endpoint for the given model, meaning any ActiveResource activity on the model will raise errors once again.



42
43
44
# File 'lib/endpoint/stub.rb', line 42

def clear_for(model)
  stubs.delete assure_model model
end

.create_for(model, options = {}, &block) ⇒ Object

Creates a fake endpoint for the given ActiveResource model.

The options hash currently only accepts :defaults, which allows you to define default attribute values for the endpoint to consider on record creation.

If a block is supplied, it will be executed in the context of the new Endpoint::Stub, allowing you to elegantly mock custom responses if needed.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/endpoint/stub.rb', line 23

def create_for(model, options={}, &block)
  model = assure_model model
  return if stubs.keys.map(&:name).include? model.name
  new_stub = Stub.new(model, options)

  EndpointStub::Config.default_responses.each do |response|
    new_stub.mock_response(*response)
  end

  @stubs[model] = new_stub

  new_stub.instance_eval(&block) if block_given?
  new_stub
end

.each(&block) ⇒ Object



67
68
69
# File 'lib/endpoint/stub.rb', line 67

def each(&block)
  @stubs.each(&block)
end

.get_for(model) ⇒ Object



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

def get_for(model)
  @stubs[assure_model(model)]
end

Instance Method Details

#add_default(attrs) ⇒ Object Also known as: add_defaults

Adds default attributes for record creation.



171
172
173
# File 'lib/endpoint/stub.rb', line 171

def add_default(attrs)
  @defaults.merge!(attrs)
end

#add_record(attrs) ⇒ Object

Adds a record to the stub, automatically assigning an id as though it were in a database.



99
100
101
102
103
104
105
106
107
# File 'lib/endpoint/stub.rb', line 99

def add_record(attrs)
  unless attrs.is_a? Hash
    raise "Endpoint::Stub#add_record expects a Hash. Got #{attrs.class.name}."
  end
  attrs[:id] = current_id
  attrs.merge!(@defaults) { |k,a,b| a }
  @records << attrs
  attrs
end

#clear_records!Object

Clear all records in this stub.



133
134
135
# File 'lib/endpoint/stub.rb', line 133

def clear_records!
  @records = []
end

#current_idObject

The next id for a record to be assigned to.



151
152
153
# File 'lib/endpoint/stub.rb', line 151

def current_id
  @records.count
end

#drop_overrides!Object

Removes all overrides, reducing each response to their originals.



234
235
236
237
238
239
240
# File 'lib/endpoint/stub.rb', line 234

def drop_overrides!
  @responses.each do |type, responses|
    responses.each do |route, response|
      response.drop_overrides!
    end
  end
end

#last_idObject

The last assigned id.



145
146
147
# File 'lib/endpoint/stub.rb', line 145

def last_id
  @records.count-1
end

#location(id) ⇒ Object

Gets the url location for the given id, as used by RESTful record creation.



164
165
166
167
# File 'lib/endpoint/stub.rb', line 164

def location(id)
  site = @site.to_s[-1] == '/' ? @site.to_s[0...-1] : @site
  "#{site}/#{id}"
end

#mock_response(type, route = '', proc = nil, &block) ⇒ Object

Mock a custom response. Requires a type (http mthod), and route. This method will override any previous responses assigned to the given type and route.

The route is the uri relative to the record’s assigned site and can be formatted similarly to rails routes. Such as: ‘/test/:some_param.json’ or ‘.xml’ to simply imply the model’s site with ‘.xml’ appended.

Lastly, a proc or block is needed to actually handle requests. The proc will be called with the request object, the extracted parameters from the uri, and the stub object so that you can interact with the stubbed records.



191
192
193
194
195
196
197
198
199
# File 'lib/endpoint/stub.rb', line 191

def mock_response(type, route='', proc=nil, &block)
  proc = block if block_given?
  route = clean_route route

  @responses[type] ||= {}
  @responses[type][route].deactivate! if @responses[type][route]
  @responses[type][route] = Response.new(type, prepare_uri(type, route), self, &proc)
  @responses[type][route].activate!
end

#model_nameObject

The name of the represented model in underscore notation.



157
158
159
# File 'lib/endpoint/stub.rb', line 157

def model_name
  @model.name.underscore
end

#override_all(&block) ⇒ Object

Overrides all currently assigned responses. Will not have any effect on responses mocked after this method is called.



224
225
226
227
228
229
230
# File 'lib/endpoint/stub.rb', line 224

def override_all(&block)
  @responses.each do |type, responses|
    responses.each do |route, response|
      response.add_to_stack(&block)
    end
  end
end

#override_response(type, route, proc = nil, &block) ⇒ Object

Same thing as mock_response, except it will not overWRITE existing mocks. Instead, it allows you to call a block inside of your response which will act as a ‘super’ call, invoking previously defined responses. Yielding inside a top-level response will give you an empty hash, so no nil related issues should arrise (unless of course the super-response returns nil, which it shouldn’t).

Also note that this does not re-activate a deactivated response.



210
211
212
213
214
215
216
217
218
219
# File 'lib/endpoint/stub.rb', line 210

def override_response(type, route, proc=nil, &block)
  proc = block if block_given?
  route = clean_route route

  if @responses[type] and @responses[type][route]
    @responses[type][route].add_to_stack(&proc)
  else
    mock_response(type, route, proc)
  end
end

#record(id) ⇒ Object

Get the record at the given id. Accepts strings as well as ints.



139
140
141
# File 'lib/endpoint/stub.rb', line 139

def record(id)
  @records[id.to_i]
end

#remove_record(id) ⇒ Object

Removes the record with the given id from the fake database.



123
124
125
126
127
128
129
# File 'lib/endpoint/stub.rb', line 123

def remove_record(id)
  id = id.to_i
  if @records[id]
    @records[id] = nil
    true
  end
end

#unmock_response(type, route) ⇒ Object

Remove a mocked response with the given type and route.



244
245
246
247
248
249
250
251
# File 'lib/endpoint/stub.rb', line 244

def unmock_response(type, route)
  route = clean_route route
  if @responses[type] && @responses[type][route]
    @responses[type][route].deactivate!
    @responses[type][route] = nil
    true
  end
end

#update_record(id, attrs) ⇒ Object

Updates the record with the given id with the given attributes.



111
112
113
114
115
116
117
118
119
# File 'lib/endpoint/stub.rb', line 111

def update_record(id, attrs)
  unless attrs.is_a? Hash
    raise "Endpoint::Stub#update_record expects a Hash. Got #{attrs.class.name}."
  end
  id = id.to_i
  if @records[id]
    @records[id].merge! attrs
  end
end