route_soap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/route_soap.rb +155 -0
  3. data/route_soap_spec.rb +58 -0
  4. metadata +124 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dda88b7513c1a4d3f8a56eac537da1b0768fd883
4
+ data.tar.gz: ae5b432be1c4f484b8c0722b6769ead16d05de01
5
+ SHA512:
6
+ metadata.gz: 38aa431651f9653acc1282e18e91b4a6db66d251af02fedcc4dc88fe4c1dadc5e4448ccf1c85902f201eb93707d8acbabeb78bf7ec5e6f506cc9b74500bf09c0
7
+ data.tar.gz: 4c76e28b2e696da0b4381954e7af82cab4909f985990d0204e52e777cda39cc077d6f00da85eaefcfedad1f2e9af431cbc950f7a29e88d8f473f6de435239b75
data/route_soap.rb ADDED
@@ -0,0 +1,155 @@
1
+ require "contracts"
2
+ require "securerandom"
3
+
4
+ module RouteSoap
5
+ DefaultRouter = Rails.application.routes if defined? Rails
6
+
7
+ PathLike = -> (_) do
8
+ _.respond_to?(:required_names) && _.respond_to?(:spec)
9
+ end
10
+
11
+ RouteLike = -> (_) do
12
+ _.respond_to?(:defaults) && _.respond_to?(:path) && _.respond_to?(:verb)
13
+ end
14
+
15
+ RouterLike = -> (_) do
16
+ _.respond_to?(:routes)
17
+ end
18
+
19
+ class Route
20
+ include Contracts
21
+
22
+ BLACKLIST = [
23
+ "/",
24
+ "/prototypes/*id",
25
+ "/rails/info",
26
+ "/rails/info/properties",
27
+ "/rails/info/routes"
28
+ ]
29
+
30
+ VERBS = {
31
+ /^DELETE$/ => :delete,
32
+ /^GET$/ => :get,
33
+ /^PATCH$/ => :patch,
34
+ /^POST$/ => :post,
35
+ /^PUT$/ => :put
36
+ }
37
+
38
+ Contract RouteLike => Route
39
+ def initialize(route)
40
+ @route = route
41
+ self
42
+ end
43
+
44
+ Contract nil => Bool
45
+ def valid?
46
+ action && controller && verb && !blacklisted? && true || false
47
+ end
48
+
49
+ Contract nil => String
50
+ def controller_action
51
+ @controller_action ||= [route.defaults[:controller], route.defaults[:action]].join("#")
52
+ end
53
+
54
+ Contract nil => String
55
+ def path
56
+ @path ||= String(route_path.spec).gsub("(.:format)", "").tap do |path|
57
+ required_params.each do |param, value|
58
+ path.gsub!(":#{param}", value)
59
+ end
60
+ end
61
+ end
62
+
63
+ Contract nil => Hash
64
+ def required_params
65
+ @required_params ||= route_path.required_names.reduce(Hash.new) do |acc, param|
66
+ acc[param] = SecureRandom.hex(3)
67
+ acc
68
+ end
69
+ end
70
+
71
+ Contract nil => Symbol
72
+ def verb
73
+ @verb ||= VERBS[route.verb]
74
+ end
75
+
76
+ private
77
+
78
+ attr_reader :route
79
+
80
+ Contract nil => Or[String, nil]
81
+ def action
82
+ route.defaults[:action]
83
+ end
84
+
85
+ Contract nil => Bool
86
+ def blacklisted?
87
+ BLACKLIST.include?(path)
88
+ end
89
+
90
+ Contract nil => Or[String, nil]
91
+ def controller
92
+ route.defaults[:controller]
93
+ end
94
+
95
+ Contract nil => PathLike
96
+ def route_path
97
+ route.path
98
+ end
99
+ end
100
+
101
+ class Spec
102
+ include Contracts
103
+
104
+ Contract Route => Spec
105
+ def initialize(route)
106
+ @route = route
107
+ self
108
+ end
109
+
110
+ Contract nil => String
111
+ def to_s
112
+ "it { #{ expectation } #{ result } }"
113
+ end
114
+
115
+ private
116
+
117
+ attr_reader :route
118
+
119
+ def expectation
120
+ "expect(#{ route.verb }(\"#{ route.path }\")).to"
121
+ end
122
+
123
+ def result
124
+ "route_to(\"#{ route.controller_action }\", #{ route.required_params })"
125
+ end
126
+ end
127
+
128
+ class Command
129
+ class << self
130
+ include Contracts
131
+
132
+ Contract Or[RouterLike, nil] => nil
133
+ def run!(router = DefaultRouter)
134
+ puts Query.run(router)
135
+ end
136
+ end
137
+ end
138
+
139
+ class Query
140
+ class << self
141
+ include Contracts
142
+
143
+ Contract Or[RouterLike, nil] => Array[String]
144
+ def run(router = DefaultRouter)
145
+ Array.new.tap do |lines|
146
+ router.routes.each do |route|
147
+ Route.new(route).tap do |simple_route|
148
+ lines << String(Spec.new(simple_route)) if simple_route.valid?
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,58 @@
1
+ require "route_soap.rb"
2
+
3
+ describe RouteSoap do
4
+ module RAILS_4_0_3
5
+ class Spec
6
+ def to_s
7
+ "/admin/workspaces/:workspace_id/users/:id(.:format)"
8
+ end
9
+ end
10
+
11
+ class Path
12
+ def required_names
13
+ ["workspace_id", "id"]
14
+ end
15
+
16
+ def spec
17
+ RAILS_4_0_3::Spec.new
18
+ end
19
+ end
20
+
21
+ class Route
22
+ def defaults
23
+ { controller: "admin/users", action: "show" }
24
+ end
25
+
26
+ def path
27
+ RAILS_4_0_3::Path.new
28
+ end
29
+
30
+ def verb
31
+ /^GET$/
32
+ end
33
+ end
34
+
35
+ class Router
36
+ def routes
37
+ [RAILS_4_0_3::Route.new]
38
+ end
39
+ end
40
+ end
41
+
42
+ let(:result) do
43
+ "it { expect(get(\"/admin/workspaces/42/users/42\")).to route_to(\"admin/users#show\", {\"workspace_id\"=>\"42\", \"id\"=>\"42\"}) }"
44
+ end
45
+
46
+ context "when i have router-like object" do
47
+ describe ".run" do
48
+ it "should be possible to transform it into an array specs" do
49
+ stub_hex!
50
+ expect(RouteSoap::Query.run(RAILS_4_0_3::Router.new)).to eq([result])
51
+ end
52
+ end
53
+ end
54
+
55
+ def stub_hex!
56
+ allow(SecureRandom).to receive(:hex).and_return("42")
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: route_soap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Philip Cunningham
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: contracts
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0.4'
76
+ - - <
77
+ - !ruby/object:Gem::Version
78
+ version: '0.6'
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0.4'
86
+ - - <
87
+ - !ruby/object:Gem::Version
88
+ version: '0.6'
89
+ description: Route spec generator
90
+ email:
91
+ - hello@filib.io
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - route_soap.rb
97
+ - route_soap_spec.rb
98
+ homepage: https://github.com/filib/route_soap
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - .
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: 1.9.3
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.0.2
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: A little gem to assist you in keeping your routes clean.
122
+ test_files:
123
+ - route_soap_spec.rb
124
+ has_rdoc: