json-rigs 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1b5071004ba3dbb211567a93bd9f1e6abae17c6
4
- data.tar.gz: 6bdedbf07a0bac649bf6974ddb0fa2c06c3edd1b
3
+ metadata.gz: 11bd6f19e66a92d5555bb2627629b39fe6a4bc94
4
+ data.tar.gz: 16d7f93eaac261677e99cd292d9ba1b0477aa513
5
5
  SHA512:
6
- metadata.gz: f5655dd7ea5ff601f3006db20b1a9fc3cb40ded6086a87d7c1dd8047127743ffa681b7854e947fb4934da0834e80694b9811b1eb7d7f28dc521b025748330ead
7
- data.tar.gz: e9dcb96a57265427cf7d22843ce704515ac0d18d775b70d664834c646067157551861fd0d815d08e1cc0d8523d01b89328050351e5ed1532cb7569d9eb04d519
6
+ metadata.gz: 13496c0df69e5f81b6c574220859b28322c15e7d8b8db77e077cc17d37132565575605c4eef899ec13512251814bb55485c4a3f384362cb74462f312b3078df1
7
+ data.tar.gz: c572f014c9acbf33d0e78ad9936dce72cce15f3fd2952da8e456a95ef43d0fb17d033ac242d9403d1e1bdc66da3b1c7153b4a2c5691bfc03098dc66fc7d81013
@@ -1,5 +1,3 @@
1
- require 'json-rigs/fixture-templates'
2
-
3
1
  module JsonRigs
4
2
  class Fixture
5
3
 
@@ -14,9 +12,15 @@ module JsonRigs
14
12
  alias_method :delay, :pause_time=
15
13
  end
16
14
 
17
- class FixturedAction
18
- include JsonRigs::FixtureTemplates
15
+ class DynamicFixture < Fixture
16
+ attr_accessor :dynamic_response
17
+
18
+ def respond_to params
19
+ @dynamic_response.call(params)
20
+ end
21
+ end
19
22
 
23
+ class FixturedAction
20
24
  attr_reader :fixtures, :url
21
25
  attr_accessor :active_fixture_name
22
26
 
@@ -31,12 +35,33 @@ module JsonRigs
31
35
  fixture = Fixture.new
32
36
  @fixtures[fixture_name.to_s] = fixture
33
37
  fixture.instance_eval &block
34
- @active_fixture_name = ''
38
+ end
39
+
40
+ def dynamic_fixture fixture_name, proc
41
+ fixture = DynamicFixture.new
42
+ @fixtures[fixture_name.to_s] = fixture
43
+ fixture.dynamic_response = proc
35
44
  end
36
45
 
37
46
  def active_fixture; @fixtures[@active_fixture_name] end
38
47
  def active_fixture?; !@active_fixture_name.empty? end
39
48
 
49
+ def active_fixture_name=(fixture_name)
50
+ return unless has_fixture?(fixture_name) or fixture_name.empty?
51
+ @active_fixture_name = fixture_name
52
+ end
53
+
54
+ def has_fixture? fixture_name
55
+ @fixtures[fixture_name.to_s]
56
+ end
57
+
58
+ def remove_fixture fixture_name
59
+ if @active_fixture_name == fixture_name
60
+ @active_fixture_name = ''
61
+ end
62
+ @fixtures.delete(fixture_name)
63
+ end
64
+
40
65
  def to_hash
41
66
  fixture_hash = @fixtures.keys.each_with_object({}) do |fixture_name, hash|
42
67
  hash[fixture_name] = @active_fixture_name == fixture_name
@@ -53,6 +53,13 @@ module JsonRigs
53
53
  end
54
54
  end
55
55
 
56
+ def self.remove_action(url, method, response_name)
57
+ action = find_action(url, method)
58
+ if action
59
+ action.remove_fixture(response_name.to_s)
60
+ end
61
+ end
62
+
56
63
  def self.clear_all!
57
64
  @actions = {}
58
65
  end
@@ -16,12 +16,18 @@ module JsonRigs
16
16
  def self.load_fixtures
17
17
  puts "Loading fixtures from #{settings.fixture_path}..."
18
18
  Dir[File.join(settings.fixture_path, '**/*.json')].each do |f|
19
- load_fixture(f)
19
+ load_static_fixture(f)
20
+ end
21
+ Dir[File.join(settings.fixture_path, '**/*.rb')].each do |f|
22
+ load_dynamic_fixture(f)
20
23
  end
21
24
  end
22
25
 
23
26
  def self.load_fixture(f)
