universal_api 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/controllers/concerns/universal_api/controller.rb +109 -0
  5. data/app/controllers/universal_api/application_controller.rb +5 -0
  6. data/app/controllers/universal_api/base_controller.rb +8 -0
  7. data/app/views/universal_api/application/create.json.erb +1 -0
  8. data/app/views/universal_api/application/create.xml.erb +1 -0
  9. data/app/views/universal_api/application/index.json.erb +1 -0
  10. data/app/views/universal_api/application/index.xml.erb +1 -0
  11. data/app/views/universal_api/application/show.json.erb +1 -0
  12. data/app/views/universal_api/application/show.xml.erb +1 -0
  13. data/app/views/universal_api/application/update.json.erb +1 -0
  14. data/app/views/universal_api/application/update.xml.erb +1 -0
  15. data/config/routes.rb +10 -0
  16. data/lib/universal_api.rb +7 -0
  17. data/lib/universal_api/engine.rb +5 -0
  18. data/lib/universal_api/version.rb +3 -0
  19. data/test/dummy/README.rdoc +28 -0
  20. data/test/dummy/Rakefile +6 -0
  21. data/test/dummy/app/controllers/application_controller.rb +5 -0
  22. data/test/dummy/app/models/test_model.rb +3 -0
  23. data/test/dummy/bin/bundle +3 -0
  24. data/test/dummy/bin/rails +4 -0
  25. data/test/dummy/bin/rake +4 -0
  26. data/test/dummy/config.ru +4 -0
  27. data/test/dummy/config/application.rb +23 -0
  28. data/test/dummy/config/boot.rb +5 -0
  29. data/test/dummy/config/database.yml +25 -0
  30. data/test/dummy/config/environment.rb +5 -0
  31. data/test/dummy/config/environments/development.rb +37 -0
  32. data/test/dummy/config/environments/production.rb +78 -0
  33. data/test/dummy/config/environments/test.rb +39 -0
  34. data/test/dummy/config/initializers/assets.rb +8 -0
  35. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  37. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  38. data/test/dummy/config/initializers/inflections.rb +16 -0
  39. data/test/dummy/config/initializers/mime_types.rb +4 -0
  40. data/test/dummy/config/initializers/session_store.rb +3 -0
  41. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  42. data/test/dummy/config/locales/en.yml +23 -0
  43. data/test/dummy/config/routes.rb +4 -0
  44. data/test/dummy/config/secrets.yml +22 -0
  45. data/test/dummy/db/development.sqlite3 +0 -0
  46. data/test/dummy/db/migrate/20160603155052_create_test_table.rb +7 -0
  47. data/test/dummy/db/schema.rb +20 -0
  48. data/test/dummy/db/test.sqlite3 +0 -0
  49. data/test/dummy/log/development.log +11 -0
  50. data/test/dummy/log/test.log +5813 -0
  51. data/test/helpers/api_helper.rb +62 -0
  52. data/test/integration/api_index_test.rb +49 -0
  53. data/test/integration/api_test.rb +86 -0
  54. data/test/test_helper.rb +21 -0
  55. metadata +175 -0
