Class: ApiFixtures::Fixtures
- Inherits:
-
Object
- Object
- ApiFixtures::Fixtures
- Defined in:
- lib/api_fixtures/fixtures.rb
Constant Summary collapse
- FIXTURES =
{ :get => {}, :put => {}, :post => {}, :delete => {} }
- EXPECTATIONS =
[]
- CALLS =
[]
Class Method Summary collapse
- .assert_expected_calls(test) ⇒ Object
- .clear ⇒ Object
- .expect(method, path, options = {}) ⇒ Object
- .fixture(method, path, response = nil) ⇒ Object
- .folder ⇒ Object
- .folder=(folder) ⇒ Object
- .lookup(method, path) ⇒ Object
- .normalize_method(method) ⇒ Object
- .normalize_path(path) ⇒ Object
Class Method Details
.assert_expected_calls(test) ⇒ Object
79 80 81 82 83 |
# 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 ⇒ Object
69 70 71 72 73 74 75 76 77 |
# 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 = {}) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/api_fixtures/fixtures.rb', line 55 def self.expect(method, path, = {}) if [:response] fixture(method, path, [:response]) end EXPECTATIONS << [normalize_method(method), path] end |
.fixture(method, path, response = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# 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 ⇒ Object
23 24 25 26 27 28 29 30 |
# 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) ⇒ Object
32 33 34 |
# File 'lib/api_fixtures/fixtures.rb', line 32 def self.folder=(folder) @folder = Pathname.new(folder) end |
.lookup(method, path) ⇒ Object
62 63 64 65 66 67 |
# 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) ⇒ Object
15 16 17 |
# File 'lib/api_fixtures/fixtures.rb', line 15 def self.normalize_method(method) method.downcase.to_sym end |
.normalize_path(path) ⇒ Object
19 20 21 |
# File 'lib/api_fixtures/fixtures.rb', line 19 def self.normalize_path(path) path.gsub(/\.\w+$/, '') end |