24
- puts "Loading #{f}..."
27
+ File.extname(f) == '.rb' ? load_dynamic_fixture(f) : load_static_fixture(f)
28
+ end
29
+
30
+ def self.extract_pieces_or_halt(f)
25
31
  relevant_path = f.sub(/\A#{settings.fixture_path}/, '')
26
32
  pieces = Pathname(relevant_path).each_filename.to_a
27
33
 
@@ -37,6 +43,13 @@ module JsonRigs
37
43
  url = pieces[0...-2].join('/')
38
44
  method = pieces[-2]
39
45
  response_name = File.basename(pieces[-1], ".*")
46
+
47
+ [url, method, response_name]
48
+ end
49
+
50
+ def self.load_static_fixture(f)
51
+ puts "Loading #{f}..."
52
+ url, method, response_name = extract_pieces_or_halt(f)
40
53
  contents = File.read(f)
41
54
  JsonRigs::Fixtures::fixture_action url, method do
42
55
  fixture response_name.to_sym do
@@ -45,8 +58,24 @@ module JsonRigs
45
58
  end
46
59
  end
47
60
 
61
+ def self.load_dynamic_fixture(f)
62
+ puts "Loading #{f}..."
63
+ url, method, response_name = extract_pieces_or_halt(f)
64
+ contents = File.read(f)
65
+ JsonRigs::Fixtures::fixture_action url, method do
66
+ dynamic_fixture response_name.to_sym, eval(contents)
67
+ end
68
+ end
69
+
70
+ def self.remove_fixture(f)
71
+ puts "Removing #{f}..."
72
+ url, method, response_name = extract_pieces_or_halt(f)
73
+
74
+ JsonRigs::Fixtures::remove_action url, method, response_name
75
+ end
76
+
48
77
  helpers do
49
- def serve_request(servlet_url, method)
78
+ def serve_request(servlet_url, params, method)
50
79
  action = JsonRigs::Fixtures::find_action(servlet_url, method)
51
80
  halt "unknown action '#{servlet_url} #{method}'" unless action
52
81
 
@@ -63,11 +92,17 @@ module JsonRigs
63
92
  sleep(fixture.pause_time)
64
93
  end
65
94
 
95
+ if fixture.class == JsonRigs::DynamicFixture
96
+ response = fixture.respond_to params
97
+ else
98
+ response = fixture.response
99
+ end
100
+
66
101
  content_type 'application/json'
67
- return fixture.response
102
+ response
68
103
  else
69
104
  logger.info 'No fixture specified, using stub response.'
70
- return '{ "success": false, "error_code": 0 }'
105
+ return 500, "{ \"success\": false, \"error\": \"No fixture specified. Please visit the test panel and chose a fixture for #{method} /#{servlet_url}\" }"
71
106
  end
72
107
  end
73
108
 
@@ -135,19 +170,19 @@ module JsonRigs
135
170
  end
136
171
 
137
172
  get '/*' do |servlet_url|
138
- serve_request(servlet_url, :GET)
173
+ serve_request(servlet_url, request.params, :GET)
139
174
  end
140
175
 
141
176
  post '/*' do |servlet_url|
142
- serve_request(servlet_url, :POST)
177
+ serve_request(servlet_url, request.params, :POST)
143
178
  end
144
179
 
145
180
  put '/*' do |servlet_url|
146
- serve_request(servlet_url, :PUT)
181
+ serve_request(servlet_url, request.params, :PUT)
147
182
  end
148
183
 
149
184
  delete '/*' do |servlet_url|
150
- serve_request(servlet_url, :DELETE)
185
+ serve_request(servlet_url, request.params, :DELETE)
151
186
  end
152
187
  end
153
188
 
@@ -166,7 +201,7 @@ module JsonRigs
166
201
  JsonRigs::Fixtures::load_active_fixtures
167
202
 
168
203
  # Start listener on fixture path to reload fixtures if necessary.
169
- listener = Listen.to(fixture_path, only: /\.json$/) { |modified, added, removed|
204
+ listener = Listen.to(fixture_path, only: /\.(json|rb)$/) { |modified, added, removed|
170
205
  on_fixture_change(modified, added, removed)
171
206
  }
172
207
  listener.start
@@ -179,8 +214,12 @@ module JsonRigs
179
214
  def on_fixture_change(modified, added, removed)
180
215
  puts 'Reloading fixtures...'
181
216
 
182
- JsonRigs::Fixtures::clear_all!
183
- FixtureServer::load_fixtures
217
+ removed.map do |f|
218
+ FixtureServer::remove_fixture(f)
219
+ end
220
+ (modified + added).map do |f|
221
+ FixtureServer::load_fixture(f)
222
+ end
184
223
  JsonRigs::Fixtures::load_active_fixtures
185
224
 
186
225
  puts 'Fixtures reloaded.'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-rigs
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clinkle
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-03-16 00:00:00.000000000 Z
12
+ date: 2015-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: daemons
@@ -82,8 +82,8 @@ dependencies:
82
82
  - !ruby/object:Gem::Version
83
83
  version: '4.0'
84
84
  description: Serve fixtured API responses quickly and easily. Runs a tiny sinatra
85
- server that serves .json files from disk. Control which fixtures are being used
86
- at localhost:port/test-panel. Great for QA and fast prototyping.
85
+ server that serves static .json files or dynamic .rb files from disk. Control which
86
+ fixtures are being used at localhost:port/test-panel. Great for QA and fast prototyping.
87
87
  email:
88
88
  - alex@clinkle.com
89
89
  - 21echoes@gmail.com
@@ -95,7 +95,6 @@ files:
95
95
  - bin/jrigs
96
96
  - lib/json-rigs.rb
97
97
  - lib/json-rigs/client.rb
98
- - lib/json-rigs/fixture-templates.rb
99
98
  - lib/json-rigs/fixture.rb
100
99
  - lib/json-rigs/fixtures.rb
101
100
  - lib/json-rigs/server.rb
@@ -1,22 +0,0 @@
1
- module JsonRigs
2
- module FixtureTemplates
3
- def basic_post_fixture
4
- fixture :success do
5
- respond_with %q(
6
- {
7
- "success": true
8
- }
9
- )
10
- end
11
-
12
- fixture :failure do
13
- respond_with %q(
14
- {
15
- "error_code": 0,
16
- "success": false
17
- }
18
- )
19
- end
20
- end
21
- end
22
- end