@@ -0,0 +1,62 @@
1
+ require 'nori'
2
+
3
+ module ApiHelper
4
+ CONTENT_TYPES = {
5
+ json: 'application/json',
6
+ xml: 'application/xml'
7
+ }
8
+
9
+ def parse_response_json(response)
10
+ data = ActiveSupport::JSON.decode(response)
11
+
12
+ create_result_obj(data)
13
+ end
14
+
15
+ def parse_response_xml(response)
16
+ parser = Nori.new
17
+ data = parser.parse(response)
18
+
19
+ data && data.length>0 ? create_result_obj(data.to_a[0][1]) : nil
20
+ end
21
+
22
+ def create_result_obj(data)
23
+ columns = []
24
+ rows = []
25
+ if data && ((Array===data && !data.empty?) || (Hash===data && data = [data]))
26
+ data.each do |row|
27
+ row_to_insert = []
28
+ row.each_pair do |column, value|
29
+ columns << column unless columns.include? column
30
+ row_to_insert << value
31
+ end
32
+ rows << row_to_insert
33
+ end
34
+ end
35
+
36
+ { rows: rows, columns: columns, data: data }
37
+ end
38
+
39
+ def check_response_data(res, real_res)
40
+ assert res, res
41
+ assert res[:columns], res
42
+ assert res[:rows], res
43
+
44
+ if res[:columns].length>0
45
+ assert_equal real_res.columns.length, res[:columns].length, print_failure_data(real_res.columns, res[:columns])
46
+ end
47
+ assert_equal real_res.rows.length, res[:rows].length, print_failure_data(real_res.rows, res[:rows])
48
+ res[:columns].each_index do |i|
49
+ assert_equal real_res.columns[i], res[:columns][i], res[:columns]
50
+ end
51
+
52
+ res[:rows].each_index do |i|
53
+ res[:rows][i].each_index do |j|
54
+ assert_equal real_res.rows[i][j].to_s, res[:rows][i][j].to_s
55
+ end
56
+ end
57
+ end
58
+
59
+ def print_failure_data(real, from_api)
60
+ "Real result: #{real}\nApi sent: #{from_api}"
61
+ end
62
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+ require 'helpers/api_helper'
3
+ require 'ransack'
4
+ require 'will_paginate'
5
+
6
+ class ApiIndexTest < ActionDispatch::IntegrationTest
7
+
8
+ include ApiHelper
9
+
10
+ INDEX_TEST_CASES = {
11
+ simple: {
12
+ params: {
13
+ q: {
14
+ name_cont: 'test',
15
+ s: 'name'
16
+ },
17
+ select: ['name'],
18
+ limit: 5
19
+ },
20
+ sql: TestModel.limit(5).order(:name).ransack({ name_cont: 'test' }).result.select('id, name').to_sql
21
+ },
22
+ pagination: {
23
+ params: {
24
+ page: 1,
25
+ q: { s: 'name' }
26
+ },
27
+ sql: TestModel.limit(WillPaginate.per_page).order(:name).to_sql
28
+ }
29
+ }
30
+
31
+ CONTENT_TYPES.each_pair do |content_type, content_type_str|
32
+ INDEX_TEST_CASES.each_pair do |test_case, data|
33
+ define_method("test_#{test_case}_#{content_type}".to_sym) do
34
+ get "/universal_api/TestModel",
35
+ data[:params],
36
+ {
37
+ "Accept" => content_type_str,
38
+ "CONTENT_TYPE" => content_type_str
39
+ }
40
+ assert @response.ok?, @response.inspect
41
+
42
+ response_body = @response.body
43
+ res = send("parse_response_#{content_type}", response_body)
44
+
45
+ check_response_data(res, ActiveRecord::Base.connection.select_all(data[:sql]))
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,86 @@
1
+ require 'test_helper'
2
+ require 'helpers/api_helper'
3
+
4
+ class ApiTest < ActionDispatch::IntegrationTest
5
+
6
+ include ApiHelper
7
+
8
+ CONTENT_TYPES.each_pair do |content_type, content_type_str|
9
+ define_method("test_update_#{content_type}".to_sym) do
10
+ update_content_type_test(content_type, content_type_str)
11
+ end
12
+ define_method("test_create_#{content_type}".to_sym) do
13
+ create_content_type_test(content_type, content_type_str)
14
+ end
15
+ define_method("test_delete_#{content_type}".to_sym) do
16
+ delete_content_type_with_query_test(content_type, content_type_str)
17
+ end
18
+ end
19
+
20
+ def update_content_type_test(content_type, content_type_str)
21
+ test_model = TestModel.create(name: 'test_model')
22
+
23
+ body_obj = {
24
+ name: 'rename_test_model'
25
+ }
26
+
27
+ put "/universal_api/TestModel/#{test_model.id}",
28
+ body_obj.send("to_#{content_type}"),
29
+ {
30
+ "Accept" => content_type_str,
31
+ "CONTENT_TYPE" => content_type_str
32
+ }
33
+ assert @response.ok?, @response.inspect
34
+
35
+ response_body = @response.body
36
+ res = send("parse_response_#{content_type}", response_body)
37
+
38
+ real_res = ActiveRecord::Base.connection.select_all(TestModel.where(id: test_model.id).to_sql)
39
+
40
+ check_response_data(res, real_res)
41
+
42
+ test_model.destroy
43
+ end
44
+
45
+ def create_content_type_test(content_type, content_type_str)
46
+ body_obj = {
47
+ name: 'test_model'
48
+ }
49
+
50
+ post "/universal_api/TestModel",
51
+ body_obj.send("to_#{content_type}"),
52
+ {
53
+ "Accept" => content_type_str,
54
+ "CONTENT_TYPE" => content_type_str
55
+ }
56
+ assert @response.ok?, @response.inspect
57
+
58
+ response_body = @response.body
59
+ res = send("parse_response_#{content_type}", response_body)
60
+
61
+ real_res = ActiveRecord::Base.connection.select_all(TestModel.where(name: body_obj[:name]).to_sql)
62
+
63
+ check_response_data(res, real_res)
64
+
65
+ TestModel.where(name: body_obj[:name]).first.destroy
66
+ end
67
+
68
+ def delete_content_type_with_query_test(content_type, content_type_str)
69
+ test_model = TestModel.create(name: 'test_model')
70
+
71
+ delete "/universal_api/TestModel/#{test_model.id}",
72
+ nil,
73
+ {
74
+ "Accept" => content_type_str,
75
+ "CONTENT_TYPE" => content_type_str
76
+ }
77
+
78
+ assert_equal @response.status, 204, @response.inspect
79
+
80
+ real_res = ActiveRecord::Base.connection.select_all(TestModel.where(id: test_model.id).to_sql)
81
+
82
+ assert_empty real_res, real_res.inspect
83
+
84
+ test_model.destroy
85
+ end
86
+ end
@@ -0,0 +1,21 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../../test/dummy/config/environment.rb", __FILE__)
5
+ ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
6
+ ActiveRecord::Migrator.migrations_paths << File.expand_path('../../db/migrate', __FILE__)
7
+ require "rails/test_help"
8
+
9
+ # Filter out Minitest backtrace while allowing backtrace from other libraries
10
+ # to be shown.
11
+ Minitest.backtrace_filter = Minitest::BacktraceFilter.new
12
+
13
+ # Load support files
14
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
15
+
16
+ # Load fixtures from the engine
17
+ if ActiveSupport::TestCase.method_defined?(:fixture_path=)
18
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
19
+ end
20
+
21
+ require 'dummy/app/models/test_model.rb'
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: universal_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.11
5
+ platform: ruby
6
+ authors:
7
+ - sov-87
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: will_paginate
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: ransack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: UniversalApi gives REST interface for accessing ActiveRecord models
56
+ email:
57
+ - afetisov87@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - Rakefile
64
+ - app/controllers/concerns/universal_api/controller.rb
65
+ - app/controllers/universal_api/application_controller.rb
66
+ - app/controllers/universal_api/base_controller.rb
67
+ - app/views/universal_api/application/create.json.erb
68
+ - app/views/universal_api/application/create.xml.erb
69
+ - app/views/universal_api/application/index.json.erb
70
+ - app/views/universal_api/application/index.xml.erb
71
+ - app/views/universal_api/application/show.json.erb
72
+ - app/views/universal_api/application/show.xml.erb
73
+ - app/views/universal_api/application/update.json.erb
74
+ - app/views/universal_api/application/update.xml.erb
75
+ - config/routes.rb
76
+ - lib/universal_api.rb
77
+ - lib/universal_api/engine.rb
78
+ - lib/universal_api/version.rb
79
+ - test/dummy/README.rdoc
80
+ - test/dummy/Rakefile
81
+ - test/dummy/app/controllers/application_controller.rb
82
+ - test/dummy/app/models/test_model.rb
83
+ - test/dummy/bin/bundle
84
+ - test/dummy/bin/rails
85
+ - test/dummy/bin/rake
86
+ - test/dummy/config.ru
87
+ - test/dummy/config/application.rb
88
+ - test/dummy/config/boot.rb
89
+ - test/dummy/config/database.yml
90
+ - test/dummy/config/environment.rb
91
+ - test/dummy/config/environments/development.rb
92
+ - test/dummy/config/environments/production.rb
93
+ - test/dummy/config/environments/test.rb
94
+ - test/dummy/config/initializers/assets.rb
95
+ - test/dummy/config/initializers/backtrace_silencers.rb
96
+ - test/dummy/config/initializers/cookies_serializer.rb
97
+ - test/dummy/config/initializers/filter_parameter_logging.rb
98
+ - test/dummy/config/initializers/inflections.rb
99
+ - test/dummy/config/initializers/mime_types.rb
100
+ - test/dummy/config/initializers/session_store.rb
101
+ - test/dummy/config/initializers/wrap_parameters.rb
102
+ - test/dummy/config/locales/en.yml
103
+ - test/dummy/config/routes.rb
104
+ - test/dummy/config/secrets.yml
105
+ - test/dummy/db/development.sqlite3
106
+ - test/dummy/db/migrate/20160603155052_create_test_table.rb
107
+ - test/dummy/db/schema.rb
108
+ - test/dummy/db/test.sqlite3
109
+ - test/dummy/log/development.log
110
+ - test/dummy/log/test.log
111
+ - test/helpers/api_helper.rb
112
+ - test/integration/api_index_test.rb
113
+ - test/integration/api_test.rb
114
+ - test/test_helper.rb
115
+ homepage: https://github.com/sov-87/universal_api
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.4.8
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: UniversalApi gives REST interface for accessing ActiveRecord models
139
+ test_files:
140
+ - test/dummy/app/controllers/application_controller.rb
141
+ - test/dummy/app/models/test_model.rb
142
+ - test/dummy/bin/bundle
143
+ - test/dummy/bin/rails
144
+ - test/dummy/bin/rake
145
+ - test/dummy/config/application.rb
146
+ - test/dummy/config/boot.rb
147
+ - test/dummy/config/database.yml
148
+ - test/dummy/config/environment.rb
149
+ - test/dummy/config/environments/development.rb
150
+ - test/dummy/config/environments/production.rb
151
+ - test/dummy/config/environments/test.rb
152
+ - test/dummy/config/initializers/assets.rb
153
+ - test/dummy/config/initializers/backtrace_silencers.rb
154
+ - test/dummy/config/initializers/cookies_serializer.rb
155
+ - test/dummy/config/initializers/filter_parameter_logging.rb
156
+ - test/dummy/config/initializers/inflections.rb
157
+ - test/dummy/config/initializers/mime_types.rb
158
+ - test/dummy/config/initializers/session_store.rb
159
+ - test/dummy/config/initializers/wrap_parameters.rb
160
+ - test/dummy/config/locales/en.yml
161
+ - test/dummy/config/routes.rb
162
+ - test/dummy/config/secrets.yml
163
+ - test/dummy/config.ru
164
+ - test/dummy/db/development.sqlite3
165
+ - test/dummy/db/migrate/20160603155052_create_test_table.rb
166
+ - test/dummy/db/schema.rb
167
+ - test/dummy/db/test.sqlite3
168
+ - test/dummy/log/development.log
169
+ - test/dummy/log/test.log
170
+ - test/dummy/Rakefile
171
+ - test/dummy/README.rdoc
172
+ - test/helpers/api_helper.rb
173
+ - test/integration/api_index_test.rb
174
+ - test/integration/api_test.rb
175
+ - test/test_helper.rb