hyperender 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +65 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hyperender.gemspec +36 -0
- data/lib/hyperender.rb +148 -0
- data/lib/hyperender/version.rb +3 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 349e1b66ef23157c3d9e14458cbe53d0742b3fe2
|
4
|
+
data.tar.gz: ccfb20b5ad551b40769fd15392eb4f8d5cc43e6d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5ba3eadf91f11d4729b8f29d77ed818a8695a8161cb273118f1445ebe658439a72ac2687464f264a42355db46c325d43feca4ae3734d9d2d66e84eceadee1ebf
|
7
|
+
data.tar.gz: 11e03fa0ff5e4c90040eaf79f2552ef35529cb0c29da6e0ee9556a2fdb84596fbabdd5604e3a3184979f5e3e1126e89496e0df87a279e632d3d6967c555654fe
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tunnm@topica.edu.vn. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Nguyễn Nhật Minh Tú
|
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,65 @@
|
|
1
|
+
# Hyperender
|
2
|
+
|
3
|
+
HATEOAS-like rendering layout for Rails Applications
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hyperender'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hyperender
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add this line to your application's controller:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
include Hyperender::Action
|
27
|
+
```
|
28
|
+
|
29
|
+
All of the functions below support a variety of functions independent on the arguments passed to it:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
hateoas_data *args # add data for rendering
|
33
|
+
hateoas_error *args # add error for rendering
|
34
|
+
hateoas_params *args # add parameters for rendering
|
35
|
+
```
|
36
|
+
|
37
|
+
And without arguments passed, return variables:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
hateoas_data # return data
|
41
|
+
hateoas_error # return error
|
42
|
+
hateoas_params # return parameters
|
43
|
+
hateoas_message # return message
|
44
|
+
hateoas_status # return status
|
45
|
+
hateoas_request # return request
|
46
|
+
hateoas_render # return the HATEOAS-like JSON value
|
47
|
+
```
|
48
|
+
|
49
|
+
To render HATEOAS-like JSON value:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
hateoas_rendering # render json: hateoas_render
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
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.
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on Edumall Gitlab at https://git.edumall.io/minhtu/hyperender. This project is intended with all TST Team to be a safe, welcoming space for collaboration, and contributors.
|
62
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hyperender"
|
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__)
|
data/bin/setup
ADDED
data/hyperender.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "hyperender/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hyperender"
|
8
|
+
spec.version = Hyperender::VERSION
|
9
|
+
spec.authors = ["Nguyễn Nhật Minh Tú"]
|
10
|
+
spec.email = ["tunnm@topica.edu.vn"]
|
11
|
+
|
12
|
+
spec.summary = "HATEOAS-like rendering layout for Rails Applications"
|
13
|
+
spec.description = "This gem offers the standard hateoas render solution for rails applications"
|
14
|
+
spec.homepage = "https://git.edumall.io/minhtu/hyperender"
|
15
|
+
spec.license = "MIT"
|
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/gems/hyperender"
|
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.16"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rails" ,"~> 4"
|
36
|
+
end
|
data/lib/hyperender.rb
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
require "hyperender/version"
|
2
|
+
|
3
|
+
module Hyperender
|
4
|
+
|
5
|
+
HATEOAS_MSG = {
|
6
|
+
100 => "Continue",
|
7
|
+
101 => "Switching Protocols",
|
8
|
+
200 => "OK",
|
9
|
+
201 => "Created",
|
10
|
+
202 => "Accepted",
|
11
|
+
203 => "Non-Authoritative Information",
|
12
|
+
204 => "No Content",
|
13
|
+
205 => "Reset Content",
|
14
|
+
206 => "Partial Content",
|
15
|
+
300 => "Multiple Choices",
|
16
|
+
301 => "Moved Permanently",
|
17
|
+
302 => "Found",
|
18
|
+
303 => "See Other",
|
19
|
+
304 => "Not Modified",
|
20
|
+
307 => "Temporary Redirect",
|
21
|
+
308 => "Permanent Redirect",
|
22
|
+
400 => "Bad Request",
|
23
|
+
401 => "Unauthorized",
|
24
|
+
403 => "Forbidden",
|
25
|
+
404 => "Not Found",
|
26
|
+
405 => "Method Not Allowed",
|
27
|
+
406 => "Not Acceptable",
|
28
|
+
407 => "Proxy Authentication Required",
|
29
|
+
408 => "Request Timeout",
|
30
|
+
409 => "Conflict",
|
31
|
+
410 => "Gone",
|
32
|
+
411 => "Length Required",
|
33
|
+
412 => "Precondition Failed",
|
34
|
+
413 => "Payload Too Large",
|
35
|
+
414 => "URI Too Long",
|
36
|
+
415 => "Unsupported Media Type",
|
37
|
+
416 => "Range Not Satisfiable",
|
38
|
+
417 => "Expectation Failed",
|
39
|
+
426 => "Upgrade Required",
|
40
|
+
428 => "Precondition Required",
|
41
|
+
429 => "Too Many Requests",
|
42
|
+
431 => "Request Header Fields Too Large",
|
43
|
+
451 => "Unavailable For Legal Reasons",
|
44
|
+
500 => "Internal Server Error",
|
45
|
+
501 => "Not Implemented",
|
46
|
+
502 => "Bad Gateway",
|
47
|
+
503 => "Service Unavailable",
|
48
|
+
504 => "Gateway Timeout",
|
49
|
+
505 => "HTTP Version Not Supported",
|
50
|
+
511 => "Network Authentication Required"
|
51
|
+
}
|
52
|
+
|
53
|
+
def self.render data, errors, parameters, message, status, request
|
54
|
+
{
|
55
|
+
data: (Hyperender.generated_data data, errors),
|
56
|
+
query_parameters: parameters,
|
57
|
+
response: {
|
58
|
+
message: message,
|
59
|
+
status: status
|
60
|
+
},
|
61
|
+
request: {
|
62
|
+
url: request.original_url,
|
63
|
+
method: request.request_method,
|
64
|
+
headers: (Hyperender.generated_headers request),
|
65
|
+
}
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.generated_data data, errors
|
70
|
+
data[:errors] = errors unless errors.blank?
|
71
|
+
data
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.generated_headers request
|
75
|
+
enviroment = [
|
76
|
+
#Server specific variables
|
77
|
+
# "SERVER_SOFTWARE", "SERVER_NAME", "GATEWAY_INTERFACE",
|
78
|
+
#Request specific variables
|
79
|
+
"SERVER_PROTOCOL", "SERVER_PORT", "REQUEST_METHOD", "PATH_INFO", "PATH_TRANSLATED", "SCRIPT_NAME", "QUERY_STRING", "REMOTE_HOST", "REMOTE_ADDR", "AUTH_TYPE", "REMOTE_USER", "REMOTE_IDENT", "CONTENT_TYPE", "CONTENT_LENGTH", "HTTP_USER_AGENT", "X_CUSTOM_HEADER"
|
80
|
+
]
|
81
|
+
headers = {}
|
82
|
+
enviroment.each do |env|
|
83
|
+
attribute = request.headers[env]
|
84
|
+
headers[env.downcase] = attribute unless attribute.blank?
|
85
|
+
end
|
86
|
+
headers
|
87
|
+
end
|
88
|
+
|
89
|
+
module Action
|
90
|
+
def self.included(base)
|
91
|
+
base.class_eval do
|
92
|
+
def hateoas_data *args
|
93
|
+
case args.count
|
94
|
+
when 0 then @hateoas_data_render ||= {}
|
95
|
+
when 1 then @hateoas_data_render = args[0]
|
96
|
+
else @hateoas_data_render = args
|
97
|
+
end
|
98
|
+
@hateoas_data_render
|
99
|
+
end
|
100
|
+
|
101
|
+
def hateoas_error *args
|
102
|
+
case args.count
|
103
|
+
when 0 then @errors ||= {}
|
104
|
+
when 1 then @errors = args[0]
|
105
|
+
else @errors = args
|
106
|
+
end
|
107
|
+
@errors
|
108
|
+
end
|
109
|
+
|
110
|
+
def hateoas_params *args
|
111
|
+
case args.count
|
112
|
+
when 0 then @params ||= request.query_parameters
|
113
|
+
when 1 then @params = args[0]
|
114
|
+
else @params = args
|
115
|
+
end
|
116
|
+
params_class = @params.class.to_s rescue nil
|
117
|
+
unless params_class.in? ["Hash","Array","String"]
|
118
|
+
@params = (@params.as_json rescue (@params.to_s rescue "not renderable"))
|
119
|
+
end
|
120
|
+
@params
|
121
|
+
end
|
122
|
+
|
123
|
+
def hateoas_message
|
124
|
+
@message ||= HATEOAS_MSG[response.status]
|
125
|
+
@message
|
126
|
+
end
|
127
|
+
|
128
|
+
def hateoas_status
|
129
|
+
@status ||= response.status
|
130
|
+
@status
|
131
|
+
end
|
132
|
+
|
133
|
+
def hateoas_request
|
134
|
+
@request ||= request
|
135
|
+
@request
|
136
|
+
end
|
137
|
+
|
138
|
+
def hateoas_render
|
139
|
+
Hyperender.render (hateoas_data), (hateoas_error), (hateoas_params), (hateoas_message), (hateoas_status), (hateoas_request)
|
140
|
+
end
|
141
|
+
|
142
|
+
def hateoas_rendering
|
143
|
+
render json: hateoas_render
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hyperender
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nguyễn Nhật Minh Tú
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-09 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4'
|
55
|
+
description: This gem offers the standard hateoas render solution for rails applications
|
56
|
+
email:
|
57
|
+
- tunnm@topica.edu.vn
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- CODE_OF_CONDUCT.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- hyperender.gemspec
|
71
|
+
- lib/hyperender.rb
|
72
|
+
- lib/hyperender/version.rb
|
73
|
+
homepage: https://git.edumall.io/minhtu/hyperender
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata:
|
77
|
+
allowed_push_host: https://rubygems.org/gems/hyperender
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.6.11
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: HATEOAS-like rendering layout for Rails Applications
|
98
|
+
test_files: []
|