exosinatra 0.1.0

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
+ SHA256:
3
+ metadata.gz: 6783b852edab104742cbc434fc613953c1d39a5a3a520c6faa08939c360a4de6
4
+ data.tar.gz: 31f05f2cb2e1a963b1c3ebe4372916e1375d43f3f120ed8e57bbd7fb53d14573
5
+ SHA512:
6
+ metadata.gz: 6f58095393ee0e2afe6ce1c22175f73b423cd04f5831acac0a6ebcfe765188a09229759afdbf6a9c1e07373e5ac39cce909037a530524314cfc460d6294ee319
7
+ data.tar.gz: 53011372ca9dd75841b2076455cca90f9873d0d26a352896981e0308bd945e1d849efb70fef966a95e8fcf42420943ff87a6bf8a49f88c0a5deb00856af94be9
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # ExoSinatra
2
+
3
+ SinatraHelpers: This gem provides helpers for the Sinatra framework in a Ruby project.
4
+
5
+ - JSON responses
6
+
7
+ [reference for gem creation: [newgem-template](https://github.com/wycats/newgem-template)]
8
+
9
+ [reference for git init: [github init](https://gist.github.com/seankross/8830032)]
10
+
11
+
12
+ ## Getting started
13
+
14
+ ### Requirements
15
+
16
+ - Ruby 2.5+
17
+ - Sinatra 2.1.0
@@ -0,0 +1,227 @@
1
+ require 'json'
2
+
3
+ module JsonResponses
4
+ # JSON Responses
5
+
6
+ #
7
+ # resp: response, Hash or Array
8
+ #
9
+ def json(resp)
10
+ headers['Content-Type'] = 'application/json'
11
+
12
+ resp.to_json
13
+ end
14
+
15
+ #
16
+ # code: error code, Int
17
+ # errors: specific errors, String or Array of strings (merged as string -with commas- in output)
18
+ #
19
+ def json_error(code, errors='')
20
+ halt(code,
21
+ {'Content-Type' => 'application/json'},
22
+ {
23
+ :code => code,
24
+ :message => http_status_codes[code],
25
+ :errors => errors.class == Array ? errors.join(', ') : errors
26
+ }.to_json)
27
+ end
28
+
29
+ #
30
+ # code: status code, Int
31
+ # warnings: specific warnings,
32
+ # String or Array of strings (merged as string -with commas- in output)
33
+ #
34
+ def json_warning(code, warnings='')
35
+ [
36
+ code,
37
+ {'Content-Type' => 'application/json'},
38
+ {
39
+ :code => code,
40
+ :message => http_status_codes[code],
41
+ :warnings => warnings.class == Array ? warnings.join(', ') : warnings
42
+ }.to_json
43
+ ]
44
+ end
45
+
46
+ #
47
+ # code: status code, Int
48
+ #
49
+ def json_info(code)
50
+ [
51
+ code,
52
+ {'Content-Type' => 'application/json'},
53
+ {
54
+ :code => code,
55
+ :message => http_status_codes[code]
56
+ }.to_json
57
+ ]
58
+ end
59
+
60
+ #
61
+ # ret: return value, Hash
62
+ #
63
+ def json_return(ret)
64
+ r = ret[:return]
65
+ case r
66
+ when -1
67
+ json_error(400, ret[:errors])
68
+ when -2
69
+ json_error(500, ret[:errors])
70
+ when 0
71
+ json_warning(204, ret[:warnings])
72
+ else
73
+ json(ret[:body])
74
+ end
75
+ end
76
+
77
+ #
78
+ # code: status code, Int
79
+ #
80
+ def status_of(code)
81
+ case code
82
+ when 100..199
83
+ :info
84
+ when 200, 201
85
+ :success_payload
86
+ when 202..399
87
+ :success_no_payload
88
+ when 400..599
89
+ :error
90
+ else
91
+ :other
92
+ end
93
+ end
94
+
95
+ #
96
+ # ret: return value, Hash
97
+ #
98
+ def status_of_http_return(ret)
99
+ status_of(ret[:http_return])
100
+ end
101
+
102
+ #
103
+ # ret: return value, Hash
104
+ #
105
+ def json_http_return(ret)
106
+ r = ret[:http_return]
107
+ status = status_of(ret[:http_return])
108
+ case status
109
+ when :info
110
+ json_info(r)
111
+ when :success_payload
112
+ json(ret[:body])
113
+ when :success_no_payload
114
+ json_warning(r, ret[:warnings])
115
+ when :error
116
+ json_error(r, ret[:errors])
117
+ else
118
+ json(ret)
119
+ end
120
+ end
121
+
122
+ #
123
+ # ret: return value, Hash
124
+ #
125
+ def status_of_result(ret)
126
+ ret[:success]
127
+ end
128
+
129
+ #
130
+ # ret: return value, Hash
131
+ #
132
+ def json_result(ret, err_code)
133
+ r = ret[:success]
134
+ if r
135
+ json(ret)
136
+ else
137
+ json_error(err_code, ret[:errors])
138
+ end
139
+ end
140
+
141
+ #
142
+ # ret: return value, Boolean
143
+ #
144
+ def json_bool(ret)
145
+ json({
146
+ :success => ret
147
+ })
148
+ end
149
+
150
+ #
151
+ # TODO: Error codes & messages list
152
+ #
153
+ # 1xx -> Informational
154
+ # 2xx -> Success
155
+ # 3xx -> Redirection
156
+ # 4xx -> Client Error
157
+ # 5xx -> Server Error
158
+ #
159
+ def http_status_codes
160
+ {
161
+ 100 => 'Continue',
162
+ 101 => 'Switching Protocols',
163
+ 102 => 'Processing',
164
+ 200 => 'OK',
165
+ 201 => 'Created',
166
+ 202 => 'Accepted',
167
+ 203 => 'Non-authoritative Information',
168
+ 204 => 'No Content',
169
+ 205 => 'Reset Content',
170
+ 206 => 'Partial Content',
171
+ 207 => 'Multi-Status',
172
+ 208 => 'Already Reported',
173
+ 226 => 'IM Used',
174
+ 300 => 'Multiple Choices',
175
+ 301 => 'Moved Permanently',
176
+ 302 => 'Found',
177
+ 303 => 'See Other',
178
+ 304 => 'Not Modified',
179
+ 305 => 'Use Proxy',
180
+ 307 => 'Temporary Redirect',
181
+ 308 => 'Permanent Redirect',
182
+ 400 => 'Bad Request',
183
+ 401 => 'Unauthorized',
184
+ 402 => 'Payment Required',
185
+ 403 => 'Forbidden',
186
+ 404 => 'Not Found',
187
+ 405 => 'Method Not Allowed',
188
+ 406 => 'Not Acceptable',
189
+ 407 => 'Proxy Authentication Required',
190
+ 408 => 'Request Timeout',
191
+ 409 => 'Conflict',
192
+ 410 => 'Gone',
193
+ 411 => 'Length Required',
194
+ 412 => 'Precondition Failed',
195
+ 413 => 'Payload Too Large',
196
+ 414 => 'Request-URI Too Long',
197
+ 415 => 'Unsupported Media Type',
198
+ 416 => 'Requested Range Not Satisfiable',
199
+ 417 => 'Expectation Failed',
200
+ 418 => "I'm a teapot",
201
+ 421 => 'Misdirected Request',
202
+ 422 => 'Unprocessable Entity',
203
+ 423 => 'Locked',
204
+ 424 => 'Failed Dependency',
205
+ 426 => 'Upgrade Required',
206
+ 428 => 'Precondition Required',
207
+ 429 => 'Too Many Requests',
208
+ 431 => 'Request Header Fields Too Large',
209
+ 444 => 'Connection Closed Without Response',
210
+ 451 => 'Unavailable For Legal Reasons',
211
+ 499 => 'Client Closed Request',
212
+ 500 => 'Internal Server Error',
213
+ 501 => 'Not Implemented',
214
+ 502 => 'Bad Gateway',
215
+ 503 => 'Service Unavailable',
216
+ 504 => 'Gateway Timeout',
217
+ 505 => 'HTTP Version Not Supported',
218
+ 506 => 'Variant Also Negotiates',
219
+ 507 => 'Insufficient Storage',
220
+ 508 => 'Loop Detected',
221
+ 510 => 'Not Extended',
222
+ 511 => 'Network Authentication Required',
223
+ 599 => 'Network Connect Timeout Error'
224
+ }
225
+ end
226
+
227
+ end
@@ -0,0 +1,3 @@
1
+ module ExoSinatra
2
+ VERSION = "0.1.0"
3
+ end
data/lib/exosinatra.rb ADDED
@@ -0,0 +1 @@
1
+ require 'exosinatra/json_responses'
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exosinatra
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dionysios Kakolyris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: exobasic
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.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.1.0
41
+ description: Exotic Sinatra Helpers
42
+ email:
43
+ - contact@exotic.industries
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/exosinatra.rb
50
+ - lib/exosinatra/json_responses.rb
51
+ - lib/exosinatra/version.rb
52
+ homepage: https://bitbucket.org/vertigoindustries/exotic-sinatra
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: 1.3.6
70
+ requirements: []
71
+ rubygems_version: 3.0.6
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: SinatraHelpers
75
+ test_files: []