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



78
79
80
81
82
# File 'lib/api_fixtures/fixtures.rb', line 78

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



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

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



54
55
56
57
58
59
# File 'lib/api_fixtures/fixtures.rb', line 54

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



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

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

.folderObject



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

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

.folder=(folder) ⇒ Object



31
32
33
# File 'lib/api_fixtures/fixtures.rb', line 31

def self.folder=(folder)
  @folder = Pathname.new(folder)
end

.lookup(method, path) ⇒ Object



61
62
63
64
65
66
# File 'lib/api_fixtures/fixtures.rb', line 61

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

.normalize_method(method) ⇒ Object



14
15
16
# File 'lib/api_fixtures/fixtures.rb', line 14

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

.normalize_path(path) ⇒ Object



18
19
20
# File 'lib/api_fixtures/fixtures.rb', line 18

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