Class: JsonRigs::FixtureServer
- Inherits:
-
Sinatra::Base
- Object
- Sinatra::Base
- JsonRigs::FixtureServer
- Defined in:
- lib/json-rigs/server.rb
Class Method Summary collapse
- .extract_pieces_or_halt(f) ⇒ Object
- .load_dynamic_fixture(f) ⇒ Object
- .load_fixture(f) ⇒ Object
- .load_fixtures ⇒ Object
- .load_static_fixture(f) ⇒ Object
- .remove_fixture(f) ⇒ Object
Class Method Details
.extract_pieces_or_halt(f) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/json-rigs/server.rb', line 30 def self.extract_pieces_or_halt(f) relevant_path = f.sub(/\A#{settings.fixture_path}/, '') pieces = Pathname(relevant_path).each_filename.to_a unless pieces.length >= 2 = %Q( Fixtures are not structured correctly. Please place .json files inside #{settings.fixture_path} as /[url]/[HTTP method]/[response type].json, e.g. /users/GET/success.json ) logger.error halt end url = pieces[0...-2].join('/') method = pieces[-2] response_name = File.basename(pieces[-1], ".*") [url, method, response_name] end |
.load_dynamic_fixture(f) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/json-rigs/server.rb', line 61 def self.load_dynamic_fixture(f) puts "Loading #{f}..." url, method, response_name = extract_pieces_or_halt(f) contents = File.read(f) JsonRigs::Fixtures::fixture_action url, method do dynamic_fixture response_name.to_sym, eval(contents) end end |
.load_fixture(f) ⇒ Object
26 27 28 |
# File 'lib/json-rigs/server.rb', line 26 def self.load_fixture(f) File.extname(f) == '.rb' ? load_dynamic_fixture(f) : load_static_fixture(f) end |
.load_fixtures ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/json-rigs/server.rb', line 16 def self.load_fixtures puts "Loading fixtures from #{settings.fixture_path}..." Dir[File.join(settings.fixture_path, '**/*.json')].each do |f| load_static_fixture(f) end Dir[File.join(settings.fixture_path, '**/*.rb')].each do |f| load_dynamic_fixture(f) end end |
.load_static_fixture(f) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/json-rigs/server.rb', line 50 def self.load_static_fixture(f) puts "Loading #{f}..." url, method, response_name = extract_pieces_or_halt(f) contents = File.read(f) JsonRigs::Fixtures::fixture_action url, method do fixture response_name.to_sym do respond_with(contents) end end end |
.remove_fixture(f) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/json-rigs/server.rb', line 70 def self.remove_fixture(f) puts "Removing #{f}..." url, method, response_name = extract_pieces_or_halt(f) JsonRigs::Fixtures::remove_action url, method, response_name end |