module OldApiResource::Mocks

Public Class Methods

clear_endpoints() click to toggle source

clear out the defined mocks

# File lib/old_api_resource/mocks.rb, line 46
def self.clear_endpoints
  ret = @@endpoints
  @@endpoints = {}
  ret
end
define(&block) click to toggle source
# File lib/old_api_resource/mocks.rb, line 59
def self.define(&block)
  instance_eval(&block) if block_given?
end
endpoint(path, &block) click to toggle source

define an endpoint for the mock

# File lib/old_api_resource/mocks.rb, line 63
def self.endpoint(path, &block)
  path, format = path.split(".")
  @@endpoints[path] ||= []
  with_path_and_format(path, format) do
    instance_eval(&block) if block_given?
  end
end
endpoints() click to toggle source

return the defined endpoints

# File lib/old_api_resource/mocks.rb, line 56
def self.endpoints
  @@endpoints
end
extract_params(known_path, entered_path) click to toggle source

This method assumes that the two are matching paths if they aren’t the behavior is undefined

# File lib/old_api_resource/mocks.rb, line 105
def self.extract_params(known_path, entered_path)
  PathString.extract_params(known_path, entered_path)
end
find_response(request) click to toggle source

find a matching response

# File lib/old_api_resource/mocks.rb, line 71
def self.find_response(request)
  path = request.path.split("?").first
  path = path.split(/\./).first
  # these are stored as [[Request, Response], [Request, Response]]
  responses_and_params = self.matching(path)
  ret = (responses_and_params[:responses] || []).select{|pair| pair.first.match?(request)}
  raise Exception.new("More than one response matches #{request}") if ret.length > 1
  return ret.first ? {:response => ret.first[1], :params => responses_and_params[:params]} : nil
end
init() click to toggle source

set OldApiResource’s http

# File lib/old_api_resource/mocks.rb, line 36
def self.init
  ::OldApiResource::Connection.class_eval do
    private
    def http(path)
      Interface.new(path)
    end
  end
end
matching(path) click to toggle source

returns a hash {:responses => [[Request, Response],], :params => {…}} if there is no match returns nil

# File lib/old_api_resource/mocks.rb, line 83
def self.matching(path)
  # The obvious case
  if @@endpoints[path]
    return {:responses => @@endpoints[path], :params => {}}
  end
  # parameter names prefixed with colons should match parts
  # of the path and push those parameters into the response
  @@endpoints.keys.each do |possible_path|
    if self.paths_match?(possible_path, path)
      return {:responses => @@endpoints[possible_path], :params => self.extract_params(possible_path, path)}
    end
  end

  return {:responses => nil, :params => nil}
end
paths_match?(known_path, entered_path) click to toggle source
# File lib/old_api_resource/mocks.rb, line 99
def self.paths_match?(known_path, entered_path)
  PathString.paths_match?(known_path, entered_path)
end
set_endpoints(new_endpoints) click to toggle source

re-set the endpoints

# File lib/old_api_resource/mocks.rb, line 52
def self.set_endpoints(new_endpoints)
  @@endpoints = new_endpoints
end

Private Class Methods

with_path_and_format(path, format) { || ... } click to toggle source
# File lib/old_api_resource/mocks.rb, line 111
def self.with_path_and_format(path, format, &block)
  @@path, @@format = path, format
  ret = yield
  @@path, @@format = nil, nil
  ret
end

Public Instance Methods

http(path) click to toggle source
# File lib/old_api_resource/mocks.rb, line 39
def http(path)
  Interface.new(path)
end