rails-api-scheme 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a19e950c75598001bced1b34fd7de0947d7e7e7a
4
+ data.tar.gz: 576f8547023807aa677777657fa309eedbf063b9
5
+ SHA512:
6
+ metadata.gz: 009b7a88c50f1dffc3122602f52a7f2a77b7310e4fc58f81e77ad7b6a82825e14bae42443acfba7d82cabd0744d914bbc27f1710b999f543d2a4bb3ec2c01893
7
+ data.tar.gz: 69be1f27d9ea768595a7f828fd6cb3bd51af23f7b9f939b11fc7e60066b5c627158fde452b8b33d8b1b5410b0978b69fabae63e6f53a0f94e30f3f31fe46a035
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ *~
15
+ *.sw*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails-api-scheme.gemspec
4
+ gemspec
@@ -0,0 +1,69 @@
1
+ # Api::Scheme
2
+
3
+ Provides simple error handling and param processing scheme
4
+ to make API and other actions for Rail Action Controller
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rails-api-scheme'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rails-api-scheme
21
+
22
+ ## Usage
23
+
24
+ Do:
25
+
26
+ ```
27
+ require 'api/scheme'
28
+ ```
29
+
30
+ before usage. Then add the similar the following scheme
31
+ into the rails action controller:
32
+
33
+ ```
34
+ class UsersController < ApplicationController
35
+ include Api::Scheme
36
+
37
+ error_map %W(ActionController::ParameterMissing) => 400,
38
+ %W(ActiveRecord::RecordInvalid) => 422..0,
39
+ %W(ActiveRecord::RecordNotUnique) => 422..1
40
+
41
+ param_map({
42
+ user: [
43
+ :first_name,
44
+ :last_name,
45
+ :email
46
+ ]
47
+ })
48
+
49
+ render_error_with :render_error
50
+
51
+ def create
52
+ @user = User.create(permitted_params)
53
+ end
54
+
55
+ def render_error text, minor, major
56
+ render json: { text: text, code: minor }, status: major
57
+ end
58
+ end
59
+ ```
60
+
61
+ ## Development
62
+
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+
69
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails-api-scheme.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails/api/scheme"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,167 @@
1
+ require "api/scheme/version"
2
+
3
+ module Api::Scheme
4
+ def self.included klass
5
+ klass.class_eval %Q"
6
+ class << self
7
+ def error_map map
8
+ @error_map ||= map
9
+ end
10
+
11
+ def model_error_map map
12
+ @model_error_map ||= map
13
+ end
14
+
15
+ def param_map map
16
+ @param_map ||= map
17
+ end
18
+
19
+ def render_error_with method = nil, &prc
20
+ @error_proc ||= method || prc
21
+ end
22
+ end
23
+ "
24
+
25
+ klass.rescue_from StandardError, with: :render_default_error
26
+ end
27
+
28
+ def error_map
29
+ error_map = self.class.instance_variable_get(:@error_map)
30
+
31
+ error_map ||= map
32
+ return error_map if self.class.instance_variable_get(:@error_map_parsed)
33
+
34
+ error_map = parse_error_map
35
+ self.class.instance_variable_set(:@error_map_parsed, true)
36
+ self.class.instance_variable_set(:@error_map, error_map)
37
+ end
38
+
39
+ def parse_error_map
40
+ error_map = self.class.instance_variable_get(:@error_map)
41
+ error_map.map do |(errors, code)|
42
+ list = errors.map do |e|
43
+ begin
44
+ e.constantize
45
+ rescue NameError
46
+ begin
47
+ "#{self.class}::#{e}".constantize
48
+ rescue NameError
49
+ begin
50
+ send(:class_eval, e.camelize)
51
+ rescue NameError
52
+ raise InvalidErrorTypeError, e
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ [ list, code ]
59
+ end.to_h
60
+ end
61
+
62
+ def code_parse code, e
63
+ json = { message: e.message, type: e.class }
64
+
65
+ case code
66
+ when Range
67
+ logger.error "#{e.class}: #{e.message}"
68
+
69
+ [ code.first, json.merge(error_code: code.last) ]
70
+ when NilClass
71
+ logger.error "#{e.class}: #{e.message}\n\t#{e.backtrace[0...50].join("\n\t")}"
72
+
73
+ [ 500, json ]
74
+ else
75
+ logger.error "#{e.class}: #{e.message}"
76
+
77
+ [ code, json ]
78
+ end
79
+ end
80
+
81
+ def get_code_of_model_error_map e
82
+ error_text = e.record.errors.messages.reduce(nil) {|s, (_, v)| s || v.join }
83
+
84
+ model_error_map = self.class.instance_variable_get(:@model_error_map)
85
+ model_error_map.to_a.reverse.reduce(nil) do |code, (re, new_code)|
86
+ re =~ error_text && new_code || code
87
+ end
88
+ end
89
+
90
+ def get_code_of_error_map e
91
+ error_map.find do |errors, codes|
92
+ errors.any? { |error| e.kind_of?(error) }
93
+ end.try(:last)
94
+ end
95
+
96
+ def get_pure_code code
97
+ if code.is_a?(Range)
98
+ code.begin
99
+ else
100
+ code
101
+ end
102
+ end
103
+
104
+ def get_sub_code code
105
+ if code.is_a?(Range)
106
+ code.end
107
+ end
108
+ end
109
+
110
+ def get_code_text code
111
+ path = code.is_a?(Range) && "#{code.begin}.#{code.end}" || code.to_s
112
+
113
+ I18n.t("action_controller.#{controller_path}.errors.#{path}")
114
+ end
115
+
116
+ def render_default_error e
117
+ code =
118
+ if e.to_s.split('::').last == 'Validations'
119
+ get_code_of_model_error_map(e)
120
+ else
121
+ get_code_of_error_map(e)
122
+ end
123
+ rescue => e
124
+ ensure
125
+ error_proc = self.class.instance_variable_get(:@error_proc)
126
+
127
+ case error_proc
128
+ when Proc, Symbol
129
+ prc = error_proc.is_a?(Symbol) && self.method(error_proc) || error_proc
130
+ args = [get_code_text(code), get_sub_code(code), get_pure_code(code) ]
131
+ prc[*args[0...prc.arity.abs]]
132
+ when Symbol
133
+ send(error_proc, get_pure_code(code), get_sub_code(code), get_code_text(code))
134
+ else
135
+ status, json = code_parse(code, e)
136
+
137
+ render data: json, status: status
138
+ end
139
+ end
140
+
141
+ def permitted_params_require key, map
142
+ params.require(key).permit(*map)
143
+ end
144
+
145
+ def permitted_params_permit keys
146
+ default = {}
147
+
148
+ keys.reduce(default) do |h, key|
149
+ value = /(?<name>.*)\?$/ =~ key ? params[name] : params.require(key)
150
+ value.nil? && h || h.merge( (name || key).to_sym => value )
151
+ end
152
+ end
153
+
154
+ def permitted_params
155
+ param_map = self.class.instance_variable_get(:@param_map)
156
+
157
+ if param_map.is_a?(Hash)
158
+ permitted_params_require(param_map.keys.first, param_map.values.first)
159
+ else
160
+ permitted_params_permit([ param_map ].flatten)
161
+ end
162
+ end
163
+
164
+ def validate_access_token
165
+ raise InvalidUserError if not current_user
166
+ end
167
+ end
@@ -0,0 +1,5 @@
1
+ module Api
2
+ module Scheme
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "api/scheme"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-api-scheme"
8
+ spec.version = Api::Scheme::VERSION
9
+ spec.authors = ["Malo Skrylevo"]
10
+ spec.email = ["majioa@yandex.ru"]
11
+
12
+ spec.summary = %q{API Scheme for Rails Action Controller}
13
+ spec.description = %q{Provides simple error handling and param processing scheme
14
+ to make API and other actions for Rail Action Controller}
15
+ spec.homepage = "https://github.com/majioa/rails-api-scheme"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.15"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-api-scheme
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Malo Skrylevo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |-
56
+ Provides simple error handling and param processing scheme
57
+ to make API and other actions for Rail Action Controller
58
+ email:
59
+ - majioa@yandex.ru
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/api/scheme.rb
73
+ - lib/api/scheme/version.rb
74
+ - rails-api-scheme.gemspec
75
+ homepage: https://github.com/majioa/rails-api-scheme
76
+ licenses: []
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.12
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: API Scheme for Rails Action Controller
99
+ test_files: []