api_versioning 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +131 -0
  3. data/Rakefile +22 -0
  4. data/app/controllers/api_versioning/action_controller_extension.rb +63 -0
  5. data/app/models/api/base_api.rb +39 -0
  6. data/lib/api_versioning.rb +4 -0
  7. data/lib/api_versioning/engine.rb +12 -0
  8. data/lib/api_versioning/version.rb +3 -0
  9. data/test/dummy/README.rdoc +261 -0
  10. data/test/dummy/Rakefile +7 -0
  11. data/test/dummy/app/assets/javascripts/application.js +15 -0
  12. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  13. data/test/dummy/app/controllers/application_controller.rb +3 -0
  14. data/test/dummy/app/controllers/posts_controller.rb +19 -0
  15. data/test/dummy/app/helpers/application_helper.rb +2 -0
  16. data/test/dummy/app/models/api/posts_api.rb +38 -0
  17. data/test/dummy/app/models/post.rb +3 -0
  18. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  19. data/test/dummy/config.ru +4 -0
  20. data/test/dummy/config/application.rb +59 -0
  21. data/test/dummy/config/boot.rb +10 -0
  22. data/test/dummy/config/database.yml +25 -0
  23. data/test/dummy/config/environment.rb +5 -0
  24. data/test/dummy/config/environments/development.rb +37 -0
  25. data/test/dummy/config/environments/production.rb +67 -0
  26. data/test/dummy/config/environments/test.rb +37 -0
  27. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  28. data/test/dummy/config/initializers/inflections.rb +15 -0
  29. data/test/dummy/config/initializers/mime_types.rb +5 -0
  30. data/test/dummy/config/initializers/secret_token.rb +7 -0
  31. data/test/dummy/config/initializers/session_store.rb +8 -0
  32. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  33. data/test/dummy/config/locales/en.yml +5 -0
  34. data/test/dummy/config/routes.rb +4 -0
  35. data/test/dummy/db/development.sqlite3 +0 -0
  36. data/test/dummy/db/migrate/20121119074223_create_posts.rb +11 -0
  37. data/test/dummy/db/schema.rb +24 -0
  38. data/test/dummy/db/test.sqlite3 +0 -0
  39. data/test/dummy/log/development.log +52 -0
  40. data/test/dummy/log/test.log +23544 -0
  41. data/test/dummy/public/404.html +26 -0
  42. data/test/dummy/public/422.html +26 -0
  43. data/test/dummy/public/500.html +25 -0
  44. data/test/dummy/public/favicon.ico +0 -0
  45. data/test/dummy/script/rails +6 -0
  46. data/test/factories/post_factory.rb +9 -0
  47. data/test/integration/posts_integration_test.rb +58 -0
  48. data/test/support/helper_test.rb +5 -0
  49. data/test/support/integration_test.rb +14 -0
  50. data/test/support/unit_test.rb +3 -0
  51. data/test/test_helper.rb +25 -0
  52. data/test/unit/api_versioning_test.rb +49 -0
  53. metadata +269 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,9 @@
