scenario_server 0.1.1 → 0.1.2

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: 32223f4ed5f4ad3533cbe09c0ca063bc7be5b742
4
- data.tar.gz: 4eda56538b588ac35b3ae8e34ea1a406929fdd56
3
+ metadata.gz: a2d129f756b4fd46d7b76c1e9c4da719f8b54d60
4
+ data.tar.gz: 86ccf9bc6aef34399203dd7b4d4b509e1a78d5fb
5
5
  SHA512:
6
- metadata.gz: ffe3f0ee56ee992479356a51f179b802c71490878cf3ff74bc85c250651ad4ac1a67b057a0a49db14d18c5a49755329f09394ab08c5e08947c901512eea607ae
7
- data.tar.gz: 84097547730134d05eeb210b75f08bb607dddefab908ecf044a6223da07dcb59c3d6059be414ee41c019c13943e5ab72055e56bfe77634273d9a1042d8ce0550
6
+ metadata.gz: 67f361fc85f467f9ca87090dee26eaccc747a14b54b639c7a7ea406612403805338226f5a4698c4b90258c90a27869d65b714dd491485f39e4053515b299ac85
7
+ data.tar.gz: 8bb363a05b863f1cbf27a2341a0382600ed80e8a97a075ea2a2ed372708eabb28db66bbb75ac7c3e949a14a10ff91079188debd23ae2229ebcc27288ad0657ab
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scenario_server (0.0.5)
4
+ scenario_server (0.1.1)
5
5
  daemons (~> 1.1)
6
6
  sequel (= 4.7)
7
7
  sinatra (~> 1.4)
@@ -11,29 +11,29 @@ PATH
11
11
  GEM
12
12
  remote: https://rubygems.org/
13
13
  specs:
14
- backports (3.6.0)
14
+ backports (3.6.4)
15
15
  daemons (1.1.9)
16
16
  diff-lcs (1.2.5)
17
- json_spec (1.1.1)
17
+ json_spec (1.1.4)
18
18
  multi_json (~> 1.0)
19
- rspec (~> 2.0)
20
- multi_json (1.8.4)
21
- rack (1.5.2)
22
- rack-protection (1.5.2)
19
+ rspec (>= 2.0, < 4.0)
20
+ multi_json (1.10.1)
21
+ rack (1.6.0)
22
+ rack-protection (1.5.3)
23
23
  rack
24
- rack-test (0.6.2)
24
+ rack-test (0.6.3)
25
25
  rack (>= 1.0)
26
- rake (10.1.1)
27
- rspec (2.14.1)
28
- rspec-core (~> 2.14.0)
29
- rspec-expectations (~> 2.14.0)
30
- rspec-mocks (~> 2.14.0)
31
- rspec-core (2.14.7)
32
- rspec-expectations (2.14.5)
26
+ rake (10.4.2)
27
+ rspec (2.99.0)
28
+ rspec-core (~> 2.99.0)
29
+ rspec-expectations (~> 2.99.0)
30
+ rspec-mocks (~> 2.99.0)
31
+ rspec-core (2.99.2)
32
+ rspec-expectations (2.99.2)
33
33
  diff-lcs (>= 1.1.3, < 2.0)
34
- rspec-mocks (2.14.5)
34
+ rspec-mocks (2.99.3)
35
35
  sequel (4.7.0)
36
- sinatra (1.4.4)
36
+ sinatra (1.4.5)
37
37
  rack (~> 1.4)
38
38
  rack-protection (~> 1.4)
39
39
  tilt (~> 1.3, >= 1.3.4)
@@ -44,7 +44,7 @@ GEM
44
44
  rack-test
45
45
  sinatra (~> 1.4.0)
46
46
  tilt (~> 1.3)
47
- sqlite3 (1.3.8)
47
+ sqlite3 (1.3.10)
48
48
  tilt (1.4.1)
49
49
 
50
50
  PLATFORMS
data/README.md CHANGED
@@ -1,59 +1,95 @@
1
1
  # Scenarios
2
2
 
3
- This gem is used to setup a quick local server using sinatra
3
+ This gem can be used to setup a quick local api server.
4
4
 
