Class: ApiFixtures::Fixtures

Inherits:
Object
  • Object
show all
Defined in:
lib/api_fixtures/fixtures.rb

Constant Summary collapse

FIXTURES =
{
  :get =>    {},
  :put =>    {},
  :post =>   {},
  :delete => {}
}
EXPECTATIONS =
[]
CALLS =
[]

Class Method Summary collapse

Class Method Details

.assert_expected_calls(test) ⇒ Object



76
77
78
79
80
# File 'lib/api_fixtures/fixtures.rb', line 76

def self.assert_expected_calls(test)
  (EXPECTATIONS - CALLS).each do | method, path|
    test.flunk("Expected API call: #{method.to_s.upcase} #{path}")
  end
end

.clearObject



66
67
68
69
70
71
72
73
74
# File 'lib/api_fixtures/fixtures.rb', line 66

def self.clear
  FIXTURES[:get].clear
  FIXTURES[:put].clear
  FIXTURES[:post].clear
  FIXTURES[:delete].clear

  EXPECTATIONS.clear
  CALLS.clear
end

.expect(method, path, options = {}) ⇒ Object



52
53
54
55
56
57
# File 'lib/api_fixtures/fixtures.rb', line 52

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) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/api_fixtures/fixtures.rb', line 33

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 = fixtures_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

.fixtures_folderObject



20
21
22
23
24
25
26
27
# File 'lib/api_fixtures/fixtures.rb', line 20

def self.fixtures_folder
  unless @fixtures_folder
    if defined?(Rails)
      @fixtures_folder = (Rails.root + 'test/api_fixtures')
    end
  end
  @fixtures_folder
end

.fixtures_folder=(folder) ⇒ Object



29
30
31
# File 'lib/api_fixtures/fixtures.rb', line 29

def self.fixtures_folder=(folder)
  @fixtures_folder = folder
end

.lookup(method, path) ⇒ Object



59
60
61
62
63
64
# File 'lib/api_fixtures/fixtures.rb', line 59

def self.lookup(method, path)
  method = normalize_method(method)
  path   = normalize_path(path)
  CALLS << [method, path]
  FIXTURES[method][path]
end

.normalize_method(method) ⇒ Object



12
13
14
# File 'lib/api_fixtures/fixtures.rb', line 12

def self.normalize_method(method)
  method.downcase.to_sym
end

.normalize_path(path) ⇒ Object



16
17
18
# File 'lib/api_fixtures/fixtures.rb', line 16

def self.normalize_path(path)
  path.gsub(/\.\w+$/, '')
end