api_taster 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -29,6 +29,14 @@ Rails.application.routes.draw do
29
29
  end
30
30
  ```
31
31
 
32
+ Add API Taster into the autoload paths in `application.rb`:
33
+
34
+ ```ruby
35
+ config.autoload_paths += %W(
36
+ #{ApiTaster::Engine.root}
37
+ )
38
+ ```
39
+
32
40
  In `routes.rb`, define parameters for each API endpoint after the normal routes definition block. For example:
33
41
 
34
42
  ```ruby
@@ -6,17 +6,21 @@ module ApiTaster
6
6
  class << self
7
7
  def routes
8
8
  _routes = []
9
+ i = -1
10
+
11
+ route_set.routes.each do |route|
12
+ next if route.app.is_a?(Sprockets::Environment)
13
+ next if route.app == ApiTaster::Engine
14
+
15
+ if rack_app = discover_rack_app(route.app)
16
+ rack_app.routes.routes.each do |rack_route|
17
+ _routes << normalise_route(rack_route, i+=1)
18
+ end
19
+ end
9
20
 
10
- route_set.routes.each_with_index do |route, index|
11
21
  next if route.verb.source.empty?
12
22
 
13
- _routes << {
14
- :id => index,
15
- :name => route.name,
16
- :verb => route.verb.source.gsub(/[$^]/, ''),
17
- :path => route.path.spec.to_s.sub('(.:format)', ''),
18
- :reqs => route.requirements
19
- }
23
+ _routes << normalise_route(route, i+=1)
20
24
  end
21
25
 
22
26
  _routes
@@ -47,6 +51,25 @@ module ApiTaster
47
51
 
48
52
  private
49
53
 
54
+ def discover_rack_app(app)
55
+ class_name = app.class.name.to_s
56
+ if class_name == "ActionDispatch::Routing::Mapper::Constraints"
57
+ discover_rack_app(app.app)
58
+ elsif class_name !~ /^ActionDispatch::Routing/
59
+ app
60
+ end
61
+ end
62
+
63
+ def normalise_route(route, id)
64
+ {
65
+ :id => id,
66
+ :name => route.name,
67
+ :verb => route.verb.source.gsub(/[$^]/, ''),
68
+ :path => route.path.spec.to_s.sub('(.:format)', ''),
69
+ :reqs => route.requirements
70
+ }
71
+ end
72
+
50
73
  def split_input(input, route)
51
74
  url_param_keys = route[:path].scan /:\w+/
52
75
 
@@ -1,3 +1,3 @@
1
1
  module ApiTaster
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/spec/route_spec.rb CHANGED
@@ -22,6 +22,7 @@ module ApiTaster
22
22
  resources :users do
23
23
  resources :comments
24
24
  end
25
+ mount Rails.application => '/app'
25
26
  end
26
27
 
27
28
  Route.route_set = routes
@@ -54,11 +55,13 @@ module ApiTaster
54
55
  :id => 0,
55
56
  :path => '/dummy/:dummy_id'
56
57
  }, {
57
- :id => 1
58
+ :id => 999,
59
+ :path => 'a_non_existing_dummy',
60
+ :verb => 'get'
58
61
  }])
59
62
  Route.inputs[0] = [{ :dummy_id => 1, :hello => 'world' }]
60
63
 
61
- Route.inputs_for(Route.find(1)).should have_key(:undefined)
64
+ Route.inputs_for(Route.find(999)).should have_key(:undefined)
62
65
 
63
66
  2.times do
64
67
  Route.inputs_for(Route.find(0)).should == [{
@@ -67,5 +70,19 @@ module ApiTaster
67
70
  }]
68
71
  end
69
72
  end
73
+
74
+ context "private methods" do
75
+ it "#discover_rack_app" do
76
+ klass = Class.new
77
+ klass.stub_chain(:class, :name).and_return(ActionDispatch::Routing::Mapper::Constraints)
78
+ klass.stub(:app).and_return('klass')
79
+
80
+ Route.send(:discover_rack_app, klass).should == 'klass'
81
+ end
82
+
83
+ it "#discover_rack_app" do
84
+ Route.send(:discover_rack_app, ApiTaster::Engine).should == ApiTaster::Engine
85
+ end
86
+ end
70
87
  end
71
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_taster
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -349,7 +349,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
349
349
  version: '0'
350
350
  segments:
351
351
  - 0
352
- hash: -4409878318783654556
352
+ hash: 1097102219077556863
353
353
  required_rubygems_version: !ruby/object:Gem::Requirement
354
354
  none: false
355
355
  requirements:
@@ -358,7 +358,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
358
358
  version: '0'
359
359
  segments:
360
360
  - 0
361
- hash: -4409878318783654556
361
+ hash: 1097102219077556863
362
362
  requirements: []
363
363
  rubyforge_project:
364
364
  rubygems_version: 1.8.24