respond 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 +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +2 -0
- data/lib/Messages.rb +156 -0
- data/lib/Respond/version.rb +5 -0
- data/lib/Respond.rb +47 -0
- data/respond.gemspec +22 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dfc720a10ed57fba538c3bc65bdbc75253118012
|
4
|
+
data.tar.gz: 4d5b9f649fa41039943fb7fb16deab20d2404c8d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 678a8088d959f7f9fbcaf304c32c676ef856e66d03aaf4e36644e1238c5926fe4fdde9e30bf59a1335bb9b657cd9cd28b9a6fc010040394fd707a95c3777c51f
|
7
|
+
data.tar.gz: 22cc3313fef2e66fd78ad83983580b0f3866f05d8d40de05902eddcbe996347ee8cca26de878226121e8ab94b34bd1677263b9328f3263f6f9845f927c68c841
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alireza Josheghani
|
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,123 @@
|
|
1
|
+
# Rails Respond
|
2
|
+
|
3
|
+
This package is provided to be used on rails framework and it gives clean methods to handle json response with specific predetermined messages.
|
4
|
+
|
5
|
+
# Requirement
|
6
|
+
* Ruby ^1.2
|
7
|
+
* Rails ^4.2
|
8
|
+
|
9
|
+
**The package is in process.**
|
10
|
+
|
11
|
+
## Install
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'Respond'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install Respond
|
26
|
+
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
You can use these methods in deffernt ways:
|
31
|
+
|
32
|
+
There are hot ones for quick usage, besides some provided to manage outputs on your own way
|
33
|
+
|
34
|
+
require library
|
35
|
+
```ruby
|
36
|
+
require 'respond'
|
37
|
+
```
|
38
|
+
|
39
|
+
create respond instance
|
40
|
+
```ruby
|
41
|
+
respond = Anetwork::Respond.new
|
42
|
+
```
|
43
|
+
|
44
|
+
**Some are shown below:**
|
45
|
+
|
46
|
+
When request succeeds and contains data to return as a result:
|
47
|
+
```ruby
|
48
|
+
respond.succeed(data)
|
49
|
+
```
|
50
|
+
|
51
|
+
When deletion action succeeds:
|
52
|
+
```ruby
|
53
|
+
respond.delete_succeeded
|
54
|
+
```
|
55
|
+
|
56
|
+
When updating succeeds:
|
57
|
+
```ruby
|
58
|
+
respond.update_succeeded
|
59
|
+
```
|
60
|
+
|
61
|
+
When insertion succeeds:
|
62
|
+
```ruby
|
63
|
+
respond.insert_succeeded
|
64
|
+
```
|
65
|
+
|
66
|
+
When deletion action fails:
|
67
|
+
```ruby
|
68
|
+
respond.delete_faild
|
69
|
+
```
|
70
|
+
|
71
|
+
When updating fails:
|
72
|
+
```ruby
|
73
|
+
respond.update_faild
|
74
|
+
```
|
75
|
+
|
76
|
+
when insertion fails:
|
77
|
+
```ruby
|
78
|
+
respond.insert_faild
|
79
|
+
```
|
80
|
+
|
81
|
+
Not Found Error:
|
82
|
+
```ruby
|
83
|
+
respond.not_found
|
84
|
+
```
|
85
|
+
|
86
|
+
When db connetion is refused:
|
87
|
+
```ruby
|
88
|
+
respond.connection_refused
|
89
|
+
```
|
90
|
+
|
91
|
+
When parameters entered are wrong:
|
92
|
+
```ruby
|
93
|
+
respond.wrong_parameters
|
94
|
+
```
|
95
|
+
|
96
|
+
When requested method is not allowed:
|
97
|
+
```ruby
|
98
|
+
respond.method_not_allowed
|
99
|
+
```
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
respond.request_field_notfound
|
103
|
+
```
|
104
|
+
|
105
|
+
Validation errors:
|
106
|
+
``` php
|
107
|
+
respond.validation_errors(data)
|
108
|
+
```
|
109
|
+
|
110
|
+
###customization
|
111
|
+
Set custom message for methods is like this:
|
112
|
+
```ruby
|
113
|
+
respond.not_found('Leave it empty or enter your custom message here')
|
114
|
+
```
|
115
|
+
|
116
|
+
And you can do more:
|
117
|
+
```ruby
|
118
|
+
respond.set_status_code(200).set_status_text('succeed').respond_with_message('Your custom message')
|
119
|
+
```
|
120
|
+
|
121
|
+
## License
|
122
|
+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
123
|
+
|
data/Rakefile
ADDED
data/lib/Messages.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
module Anetwork
|
2
|
+
|
3
|
+
class Messages
|
4
|
+
|
5
|
+
attr_accessor :code , :text , :error , :error_code , :headers , :lang , :config
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@lang = I18n.locale
|
9
|
+
lang = 'en'
|
10
|
+
|
11
|
+
if @lang != '' || @lang != nil
|
12
|
+
lang = @lang
|
13
|
+
end
|
14
|
+
|
15
|
+
@config = eval(File.open(Rails.root.join(
|
16
|
+
'config/respond/errors', "#{lang}.rb"
|
17
|
+
)).read)
|
18
|
+
end
|
19
|
+
|
20
|
+
def succeed(data)
|
21
|
+
self.set_status_code(200)
|
22
|
+
.set_status_text('success')
|
23
|
+
.respond_with_result(data)
|
24
|
+
end
|
25
|
+
|
26
|
+
def delete_succeeded(message = nil)
|
27
|
+
if message == nil
|
28
|
+
message = @config[:success][:delete]
|
29
|
+
end
|
30
|
+
|
31
|
+
self.set_status_code(200)
|
32
|
+
.set_status_text('success')
|
33
|
+
.respond_with_message(message)
|
34
|
+
end
|
35
|
+
|
36
|
+
def update_succeeded(message = nil)
|
37
|
+
if message == nil
|
38
|
+
message = @config[:success][:update]
|
39
|
+
end
|
40
|
+
|
41
|
+
self.set_status_code(200)
|
42
|
+
.set_status_text('success')
|
43
|
+
.respond_with_message(message)
|
44
|
+
end
|
45
|
+
|
46
|
+
def insert_succeeded(message = nil)
|
47
|
+
if message == nil
|
48
|
+
message = @config[:success][:insert]
|
49
|
+
end
|
50
|
+
|
51
|
+
self.set_status_code(200)
|
52
|
+
.set_status_text('success')
|
53
|
+
.respond_with_message(message)
|
54
|
+
end
|
55
|
+
|
56
|
+
def delete_faild(message = nil)
|
57
|
+
if message == nil
|
58
|
+
message = @config[:fail][:delete]
|
59
|
+
end
|
60
|
+
|
61
|
+
self.set_status_code(447)
|
62
|
+
.set_status_text('fail')
|
63
|
+
.set_error_code(5447)
|
64
|
+
.respond_with_message(message)
|
65
|
+
end
|
66
|
+
|
67
|
+
def update_faild(message = nil)
|
68
|
+
if message == nil
|
69
|
+
message = @config[:fail][:update]
|
70
|
+
end
|
71
|
+
|
72
|
+
self.set_status_code(449)
|
73
|
+
.set_status_text('fail')
|
74
|
+
.set_error_code(5449)
|
75
|
+
.respond_with_message(message)
|
76
|
+
end
|
77
|
+
|
78
|
+
def insert_faild(message = nil)
|
79
|
+
if message == nil
|
80
|
+
message = @config[:fail][:insert]
|
81
|
+
end
|
82
|
+
|
83
|
+
self.set_status_code(448)
|
84
|
+
.set_status_text('fail')
|
85
|
+
.set_error_code(5448)
|
86
|
+
.respond_with_message(message)
|
87
|
+
end
|
88
|
+
|
89
|
+
def connection_refused
|
90
|
+
|
91
|
+
self.set_status_code(445)
|
92
|
+
.set_status_text('fail')
|
93
|
+
.set_error_code(5445)
|
94
|
+
.respond_with_message()
|
95
|
+
end
|
96
|
+
|
97
|
+
def not_found
|
98
|
+
|
99
|
+
self.set_status_code(404)
|
100
|
+
.set_status_text('fail')
|
101
|
+
.set_error_code(5404)
|
102
|
+
.respond_with_message
|
103
|
+
end
|
104
|
+
|
105
|
+
def wrong_parameters
|
106
|
+
|
107
|
+
self.set_status_code(406)
|
108
|
+
.set_status_text('fail')
|
109
|
+
.set_error_code(5406)
|
110
|
+
.respond_with_message
|
111
|
+
end
|
112
|
+
|
113
|
+
def method_not_allowed
|
114
|
+
|
115
|
+
self.set_status_code(405)
|
116
|
+
.set_status_text('fail')
|
117
|
+
.set_error_code(5405)
|
118
|
+
.respond_with_message
|
119
|
+
end
|
120
|
+
|
121
|
+
def validation_errors(message = nil)
|
122
|
+
|
123
|
+
self.set_status_code(420)
|
124
|
+
.set_status_text('fail')
|
125
|
+
.set_error_code(5420)
|
126
|
+
.respond_with_message(message)
|
127
|
+
end
|
128
|
+
|
129
|
+
def request_field_notfound
|
130
|
+
|
131
|
+
self.set_status_code(446)
|
132
|
+
.set_status_text('fail')
|
133
|
+
.set_error_code(1001)
|
134
|
+
.respond_with_message
|
135
|
+
end
|
136
|
+
|
137
|
+
def request_field_duplicated
|
138
|
+
|
139
|
+
self.set_status_code(400)
|
140
|
+
.set_status_text('fail')
|
141
|
+
.set_error_code(1004)
|
142
|
+
.respond_with_message
|
143
|
+
end
|
144
|
+
|
145
|
+
def error(code)
|
146
|
+
|
147
|
+
self.set_status_code(400)
|
148
|
+
.set_status_text('fail')
|
149
|
+
.set_error_code(code)
|
150
|
+
.respond_with_message
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
data/lib/Respond.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'Messages'
|
2
|
+
|
3
|
+
module Anetwork
|
4
|
+
|
5
|
+
class Respond < Messages
|
6
|
+
|
7
|
+
def set_status_code(code)
|
8
|
+
@code = code
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
def set_status_text(text)
|
13
|
+
@text = text
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
def respond_with_message(message = nil)
|
18
|
+
result = { :status => @text , :message => message }
|
19
|
+
|
20
|
+
if message == nil
|
21
|
+
result['message'] = @error[:message]
|
22
|
+
end
|
23
|
+
|
24
|
+
if @error_code
|
25
|
+
result['error'] = @error_code
|
26
|
+
end
|
27
|
+
|
28
|
+
result
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_with_result(data)
|
32
|
+
{ :status => @text, :result => data }
|
33
|
+
end
|
34
|
+
|
35
|
+
def set_error_code(error_code)
|
36
|
+
@error_code = error_code
|
37
|
+
@error = @config[error_code]
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_headers(headers)
|
42
|
+
@headers = headers
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/respond.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'Respond/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'respond'
|
8
|
+
spec.version = Anetwork::Respond::VERSION
|
9
|
+
spec.authors = ['Alireza Josheghani']
|
10
|
+
spec.email = ['a.josheghani@anetwork.ir']
|
11
|
+
|
12
|
+
spec.summary = 'Easy peasy json response for ruby on rails!'
|
13
|
+
spec.description = 'This package is provided to be used on rails framework and it gives clean methods to handle json response with specific predetermined messages.'
|
14
|
+
spec.homepage = 'https://github.com/anetwork'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files`.split($/)
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: respond
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alireza Josheghani
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-01 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.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
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
|
+
description: This package is provided to be used on rails framework and it gives clean
|
42
|
+
methods to handle json response with specific predetermined messages.
|
43
|
+
email:
|
44
|
+
- a.josheghani@anetwork.ir
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/Messages.rb
|
55
|
+
- lib/Respond.rb
|
56
|
+
- lib/Respond/version.rb
|
57
|
+
- respond.gemspec
|
58
|
+
homepage: https://github.com/anetwork
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.6.3
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Easy peasy json response for ruby on rails!
|
82
|
+
test_files: []
|