vesper-rails 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3e98b2d23c5885f907bc2702d46af805272c5b1f98b4a8fa3f1cb916e7820fb1
4
+ data.tar.gz: 870fccc4984bc95282b61a6032eb8f83b4e4764d4f813c5c2df8af27f808a17e
5
+ SHA512:
6
+ metadata.gz: 9562351a798194fed8f265438afc25ee6f1da03c093db32897d651f739a2d6acbd29df261c42e543afebdb7d07c0ff1b7ecfcf3975866ecfe7eb2a1fc9dbe1b9
7
+ data.tar.gz: 6dfc2308d5e03ea609a000144e9c26b69e278442b4a8c206a580bd03b18250a8552535283cde36ed5e33887a1d28771eaa78b7b3cf89c068e8efdf9376383a71
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## [0.1.0] - 2026-10-18
2
+
3
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Yasumasa Ashida
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Vesper Rails
2
+
3
+ Vesper Rails links Vesper and Ruby on Rails.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ ```bash
10
+ bundle add vesper-rails
11
+ ```
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ ```bash
16
+ gem install vesper-rails
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### REST API
22
+
23
+ ```
24
+ class ApplicationController < Vesper::Api::Controller
25
+ ...
26
+ end
27
+ ```
28
+
29
+ ### other
30
+
31
+ ```
32
+ class ApplicationController < Vesper::Controller
33
+ ...
34
+ end
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/asip/vesper-rails.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Api::Controller
4
+ module Vesper
5
+ module Api
6
+ class Controller < ActionController::API
7
+ include Vesper::Api::Renderable
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Controller
4
+ class Vesper::Controller < ActionController::Base
5
+ include Vesper::Stream::Renderable
6
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Api::Error::Renderable module
4
+ module Vesper
5
+ module Api
6
+ module Error
7
+ module Renderable
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ # Standard Error
12
+ rescue_from StandardError, with: ->(exception) { request.format.json? ? on_internal_server_error(exception) : raise(exception) }
13
+
14
+ # ActiveRecord Error
15
+ rescue_from ActiveRecord::RecordNotFound, with: ->(exception) { request.format.json? ? on_not_found(exception) : raise(exception) }
16
+ rescue_from ActiveRecord::RecordNotUnique, with: ->(exception) { request.format.json? ? on_conflict(exception) : raise(exception) }
17
+ rescue_from ActiveRecord::RecordInvalid, with: ->(exception) { request.format.json? ? on_unprocessable_content(exception) : raise(exception) }
18
+
19
+ # UnauthorizedError
20
+ rescue_from Vesper::Api::UnauthorizedError, with: ->(exception) { request.format.json? ? on_unauthorized(exception) : raise(exception) }
21
+ end
22
+
23
+ protected
24
+
25
+ def render_errors(resource)
26
+ render_resource Vesper::Resources::ErrorMessagesResource.new(Vesper::Models::ErrorMessages.new(errors: error_map(resource))).serialize,
27
+ status: :unprocessable_content
28
+ end
29
+
30
+ private
31
+
32
+ def error_map(resource)
33
+ resource.errors.to_hash(false).with_indifferent_access
34
+ end
35
+
36
+ # Common helper methods for rendering error response (エラーレスポンスをレンダリングする共通のヘルパーメソッド
37
+
38
+ # HTTP Status 400 Bad Request
39
+ def on_bad_request(exception = nil)
40
+ render_error(:bad_request, exception)
41
+ end
42
+
43
+ # HTTP Status 401 Unauthorized
44
+ def on_unauthorized(exception = nil)
45
+ render_error(:unauthorized, exception)
46
+ end
47
+
48
+ # HTTP Status 404 Not Found
49
+ def on_not_found(exception = nil)
50
+ render_error(:not_found, exception)
51
+ end
52
+
53
+ # HTTP Status 409 Conflict
54
+ def on_conflict(exception = nil)
55
+ render_error(:conflict, exception)
56
+ end
57
+
58
+ # HTTP Status 422 Unprocessable Content
59
+ def on_unprocessable_content(exception = nil)
60
+ render_error(:unprocessable_content, exception)
61
+ end
62
+
63
+ # HTTP Status 500 Internal Server Error
64
+ def on_internal_server_error(exception = nil)
65
+ logger.error exception.full_message
66
+ # logger.error exception.backtrace.join("\n") # backtrace
67
+
68
+ render_error(:internal_server_error, exception)
69
+ end
70
+
71
+ def make_error_map(status, exception)
72
+ error = {
73
+ title: status.to_s.titleize,
74
+ detail: exception&.message
75
+ }
76
+
77
+ if status == :not_found
78
+ error[:source] = exception.model
79
+ end
80
+
81
+ error
82
+ end
83
+
84
+ def render_error(status, exception)
85
+ error = make_error_map(status, exception)
86
+
87
+ # puts error
88
+
89
+ render_resource Vesper::Resources::ErrorMapResource.new(Vesper::Models::ErrorMap.new(error)).serialize,
90
+ status:
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Api::Renderable module
4
+ module Vesper
5
+ module Api
6
+ module Renderable
7
+ extend ActiveSupport::Concern
8
+ include Vesper::Api::Error::Renderable
9
+
10
+ protected
11
+
12
+ def render_resource(resource, status: :ok)
13
+ render json: resource, status:
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Custom error class for unauthorized api access (API未認証アクセス用のカスタムエラークラス)
4
+ module Vesper
5
+ module Api
6
+ class UnauthorizedError < StandardError
7
+ def initialize(message = "Authentication information is invalid.")
8
+ super(message)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+
2
+
3
+ # Vesper::Engine class
4
+ module Vesper
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Vesper
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ # Vesper::Model::Errors::API module
2
+ module Vesper
3
+ module Model
4
+ module Errors
5
+ module API
6
+ extend ActiveSupport::Concern
7
+
8
+ def error_map
9
+ self.errors.to_hash(false).with_indifferent_access
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Vesper::Models::ErrorMap class
5
+ #
6
+ module Vesper
7
+ module Models
8
+ class ErrorMap
9
+ include ActiveModel::API
10
+ include ActiveModel::Attributes
11
+
12
+ attribute :title, :string
13
+ attribute :detail, :string
14
+ attribute :source, :string
15
+
16
+ def initialize(attributes)
17
+ super(attributes)
18
+ end
19
+
20
+ def to_h
21
+ attributes.with_indifferent_access
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Vesper::Models::ErrorMessages class
5
+ #
6
+ module Vesper
7
+ module Models
8
+ class ErrorMessages
9
+ include ActiveModel::API
10
+ include ActiveModel::Attributes
11
+
12
+ attribute :errors, :hash, default: {}
13
+
14
+ def initialize(attributes)
15
+ super(attributes)
16
+ end
17
+
18
+ def to_h
19
+ attributes.with_indifferent_access
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Resources::ApplicationResource
4
+ module Vesper
5
+ module Resources
6
+ class ApplicationResource
7
+ include Alba::Resource
8
+ helper Typelizer::DSL
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Resources::ErrorMapResource
4
+ module Vesper
5
+ module Resources
6
+ class ErrorMapResource < Vesper::Resources::ApplicationResource
7
+ attributes :title, :detail
8
+
9
+ attributes :source, if: ->(_error, attribute) { attribute.present? }
10
+
11
+ typelize title: :string, detail: :string, source: "string?"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Resources::ErrorMessagesResource
4
+ module Vesper
5
+ module Resources
6
+ class ErrorMessagesResource < Vesper::Resources::ApplicationResource
7
+ attributes :errors
8
+
9
+ typelize errors: "Record<string, string[]>"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Stream::Error::Renderable module
4
+ module Vesper
5
+ module Stream
6
+ module Error
7
+ module Renderable
8
+ extend ActiveSupport::Concern
9
+
10
+ protected
11
+
12
+ def render_error_stream
13
+ render_stream status: :unprocessable_content
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Vesper::Stream::Renderable module
4
+ module Vesper
5
+ module Stream
6
+ module Renderable
7
+ extend ActiveSupport::Concern
8
+ include Vesper::Stream::Error::Renderable
9
+
10
+ protected
11
+
12
+ def render_stream(status: :ok)
13
+ render layout: false, content_type: "text/vnd.turbo-stream.html", status:
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Vesper::Types::HashType class
5
+ #
6
+ module Vesper
7
+ module Types
8
+ class HashType < ActiveModel::Type::Value
9
+ def cast(value)
10
+ case value
11
+ when Hash then value
12
+ when String then Oj.load(value)
13
+ when NilClass then {}
14
+ else Hash(value)
15
+ end
16
+ end
17
+
18
+ def serialize(value)
19
+ JsonUtil.stringify(Hash(value))
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ ActiveModel::Type.register(:hash, Vesper::Types::HashType)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vesper
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'vesper/api/error/renderable'
4
+ require 'vesper/api/renderable'
5
+ require 'vesper/api/unauthorized_error'
6
+
7
+ require 'vesper/model/errors/a_p_i'
8
+
9
+ require 'vesper/types/hash_type'
10
+
11
+ require 'vesper/models/error_map'
12
+ require 'vesper/models/error_messages'
13
+
14
+ require 'vesper/resources/application_resource'
15
+ require 'vesper/resources/error_map_resource'
16
+ require 'vesper/resources/error_messages_resource'
17
+
18
+ require 'vesper/stream/error/renderable'
19
+ require 'vesper/stream/renderable'
20
+
21
+ require 'vesper/engine'
22
+
23
+ module Vesper
24
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vesper-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yasumasa Ashida
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activemodel
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '7.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: alba
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 4.0.0
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 4.0.0
54
+ - !ruby/object:Gem::Dependency
55
+ name: typelizer
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 0.14.0
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 0.14.0
68
+ email:
69
+ - ys.ashida@gmail.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - CHANGELOG.md
75
+ - LICENSE
76
+ - README.md
77
+ - Rakefile
78
+ - app/controllers/vesper/api/controller.rb
79
+ - app/controllers/vesper/controller.rb
80
+ - lib/vesper-rails.rb
81
+ - lib/vesper/api/error/renderable.rb
82
+ - lib/vesper/api/renderable.rb
83
+ - lib/vesper/api/unauthorized_error.rb
84
+ - lib/vesper/engine.rb
85
+ - lib/vesper/model/errors/a_p_i.rb
86
+ - lib/vesper/models/error_map.rb
87
+ - lib/vesper/models/error_messages.rb
88
+ - lib/vesper/resources/application_resource.rb
89
+ - lib/vesper/resources/error_map_resource.rb
90
+ - lib/vesper/resources/error_messages_resource.rb
91
+ - lib/vesper/stream/error/renderable.rb
92
+ - lib/vesper/stream/renderable.rb
93
+ - lib/vesper/types/hash_type.rb
94
+ - lib/vesper/version.rb
95
+ homepage: https://github.com/asip/vesper-rails
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: 3.2.0
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 4.0.22
114
+ specification_version: 4
115
+ summary: Vesper Rails
116
+ test_files: []