5
- As the server is started for the first time, point your browser to http://localhost:4567/scenarios
6
- Here a "default" scenario is present. You can add various routes to this scenario.
5
+ ## Installation
7
6
 
8
- for e.g. a sample route added would be
9
- GET , '/v1/season' {"temperature":"cold"}
7
+ Add this line to your application's Gemfile:
10
8
 
11
- Now when you make a get request, e.g.
12
- curl -i http://localhost:9000/v1/season
13
- you will receive the json fixture added for that route.
9
+ gem 'scenario_server'
14
10
 
15
- Working with Scenarios :
11
+ Or manually install it:
16
12
 
17
- This server also lets you add different scenarios and lets you add a different response {"temperature":"warm"} for the same api endpoint under different scenario. e.g. march
13
+ $ gem install scenario_server
18
14
 
19
- When you make a get request and then specify header SCENE with value as your scenario name, the response under that scenario would be returned.
20
- e.g.
21
- curl -i -H "SCENE:march" http://localhost:9000/v1/season
15
+ ## Configuration
22
16
 
23
- If you make a get request that is not defined for a particular scenario but is defined in the default scenario, then the response from the default scenario would be returned.
17
+ A yaml file when provided at the following location
18
+ > ~/.scenarios/config.yml
24
19
 
25
- ## Installation
20
+ can be used to define the port and path of the sqlite database holding the routes.
21
+ If this file is not provided, the db file is created wherever the ruby gem is installed.
26
22
 
27
- Add this line to your application's Gemfile:
23
+ #### Config.yml
24
+ >localport: 4567
25
+ >localdbfile: /Users/vaibhav/.scenarios/scenarios.sqlite3
28
26
 
29
- gem 'scenario_server'
27
+ ## Usage
28
+
29
+ The server can be started in daemon mode by
30
30
 
31
- And then execute:
31
+ scenario_service start
32
32
 
33
- $ bundle
33
+ or without a daemon mode
34
34
 
35
- Or install it yourself as:
35
+ scenario_service start -t
36
36
 
37
- $ gem install scenario_server
37
+ ### What are Scenarios
38
+ Scenarios is a context in which you are using your local api server. Each scenario contains one or more routes.
38
39
 
39
- ## Usage
40
+ e.g.
41
+ 1. Default
42
+ * GET , '/v1/season', 200, {"name":"summer","temperature":70}
43
+ * GET , '/v1/city' , 200, {"name":"Seattle"}
44
+ 1. Rainy
45
+ * GET , '/v1/season', 200, {"name":"rainy","temperature":50,"precipitation":5}
46
+ 1. Error
47
+ * GET , '/v1/season', 404, {"error":"sensors not found"}
48
+ * GET , '/v1/city' , 500, {"error":"server error"}
49
+
50
+ Scenarios are usually broken up as:
51
+ * For handling most common api response: 'Default' Scenario server comes with the server and this cannot be deleted.
52
+ * For handling alternate api response e.g. 'Rainy' with precipitation information in our current example
53
+ * For handling error responses e.g. 'error'
54
+
55
+ On startup, all responses returned from the server are from default scenario.
56
+
57
+ ### To fetch data from routes
58
+
59
+ Make a request with scenario server url and path of the route with the correct request type. Also specify header **SCENE** with your scenario name,
60
+ This will return the fixture under that scenario, with status code and headers specified
61
+ e.g.
40
62
 
41
- The server can be started in daemon mode by
63
+ curl -X GET -H "SCENE:rainy" http://localhost:4567/v1/season
42
64
 
43
- scenario_service start
65
+ If the header **SCENE** is not present, the current scenario set on the server would be used.
44
66
 
45
- or
67
+ will return
68
+ ` {"name":"rainy","temperature":50,"precipitation":5} `
46
69
 
47
- without a daemon mode
70
+ ### To list all scenarios
71
+ curl http://localhost:4567/scenarios
72
+ or point the browser to http://localhost:4567/scenarios
48
73
 
49
- scenario_service start -T
74
+ ### To check the current scenario
75
+ curl http://localhost:4567/scenario
76
+ or point the browser to http://localhost:4567/scenario
50
77
 
