beyond_the_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 191662efe7dc972b83e5ca4ad9f5253093b3b7be
4
+ data.tar.gz: 4c5c7a8cd7aaebef6f6a6d994ae70138f0b6d40d
5
+ SHA512:
6
+ metadata.gz: d4f3ea8b6bce79cd51756b22c3719aea00e033fd44585a9338d74b88f535cff5f4ecd6988b37b50348e9856967c182f8cbff61459bd46eb7fdd4db735d1172e1
7
+ data.tar.gz: c6198f9a81eb69ef1efbaea793368a8819c47af42f7df653bfa02fb926364f0b6ef9d9894c3819ba89ff4f279d9a391b0b2cc4c61bc047e454c681b4f4c2af8f
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore all logfiles and tempfiles.
11
+ /log/*
12
+ /tmp/*
13
+ !/log/.keep
14
+ !/tmp/.keep
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,60 @@
1
+ AllCops:
2
+ DisabledByDefault: false
3
+ TargetRubyVersion: 2.3
4
+ Exclude:
5
+ - db/migrate/*
6
+ - db/schema.rb
7
+ - lib/generators/**/*
8
+ - lib/rails/generators/**/*
9
+ - lib/populate/**/*
10
+ - bin/*
11
+ Rails:
12
+ Enabled: true
13
+ Metrics/LineLength:
14
+ Max: 100
15
+ Metrics/ClassLength:
16
+ CountComments: false
17
+ Max: 100
18
+ Exclude:
19
+ - test/**/*
20
+ Metrics/ClassLength:
21
+ CountComments: false
22
+ Max: 300
23
+ Include:
24
+ - test/**/*
25
+ Metrics/BlockLength:
26
+ Enabled: true
27
+ Exclude:
28
+ - test/**/*
29
+ Metrics/BlockLength:
30
+ Enabled: false
31
+ Include:
32
+ - test/**/*
33
+ Style/PredicateName:
34
+ Enabled: true
35
+ Exclude:
36
+ - test/support/shoulda/**/*
37
+ Style/PredicateName:
38
+ Enabled: false
39
+ Include:
40
+ - test/support/shoulda/**/*
41
+ Style/AsciiComments:
42
+ Enabled: false
43
+ Style/StringLiteralsInInterpolation:
44
+ Enabled: false
45
+ Style/Documentation:
46
+ Enabled: false
47
+ Style/EmptyLinesAroundClassBody:
48
+ Enabled: false
49
+ Style/ClassAndModuleChildren:
50
+ Enabled: false
51
+ Style/ModuleFunction:
52
+ Enabled: false
53
+ Style/EmptyLinesAroundModuleBody:
54
+ Enabled: false
55
+ Style/StringLiterals:
56
+ Enabled: false
57
+ Style/WordArray:
58
+ Enabled: false
59
+ Style/FrozenStringLiteralComment:
60
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 William Weckl
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # Beyond The API Test Helpers
2
+
3
+ Beyond The API is a framework created to standardize Rails APIs and make things easier.
4
+
5
+ This gem consist in various render helpers to help render JSON on common use cases like pagination and sort.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'beyond_the_api', '~> 0.0.1'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ ## Installation
20
+
21
+ Include in your application_controller.rb or whatever controller you want to use the helpers:
22
+ ```ruby
23
+ include BeyondTheApi::RenderHelpers
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ #### Base
29
+
30
+ * json patterns
31
+ * meta
32
+ * model serializer helper
33
+ * current_user
34
+ * versioning
35
+
36
+ #### Index renders
37
+
38
+ * total_count
39
+ * total_pages
40
+ * pagination
41
+ * filters
42
+ * sort
43
+
44
+ #### Show renders
45
+
46
+ TODO
47
+
48
+ #### Common renders
49
+
50
+ TODO
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/williamweckl/beyond_the_api.
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'beyond_the_api/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'beyond_the_api'
7
+ spec.version = BeyondTheApi::VERSION
8
+ spec.authors = ['William Weckl']
9
+ spec.email = ['william.weckl@gmail.com']
10
+
11
+ spec.summary = 'API patterns over Rails API.'
12
+ spec.description = 'API patterns over Rails API.'
13
+ spec.homepage = 'http://rubygems.org/gems/beyond_the_api'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'exe'
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'active_model_serializers', '~> 0.10.3'
22
+ spec.add_dependency 'activesupport', '>= 4.2', '< 5.1'
23
+ spec.add_dependency 'will_paginate', '~> 3.1.5'
24
+ spec.add_dependency 'versionist', '~> 1.5.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.11'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ end
data/config.reek ADDED
@@ -0,0 +1,63 @@
1
+ ---
2
+ ### Generic smell configuration
3
+
4
+ IrresponsibleModule:
5
+ enabled: false
6
+ UnusedPrivateMethod:
7
+ enabled: false
8
+ NilCheck:
9
+ enabled: false
10
+
11
+ "app/controllers":
12
+ TooManyStatements:
13
+ max_statements: 10
14
+ NestedIterators:
15
+ max_allowed_nesting: 2
16
+ UnusedPrivateMethod:
17
+ enabled: false
18
+ UncommunicativeModuleName:
19
+ enabled: false
20
+ InstanceVariableAssumption:
21
+ enabled: false
22
+ ManualDispatch:
23
+ enabled: false
24
+ "app/helpers":
25
+ UtilityFunction:
26
+ enabled: false
27
+ FeatureEnvy:
28
+ enabled: false
29
+ ManualDispatch:
30
+ enabled: false
31
+ "app/models":
32
+ Attribute:
33
+ enabled: false
34
+ "app/serializer":
35
+ RepeatedConditional:
36
+ enabled: false
37
+ "test/support":
38
+ UtilityFunction:
39
+ enabled: false
40
+ FeatureEnvy:
41
+ enabled: false
42
+ TooManyStatements:
43
+ max_statements: 10
44
+ InstanceVariableAssumption:
45
+ enabled: false
46
+ TooManyMethods:
47
+ max_methods: 20
48
+ ManualDispatch:
49
+ enabled: false
50
+ DataClump:
51
+ enabled: false
52
+ "test/requests":
53
+ UncommunicativeModuleName:
54
+ enabled: false
55
+
56
+ ### Excluding directories
57
+
58
+ # Directories below will not be scanned at all
59
+ exclude_paths:
60
+ - db/migrate
61
+ - lib/generators
62
+ - lib/rails/generators
63
+ - lib/populate
@@ -0,0 +1,85 @@
1
+ require 'will_paginate/array'
2
+ module BeyondTheApi
3
+ module IndexRenderHelpers
4
+ API_DEFAULT_PAGE_SIZE = 10
5
+
6
+ protected
7
+
8
+ ## Index ##
9
+
10
+ def render_json_list(list, options = {})
11
+ @list = list
12
+ @list = paginate(options[:default_page_size] || API_DEFAULT_PAGE_SIZE)
13
+ @meta[:total_pages] = (options[:total_pages] || @list.total_pages) if total_pages?
14
+ @meta[:total_count] ||= (options[:total_count] || @list.count) if total_count?
15
+
16
+ render index_params_to_render(options)
17
+ end
18
+
19
+ def index_params_to_render(options)
20
+ params_to_render = { json: @list, meta: @meta, current_user: current_user }
21
+ serializer = options[:serializer]
22
+ params_to_render[:each_serializer] = serializer if serializer
23
+ params_to_render
24
+ end
25
+
26
+ def render_json_list_enum(list, name, options = {})
27
+ @list = list
28
+ @list = paginate_array(options[:default_page_size] || API_DEFAULT_PAGE_SIZE)
29
+ @meta[:total_count] = @list.count if total_count?
30
+ @meta[:total_pages] = @list.total_pages if total_pages?
31
+ render json: {
32
+ name => @list,
33
+ meta: @meta
34
+ }
35
+ end
36
+
37
+ def paginate(default)
38
+ return @list unless paginate?(default)
39
+ @list.page(page).per_page(params[:per_page] || default)
40
+ end
41
+
42
+ def paginate_array(default)
43
+ return @list unless paginate?(default)
44
+ @list.paginate(page: page, per_page: params[:per_page] || default)
45
+ end
46
+
47
+ def apply_filters(list, *scopes)
48
+ scopes.each do |scope|
49
+ param_name = scope.to_s.sub('by_', '')
50
+ param = params[param_name]
51
+ list = list.send(scope, param) if param.present? && list.respond_to?(scope)
52
+ end
53
+ list
54
+ end
55
+
56
+ def enum_list_to_array_hashes(list)
57
+ array = []
58
+ list.each do |name, id|
59
+ array << {
60
+ id: id,
61
+ name: name
62
+ }
63
+ end
64
+ array
65
+ end
66
+
67
+ private
68
+
69
+ def total_pages?
70
+ @list.respond_to?(:total_pages) && params[:total_pages].to_s == 'true'
71
+ end
72
+
73
+ def total_count?
74
+ params[:total_count].to_s == 'true'
75
+ end
76
+
77
+ def page
78
+ params[:page] || 1
79
+ end
80
+
81
+ def paginate?(default)
82
+ params[:per_page] || !default == :unlimited
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,46 @@
1
+ module BeyondTheApi
2
+ module RenderHelpers
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include ::BeyondTheApi::IndexRenderHelpers
7
+ include ::BeyondTheApi::ShowRenderHelpers
8
+ before_action :set_meta
9
+ end
10
+
11
+ def render_json_message_with_serializer(object, options = {})
12
+ hash = serialized_object_with_root(object, options[:serializer_root])
13
+ hash.merge!(options[:message])
14
+ hash[:meta] = @meta
15
+ render json: hash, status: (options[:status] || :ok)
16
+ end
17
+
18
+ def render_json_message(hash, status, optionals = {})
19
+ hash = hash.merge(meta: @meta)
20
+ hash = hash.merge(optionals)
21
+ render json: hash, status: status
22
+ end
23
+
24
+ def render_json_errors(object)
25
+ errors = object.errors
26
+ render json: { errors: errors.messages,
27
+ full_error_messages: errors.full_messages, meta: @meta }, status: 422
28
+ end
29
+
30
+ private
31
+
32
+ def set_meta
33
+ @meta ||= {}
34
+ @meta[:version] = params[:controller].split('/').first
35
+ @meta[:now] = Time.current
36
+ end
37
+
38
+ def serialized_object_with_root(object, root)
39
+ options = {}
40
+ options[:current_user] = current_user if respond_to?(:current_user)
41
+
42
+ root ||= object.class.to_s.underscore
43
+ { root => object.serialize_to_json(options) }
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ module BeyondTheApi
2
+ module ShowRenderHelpers
3
+ def render_json_serializer(object, options = {})
4
+ render_params = { json: object, current_user: current_user, meta: @meta,
5
+ status: (options[:status] || :ok) }
6
+
7
+ render render_params.merge(render_json_serializer_aditional_params(options))
8
+ end
9
+
10
+ def render_json_serializer_aditional_params(options)
11
+ render_params = {}
12
+ render_params[:root] = options[:root].presence
13
+ render_params[:serializer] = options[:serializer].presence
14
+ render_params.delete_if { |_key, value| !value }
15
+ end
16
+
17
+ def render_json_object(object, name)
18
+ render json: {
19
+ name => object,
20
+ meta: @meta
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module BeyondTheApi
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_model_serializers'
2
+ require 'will_paginate'
3
+ require 'versionist'
4
+ require 'beyond_the_api/render_helpers'
5
+ require 'beyond_the_api/index_render_helpers'
6
+ require 'beyond_the_api/show_render_helpers'
7
+
8
+ ActiveRecord::Base.class_eval do
9
+ def serialize_to_json(serializer_options = {})
10
+ serialization = ActiveModelSerializers::SerializableResource.new(self,
11
+ serializer_options)
12
+ JSON.parse(serialization.to_json).values[0]
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beyond_the_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - William Weckl
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_model_serializers
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '4.2'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.1'
37
+ type: :runtime
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '4.2'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: will_paginate
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 3.1.5
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 3.1.5
61
+ - !ruby/object:Gem::Dependency
62
+ name: versionist
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 1.5.0
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: 1.5.0
75
+ - !ruby/object:Gem::Dependency
76
+ name: bundler
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.11'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.11'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rake
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '10.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '10.0'
103
+ description: API patterns over Rails API.
104
+ email:
105
+ - william.weckl@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rubocop.yml"
112
+ - ".ruby-version"
113
+ - LICENSE.txt
114
+ - README.md
115
+ - beyond_the_api.gemspec
116
+ - config.reek
117
+ - lib/beyond_the_api.rb
118
+ - lib/beyond_the_api/index_render_helpers.rb
119
+ - lib/beyond_the_api/render_helpers.rb
120
+ - lib/beyond_the_api/show_render_helpers.rb
121
+ - lib/beyond_the_api/version.rb
122
+ homepage: http://rubygems.org/gems/beyond_the_api
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.6.7
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: API patterns over Rails API.
146
+ test_files: []