1
+ FactoryGirl.define do
2
+
3
+ factory :post do
4
+ name { Forgery::LoremIpsum.words(10) }
5
+ teaser { Forgery::LoremIpsum.words(30) }
6
+ content { Forgery::LoremIpsum.paragraphs(5) }
7
+ end
8
+
9
+ end
@@ -0,0 +1,58 @@
1
+ require "test_helper"
2
+
3
+ describe "Posts Integration" do
4
+
5
+ before do
6
+ Post.destroy_all
7
+ @posts = create_list(:post, 10)
8
+ end
9
+
10
+ after do
11
+ Post.destroy_all
12
+ end
13
+
14
+ it "gets posts API with param=v1" do
15
+ response = get(posts_path(:format => :json, :api_version => 'v1'))
16
+ assert response.ok?
17
+ json = JSON.parse(response.body)
18
+ json['posts'].length.must_equal 10
19
+ json['api_version'].must_equal 'v1'
20
+ json['posts'].length.must_equal 10
21
+ end
22
+
23
+ it "gets posts API with param=v1_1" do
24
+ response = get(posts_path(:format => :json, :api_version => 'v1_1'))
25
+ assert response.ok?
26
+ json = JSON.parse(response.body)
27
+ json['posts'].length.must_equal 10
28
+ json['api_version'].must_equal 'v1_1'
29
+ json['posts'].length.must_equal 10
30
+ end
31
+
32
+ it "gets posts API with empty api_version param" do
33
+ response = get(posts_path(:format => :json))
34
+ assert response.ok?
35
+ json = JSON.parse(response.body)
36
+ json['posts'].length.must_equal 10
37
+ json['api_version'].must_equal 'v1_1'
38
+ json['posts'].length.must_equal 10
39
+ end
40
+
41
+ it "gets posts API with header" do
42
+ response = get(posts_path(:format => :json), {}, { 'HTTP_X_API_VERSION' => 'v1' })
43
+ assert response.ok?
44
+ json = JSON.parse(response.body)
45
+ json['posts'].length.must_equal 10
46
+ json['api_version'].must_equal 'v1'
47
+ json['posts'].length.must_equal 10
48
+ end
49
+
50
+ it "get an API error " do
51
+ response = get(error_path(:format => :json))
52
+ json = JSON.parse(response.body)
53
+ json['status_code'].must_equal 400
54
+ json['status_description'].must_equal 'Bad Request'
55
+ json['message'].must_equal 'Deliberate Error'
56
+ end
57
+
58
+ end
@@ -0,0 +1,5 @@
1
+ class HelperTest < MiniTest::Spec
2
+ include ActiveSupport::Testing::SetupAndTeardown
3
+ include ActionView::TestCase::Behavior
4
+ register_spec_type(/helper$/i, self)
5
+ end
@@ -0,0 +1,14 @@
1
+ class IntegrationTest < MiniTest::Spec
2
+
3
+ include Rack::Test::Methods
4
+ include FactoryGirl::Syntax::Methods
5
+ include Rails.application.routes.url_helpers
6
+ # include ActiveSupport::Testing::SetupAndTeardown
7
+
8
+ def app
9
+ DUMMY_APP
10
+ end
11
+
12
+ register_spec_type(/integration$/i, self)
13
+
14
+ end
@@ -0,0 +1,3 @@
1
+ class MiniTest::Unit::TestCase
2
+ include FactoryGirl::Syntax::Methods
3
+ end
@@ -0,0 +1,25 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+ require 'factory_girl'
7
+ require 'forgery'
8
+ require 'jbuilder'
9
+
10
+ Rails.backtrace_cleaner.remove_silencers!
11
+
12
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
13
+
14
+ # Load fixtures from the engine
15
+ if ActiveSupport::TestCase.method_defined?(:fixture_path=)
16
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
17
+ end
18
+
19
+ DUMMY_APP = Rack::Builder.parse_file('test/dummy/config.ru').first
20
+
21
+ FactoryGirl.find_definitions
22
+
23
+ Turn.config.format = :pretty
24
+ Turn.config.natural = true
25
+
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ describe "API Versioning" do
4
+
5
+ before do
6
+ Post.destroy_all
7
+ @posts = create_list(:post, 10)
8
+ @api = Api::PostsApi.new
9
+ end
10
+
11
+ after do
12
+ Post.destroy_all
13
+ end
14
+
15
+ describe "when creating an API" do
16
+
17
+ it "must create a v1 API if v1 given" do
18
+ api = Api::PostsApi.new('v1')
19
+ api.wont_be_nil
20
+ api.api_version.must_equal :v1
21
+ end
22
+
23
+ it "must create a v1.1 API if v1.1 given" do
24
+ api = Api::PostsApi.new('v1_1')
25
+ api.wont_be_nil
26
+ api.api_version.must_equal :v1_1
27
+ end
28
+
29
+ it "must create a v1.1 API if no version given" do
30
+ api = Api::PostsApi.new('')
31
+ api.wont_be_nil
32
+ api.api_version.must_equal :v1_1
33
+ end
34
+
35
+ it "must create a v1.1 API if a bad version given" do
36
+ api = Api::PostsApi.new('dlk090@!')
37
+ api.wont_be_nil
38
+ api.api_version.must_equal :v1_1
39
+ end
40
+
41
+ end
42
+
43
+ describe "when rendering json" do
44
+ it "must render json" do
45
+ @api.render(@posts).wont_be_nil
46
+ end
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,269 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_versioning
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Craig Sullivan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.9
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.9
30
+ - !ruby/object:Gem::Dependency
31
+ name: jbuilder
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest-rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: turn
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: factory_girl_rails
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: forgery
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: API versioning for rails projects. An extraction from the launch.ly project
143
+ email:
144
+ - craig@melbourne-systems.com
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files: []
148
+ files:
149
+ - app/controllers/api_versioning/action_controller_extension.rb
150
+ - app/models/api/base_api.rb
151
+ - lib/api_versioning/engine.rb
152
+ - lib/api_versioning/version.rb
153
+ - lib/api_versioning.rb
154
+ - MIT-LICENSE
155
+ - Rakefile
156
+ - README.md
157
+ - test/dummy/app/assets/javascripts/application.js
158
+ - test/dummy/app/assets/stylesheets/application.css
159
+ - test/dummy/app/controllers/application_controller.rb
160
+ - test/dummy/app/controllers/posts_controller.rb
161
+ - test/dummy/app/helpers/application_helper.rb
162
+ - test/dummy/app/models/api/posts_api.rb
163
+ - test/dummy/app/models/post.rb
164
+ - test/dummy/app/views/layouts/application.html.erb
165
+ - test/dummy/config/application.rb
166
+ - test/dummy/config/boot.rb
167
+ - test/dummy/config/database.yml
168
+ - test/dummy/config/environment.rb
169
+ - test/dummy/config/environments/development.rb
170
+ - test/dummy/config/environments/production.rb
171
+ - test/dummy/config/environments/test.rb
172
+ - test/dummy/config/initializers/backtrace_silencers.rb
173
+ - test/dummy/config/initializers/inflections.rb
174
+ - test/dummy/config/initializers/mime_types.rb
175
+ - test/dummy/config/initializers/secret_token.rb
176
+ - test/dummy/config/initializers/session_store.rb
177
+ - test/dummy/config/initializers/wrap_parameters.rb
178
+ - test/dummy/config/locales/en.yml
179
+ - test/dummy/config/routes.rb
180
+ - test/dummy/config.ru
181
+ - test/dummy/db/development.sqlite3
182
+ - test/dummy/db/migrate/20121119074223_create_posts.rb
183
+ - test/dummy/db/schema.rb
184
+ - test/dummy/db/test.sqlite3
185
+ - test/dummy/log/development.log
186
+ - test/dummy/log/test.log
187
+ - test/dummy/public/404.html
188
+ - test/dummy/public/422.html
189
+ - test/dummy/public/500.html
190
+ - test/dummy/public/favicon.ico
191
+ - test/dummy/Rakefile
192
+ - test/dummy/README.rdoc
193
+ - test/dummy/script/rails
194
+ - test/factories/post_factory.rb
195
+ - test/integration/posts_integration_test.rb
196
+ - test/support/helper_test.rb
197
+ - test/support/integration_test.rb
198
+ - test/support/unit_test.rb
199
+ - test/test_helper.rb
200
+ - test/unit/api_versioning_test.rb
201
+ homepage: http://launch.ly
202
+ licenses: []
203
+ post_install_message:
204
+ rdoc_options: []
205
+ require_paths:
206
+ - lib
207
+ required_ruby_version: !ruby/object:Gem::Requirement
208
+ none: false
209
+ requirements:
210
+ - - ! '>='
211
+ - !ruby/object:Gem::Version
212
+ version: '0'
213
+ required_rubygems_version: !ruby/object:Gem::Requirement
214
+ none: false
215
+ requirements:
216
+ - - ! '>='
217
+ - !ruby/object:Gem::Version
218
+ version: '0'
219
+ requirements: []
220
+ rubyforge_project:
221
+ rubygems_version: 1.8.24
222
+ signing_key:
223
+ specification_version: 3
224
+ summary: A model based approach to have versioned APIs.
225
+ test_files:
226
+ - test/dummy/app/assets/javascripts/application.js
227
+ - test/dummy/app/assets/stylesheets/application.css
228
+ - test/dummy/app/controllers/application_controller.rb
229
+ - test/dummy/app/controllers/posts_controller.rb
230
+ - test/dummy/app/helpers/application_helper.rb
231
+ - test/dummy/app/models/api/posts_api.rb
232
+ - test/dummy/app/models/post.rb
233
+ - test/dummy/app/views/layouts/application.html.erb
234
+ - test/dummy/config/application.rb
235
+ - test/dummy/config/boot.rb
236
+ - test/dummy/config/database.yml
237
+ - test/dummy/config/environment.rb
238
+ - test/dummy/config/environments/development.rb
239
+ - test/dummy/config/environments/production.rb
240
+ - test/dummy/config/environments/test.rb
241
+ - test/dummy/config/initializers/backtrace_silencers.rb
242
+ - test/dummy/config/initializers/inflections.rb
243
+ - test/dummy/config/initializers/mime_types.rb
244
+ - test/dummy/config/initializers/secret_token.rb
245
+ - test/dummy/config/initializers/session_store.rb
246
+ - test/dummy/config/initializers/wrap_parameters.rb
247
+ - test/dummy/config/locales/en.yml
248
+ - test/dummy/config/routes.rb
249
+ - test/dummy/config.ru
250
+ - test/dummy/db/development.sqlite3
251
+ - test/dummy/db/migrate/20121119074223_create_posts.rb
252
+ - test/dummy/db/schema.rb
253
+ - test/dummy/db/test.sqlite3
254
+ - test/dummy/log/development.log
255
+ - test/dummy/log/test.log
256
+ - test/dummy/public/404.html
257
+ - test/dummy/public/422.html
258
+ - test/dummy/public/500.html
259
+ - test/dummy/public/favicon.ico
260
+ - test/dummy/Rakefile
261
+ - test/dummy/README.rdoc
262
+ - test/dummy/script/rails
263
+ - test/factories/post_factory.rb
264
+ - test/integration/posts_integration_test.rb
265
+ - test/support/helper_test.rb
266
+ - test/support/integration_test.rb
267
+ - test/support/unit_test.rb
268
+ - test/test_helper.rb
269
+ - test/unit/api_versioning_test.rb