51
- ## Configuration
78
+ ### To change the current scenario from 'default' to 'rainy'
79
+ curl -X PUT http://localhost:4567/scenario/rainy -d ''
80
+
81
+ Note: If you are not in 'default' scenario and make a call to a route which is not defined, server will also check 'default' for that route. If present, it will return data from that route else will give a 404.
82
+
83
+ ### To add a new scenario 'winter'
84
+ curl -X POST http://localhost:4567/scenarios/new -d 'winter'
85
+ or point the browser to http://localhost:4567/scenarios and add name in the text box below **New scenario** and then click **submit**.
86
+
87
+ ### To delete scenario 'winter'
88
+ Given that winter scenario has an id **3**
89
+
90
+ curl -X DELETE http://localhost:4567/scenarios/3
52
91
 
53
- a yaml file can be provided at the following location
54
- ~/.scenarios/config.yml
55
- to hold the port to be used and the path of a sqlite database.
56
- If this file is not provided, the db file is created inside the
57
- gem folder.
92
+ ### To get all routes from 'rainy' scenario
93
+ Given that 'rainy' scenario has an id **2**
58
94
 
59
- Please see sample_config.yml for more info.
95
+ curl http://localhost:4567/scenarios/2/routes
@@ -12,7 +12,7 @@ class ScenarioServer < Sinatra::Base
12
12
  register Sinatra::ConfigFile
13
13
  use Rack::MethodOverride
14
14
 
15
- configure :production do
15
+ configure :production do
16
16
  puts 'Production Environment'
17
17
  config_file File.expand_path('~/.scenarios') + '/config.yml'
18
18
  set :db_file , File.dirname(File.expand_path(__FILE__)) + '/../data/scenario_db.sqlite3'
@@ -88,7 +88,7 @@ class ScenarioServer < Sinatra::Base
88
88
  selected_scenario = self.scenario_db.get_scenario_for_id(params[:scenario_id])
89
89
  routes = self.scenario_db.get_routes_for_scenario(params[:scenario_id])
90
90
 
91
- if (request.env['HTTP_ACCEPT'] && request.env['HTTP_ACCEPT'].include?('text/html'))
91
+ if (request.env['HTTP_ACCEPT'] && request.env['HTTP_ACCEPT'].include?('text/html'))
92
92
  if params.has_key?('error')
93
93
  erb :'scenario', :locals => {'scenario'=>selected_scenario, 'routes'=>routes, 'error'=>params['error']}
94
94
  else
@@ -132,10 +132,10 @@ class ScenarioServer < Sinatra::Base
132
132
  if valid_fixture
133
133
  self.scenario_db.add_route_for_scenario(params['route_type'],
134
134
  params['path'],
135
- params['status_code'],
136
- params['headers'],
137
- params['fixture'],
138
- params[:scenario_id])
135
+ params['status_code'],
136
+ params['headers'],
137
+ params['fixture'],
138
+ params[:scenario_id])
139
139
  redirect('/scenarios/'+params[:scenario_id])
140
140
  else
141
141
  redirect('/scenarios/'+params[:scenario_id]+'?error=invalid%20json')
@@ -145,10 +145,10 @@ class ScenarioServer < Sinatra::Base
145
145
  json_body = JSON.parse(request.body.read)
146
146
  route_id = self.scenario_db.add_route_for_scenario(json_body['route_type'],
147
147
  json_body['path'],
148
- json_body['status_code'],
149
- json_body['headers'],
150
- json_body['fixture'],
151
- params[:scenario_id])
148
+ json_body['status_code'],
149
+ json_body['headers'],
150
+ json_body['fixture'],
151
+ params[:scenario_id])
152
152
  content = {'url'=>'/scenarios/'+params[:scenario_id]+'/routes/'+route_id.to_s}.to_json
153
153
  [200, content]
154
154
  end
@@ -212,32 +212,32 @@ class ScenarioServer < Sinatra::Base
212
212
  end
213
213
 
214
214
  # route path calls
215
- get '/*' do
216
- #TODO: Validate scene from the list of scenarios
217
- ordered_scenarios = self.scenario_db.get_ordered_scenarios
215
+ get '/*' do
216
+ #TODO: Validate scene from the list of scenarios
217
+ ordered_scenarios = self.scenario_db.get_ordered_scenarios
218
218
 
219
219
 
220
- #TODO: dry up the implementation
221
- #TODO: add ignore scene header
222
- current_scenario = settings.scenario
220
+ #TODO: dry up the implementation
221
+ #TODO: add ignore scene header
222
+ current_scenario = settings.scenario
223
223
  if (request.env['HTTP_SCENE'])
224
- current_scenario = request.env['HTTP_SCENE']
225
- end
224
+ current_scenario = request.env['HTTP_SCENE']
225
+ end
226
226
 
227
227
  route_type = request.request_method.upcase
228
228
  path = request.path.downcase
229
229
 
230
230
  status_code, header, fixture = get_fixture(route_type, path, current_scenario)
231
231
  if header.nil? || header == ""
232
- header = {'Content-Type' => 'application/json'}
233
- else
234
- header = eval(header)
235
- end
232
+ header = {'Content-Type' => 'application/json'}
233
+ else
234
+ header = eval(header)
235
+ end
236
236
 
237
- if fixture.nil?
237
+ if fixture.nil?
238
238
  404
239
239
  else
240
- status status_code
240
+ status status_code
241
241
  headers header
242
242
  body fixture
243
243
  end
@@ -249,10 +249,10 @@ class ScenarioServer < Sinatra::Base
249
249
  route_type = request.request_method.upcase
250
250
  path = request.path.downcase
251
251
 
252
- current_scenario = settings.scenario
252
+ current_scenario = settings.scenario
253
253
  if (request.env['HTTP_SCENE'])
254
- current_scenario = request.env['HTTP_SCENE']
255
- end
254
+ current_scenario = request.env['HTTP_SCENE']
255
+ end
256
256
 
257
257
  status_code, header, fixture = get_fixture(route_type, path, current_scenario)
258
258
  if fixture.nil?
@@ -268,10 +268,10 @@ class ScenarioServer < Sinatra::Base
268
268
  route_type = request.request_method.upcase
269
269
  path = request.path.downcase
270
270
 
271
- current_scenario = settings.scenario
271
+ current_scenario = settings.scenario
272
272
  if (request.env['HTTP_SCENE'])
273
- current_scenario = request.env['HTTP_SCENE']
274
- end
273
+ current_scenario = request.env['HTTP_SCENE']
274
+ end
275
275
 
276
276
  status_code, header, fixture = get_fixture(route_type, path, current_scenario)
277
277
  if fixture.nil?
@@ -289,10 +289,10 @@ class ScenarioServer < Sinatra::Base
289
289
  route_type = request.request_method.upcase
290
290
  path = request.path.downcase
291
291
 
292
- current_scenario = settings.scenario
292
+ current_scenario = settings.scenario
293
293
  if (request.env['HTTP_SCENE'])
294
- current_scenario = request.env['HTTP_SCENE']
295
- end
294
+ current_scenario = request.env['HTTP_SCENE']
295
+ end
296
296
 
297
297
  status_code, header, fixture = get_fixture(route_type, path, current_scenario)
298
298
  if fixture.nil?
@@ -236,9 +236,8 @@ class ScenarioDB
236
236
  end
237
237
 
238
238
  def get_fixture_from_routes(route_type, path, scenario_name)
239
- self.routes.left_outer_join(:scenarios, :id=>:scenario_id).where(:route_type=>route_type,
240
- :path=>path,
241
- :name=>scenario_name).map([:status_code, :headers, :fixture]).first
239
+ self.routes.left_outer_join(:scenarios,
240
+ :id=>:scenario_id).where{Sequel.&({:route_type => route_type, :name=>scenario_name}, Sequel.ilike(:path,path))}.map([:status_code,:headers,:fixture]).first
242
241
 
243
242
  end
244
243
 
@@ -1,3 +1,3 @@
1
1
  module Scenarios
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scenario_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vaibhav Bhatia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-11 00:00:00.000000000 Z
11
+ date: 2015-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra