class ApiFixtures::Fixtures

Constants

CALLS
EXPECTATIONS
FIXTURES

Public Class Methods

assert_expected_calls(test) click to toggle source
# File lib/api_fixtures/fixtures.rb, line 79
def self.assert_expected_calls(test)
  (EXPECTATIONS - CALLS).each do | method, path|
    test.flunk("Expected API call: #{method.to_s.upcase} #{path}")
  end
end
clear() click to toggle source
# File lib/api_fixtures/fixtures.rb, line 69
def self.clear
  FIXTURES[:get].clear
  FIXTURES[:put].clear
  FIXTURES[:post].clear
  FIXTURES[:delete].clear

  EXPECTATIONS.clear
  CALLS.clear
end
expect(method, path, options = {}) click to toggle source
# File lib/api_fixtures/fixtures.rb, line 55
def self.expect(method, path, options = {})
  if options[:response]
    fixture(method, path, options[:response])
  end
  EXPECTATIONS << [normalize_method(method), path]
end
fixture(method, path, response = nil) click to toggle source
# File lib/api_fixtures/fixtures.rb, line 36
def self.fixture(method, path, response = nil)
  ApiFixtures::Middleware.must_be_in_stack!

  case response
  when Hash
    response = [200, {'Content-Type' => 'application/json; charset=utf-8'}, [response.to_json]]
  when Array
    case response[2]
    when String
      response[2] = [response[2]]
    end
  when nil
    file_name = folder + ('.' + path + '.json')
    response = [200, {'Content-Type' => 'application/json; charset=utf-8'}, [File.read(file_name.to_s)]]
  end

  FIXTURES[normalize_method(method)][path] = response
end
folder() click to toggle source
# File lib/api_fixtures/fixtures.rb, line 23
def self.folder
  unless @folder
    if defined?(Rails)
      @folder = (Rails.root + 'test/api_fixtures')
    end
  end
  @folder
end
folder=(folder) click to toggle source
# File lib/api_fixtures/fixtures.rb, line 32
def self.folder=(folder)
  @folder = Pathname.new(folder)
end
lookup(method, path) click to toggle source
# File lib/api_fixtures/fixtures.rb, line 62
def self.lookup(method, path)
  method = normalize_method(method)
  path   = normalize_path(path)
  CALLS << [method, path]
  FIXTURES[method][path]
end
normalize_method(method) click to toggle source
# File lib/api_fixtures/fixtures.rb, line 15
def self.normalize_method(method)
  method.downcase.to_sym
end
normalize_path(path) click to toggle source
# File lib/api_fixtures/fixtures.rb, line 19
def self.normalize_path(path)
  path.gsub(/\.\w+$/, '')
end