jsender 0.1.6 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2acb391e06eb12f23352d1c7b017b09538828282
4
- data.tar.gz: 0253f70375bb46d49e165e0d835162f040714324
3
+ metadata.gz: 902e95a805b56e2a64d7f1dfa6aa7c984009dac2
4
+ data.tar.gz: 34e846f2222977c836dcaaa34b8b1d8076fd5c3d
5
5
  SHA512:
6
- metadata.gz: 99623c31d8a4f30a296db362519d4dd98e818cbbd23f538cf5193741fdb5fd53c9afef5dfc27efc8c8da0a6115a325a6c78d0665b4f43a7374f2181ce7aedc5f
7
- data.tar.gz: fdb7501284928a127dd81f0e076a3c0cd568ec513f19063d6341c4f9c9b731ee8882a8c98047f957f7c95c0b3351e945aeedb146df8d37944a621b82ab282494
6
+ metadata.gz: 70cdc0e0ef0c43ce996b296bade3dd631b9fc55224a7a9a415b876d5e968c447df7e73fa4685b555bb8cc9904f581fd0aa6ef0f85bf4c15b26f3e7f12f0c2c93
7
+ data.tar.gz: 9221522c0aa8157438ea17c02edb017f6831bdb6fad0c648c5dba245c94c6d156adc26ae54cec6b05aaa3042a26329208648d63f585134833843558c2779ed12
data/README.md CHANGED
@@ -32,6 +32,7 @@ Or install it yourself as:
32
32
  iut = CodeClass.new
33
33
  ```
34
34
 
35
+ ### Returns Ruby Hash
35
36
 
36
37
  ```
37
38
  iut.success
@@ -68,6 +69,38 @@ Or install it yourself as:
68
69
  => {"status"=>"fail", "data"=>{"result"=>["d", "a", "t", "a"], "notifications"=>["a failure occurred"]}}
69
70
  ```
70
71
 
72
+ ### Returns JSON
73
+
74
+ ```
75
+ iut.success_json
76
+ => "{\"status\":\"success\", \"data\": null}"
77
+
78
+ iut.succes_json({:key1 => 'value1'})
79
+ => "{\"status\":\"success\",\"data\":{\"key1\":\"value1\"}}"
80
+
81
+ iut.fail_json
82
+ => "{\"status\": \"fail\", \"data\": null}"
83
+
84
+ iut.fail_json({:key1 => "value1"})
85
+ => "{\"status\":\"fail\",\"data\":{\"key1\":\"value1\"}}"
86
+
87
+ iut.error_json
88
+ => ArgumentError, 'Missing required argument message'
89
+
90
+ iut.error_json('My little error')
91
+ => "{\"status\":\"error\", \"message\":\"My little error\"}"
92
+
93
+ iut.error_json('Another little error', 401)
94
+ => "{\"status\":\"error\",\"message\":\"Another little error\",\"code\":401}"
95
+
96
+ iut.error_json('Another little error', 401, {:key1 => 'cause of another little error'})
97
+ => "{\"status\":\"error\",\"message\":\"Another little error\",\"code\":401,\"data\":{\"key1\":\"cause of another little error\"}}"
98
+
99
+ iut.error_json('Another little error', {:key1 => 'cause of another little error'})
100
+ => "{\"status\":\"error\",\"message\":\"Another little error\",\"data\":{\"key1\":\"cause of another little error\"}}"
101
+ ```
102
+
103
+
71
104
  ## Development
72
105
 
73
106
  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.
data/jsender.gemspec CHANGED
@@ -6,12 +6,12 @@ require 'jsender/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "jsender"
8
8
  spec.version = Jsender::VERSION
9
- spec.authors = ["Ernst van Graan"]
10
- spec.email = ["ernst.van.graan@hetzner.co.za"]
9
+ spec.authors = ["Ernst van Graan", "Charles Mulder"]
10
+ spec.email = ["ernst.van.graan@hetzner.co.za", "charles.mulder@hetzner.co.za"]
11
11
 
12
12
  spec.summary = %q{JSender facilitates a simple jsend implementation for ruby}
13
13
  spec.description = %q{JSender facilitates a simple jsend implementation for ruby}
14
- #spec.homepage = "TODO: Put your gem's website or public repo URL here."
14
+ spec.homepage = "https://rubygems.org/gems/jsender"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
@@ -1,3 +1,3 @@
1
1
  module Jsender
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/jsender.rb CHANGED
@@ -1,6 +1,7 @@
1
- require "jsender/version"
1
+ require 'jsender/version'
2
2
 
3
3
  module Jsender
4
+
4
5
  def report(status, message, result = nil)
5
6
  return { 'status' => 'error', 'message' => message } if status == 'error'
6
7
  data = compile_data(result)
@@ -42,6 +43,47 @@ module Jsender
42
43
  result['data']['notifications'].to_s.include?(pattern)
43
44
  end
44
45
 
46
+ ##
47
+ # @param data optional [Hash]
48
+ # @return [String] jsend json
49
+ def success_json(data = nil)
50
+ raise ArgumentError, 'Optional data argument should be of type Hash' if not data.is_a? Hash and not data.nil?
51
+ return JSON.generate({
52
+ :status => 'success',
53
+ :data => data
54
+ })
55
+ end
56
+
57
+ ##
58
+ # @param data optional [Hash]
59
+ # @return [String] jsend json
60
+ def fail_json(data = nil)
61
+ raise ArgumentError, 'Optional data argument should be of type Hash' if not data.is_a? Hash and not data.nil?
62
+ return JSON.generate({
63
+ :status => 'fail',
64
+ :data => data
65
+ })
66
+ end
67
+
68
+ ##
69
+ # @param msg [String]
70
+ # @param code optional [Integer]
71
+ # @param data optional [Hash]
72
+ # @return [String] jsend json
73
+ def error_json(msg, code = nil, data = nil)
74
+ raise ArgumentError, 'Missing required message of type String' if msg.empty? or not msg.is_a? String
75
+ code, data = nil, code if not code.is_a? Integer and code.is_a? Hash and data.nil?
76
+ raise ArgumentError, 'Optional data argument should be of type Hash' if not data.nil? and not data.is_a? Hash
77
+ raise ArgumentError, 'Optional code argument should be of type Integer' if not code.nil? and not code.is_a? Integer
78
+ jsend = {
79
+ :status => 'error',
80
+ :message => msg
81
+ }
82
+ jsend['code'] = code if not code.nil?
83
+ jsend['data'] = data if not data.nil?
84
+ return JSON.generate(jsend)
85
+ end
86
+
45
87
  private
46
88
 
47
89
  def compile_data(result)
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernst van Graan
8
+ - Charles Mulder
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2016-01-25 00:00:00.000000000 Z
12
+ date: 2016-05-26 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -55,6 +56,7 @@ dependencies:
55
56
  description: JSender facilitates a simple jsend implementation for ruby
56
57
  email:
57
58
  - ernst.van.graan@hetzner.co.za
59
+ - charles.mulder@hetzner.co.za
58
60
  executables: []
59
61
  extensions: []
60
62
  extra_rdoc_files: []
@@ -72,7 +74,7 @@ files:
72
74
  - jsender.gemspec
73
75
  - lib/jsender.rb
74
76
  - lib/jsender/version.rb
75
- homepage:
77
+ homepage: https://rubygems.org/gems/jsender
76
78
  licenses:
77
79
  - MIT
78
80
  metadata: {}