mks_common 1.0.10 → 1.1.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
  SHA256:
3
- metadata.gz: 11460c0bab7afad51a7d07e41d2387e2e2b9c1566b7a884905d03b8f2cdf6cae
4
- data.tar.gz: 38efdb7d3138abd76544f620bee2594aaa4115912bbeba728a7c4672d0dae486
3
+ metadata.gz: 8c80194251b33b0e31cac450309e3733bd4823b03e558249aabd5b803e9993ac
4
+ data.tar.gz: cb2bdd5da8b44fe4d2dd20e09532bcc611b7aeb9a63ed8d821950f16c4ff79e2
5
5
  SHA512:
6
- metadata.gz: cf1ba08b0083caaad45f5cb1f19e21de246b357f07bec0604c6fc54a43d31d60de66889e606b4b84240679368835bd0dc0bb38885c20c9ff9cb4005c72b205ff
7
- data.tar.gz: 8ec40cafb335c5866f46b1e895686768b7481eabdc4379c6bbd69e0c156c777299d07bd2a51b05cf5838ce2d174b6b9707effe8496ec866e92b55bfa17a30dbe
6
+ metadata.gz: c70e77cecf3a1d5efe2c6061cb57814e762f7379c5c5dc2d8e3aa145da87296826b1a604fae9a7226fedb6e1988b29d64d9f6a910d4f3887bcbdc6c479e70cd9
7
+ data.tar.gz: 2077e73e9c670add4b400b0b8886a45a520bd34daece364272b2209560fae6e890476b0c2ceced50c4197779797bcacaa4214008be803151173793a0f9298be4
@@ -5,7 +5,7 @@ module Mks
5
5
  class MethodResponse
6
6
  attr_accessor :success, :message, :data, :errors, :total
7
7
 
8
- def initialize(success = nil, message = nil, data = nil, errors = [], total = nil)
8
+ def initialize(success=nil, message=nil, data=nil, errors=[], total=nil)
9
9
  @success = success
10
10
  @message = message
11
11
  @data = data
@@ -13,25 +13,23 @@ module Mks
13
13
  @total = total
14
14
  end
15
15
 
16
- def self.success_response(data, msg = nil)
17
- names = %w(ActiveRecord::Relation ActiveRecord::Associations::CollectionProxy)
18
- return MethodResponse.new(true, msg, nil, nil, nil) if data.nil?
19
- if data.kind_of?(Array) || names.include?(data.class.name)
20
- return MethodResponse.new(true, msg, [], nil, nil) if data.count.zero?
21
- return MethodResponse.new(true, msg, data[0].json(data), nil, data.count)
22
- else
23
- return MethodResponse.new(true, msg, data.json, nil, nil)
24
- end
25
- end
26
-
27
- def self.error_response(data)
28
- errors = data.is_a?(String) ? [data] : data.errors
29
- return MethodResponse.new(false,nil, nil, errors, nil)
16
+ def self.from_json(json_response)
17
+ response = JSON.parse json_response
18
+ success = response['success'] if response.has_key?('success')
19
+ message = response['message'] if response.has_key?('message')
20
+ data = response['data'] if response.has_key?('data')
21
+ errors = response['errors'] if response.has_key?('errors')
22
+ total = response['total'] if response.has_key?('total')
23
+ MethodResponse.new(success, message, data, errors, total)
30
24
  end
31
25
 
32
- def self.failure_response(data)
33
- data.errors.details
26
+ def to_hash
27
+ hash = {}
28
+ self.instance_variables.each do |var|
29
+ hash[var.to_s.delete("@")] = self.instance_variable_get var
30
+ end
31
+ return hash
34
32
  end
35
33
  end
36
34
  end
37
- end
35
+ end
@@ -0,0 +1,9 @@
1
+ module Mks
2
+ module Common
3
+ class Util
4
+ def self.error_messages obj, name=obj.class.name
5
+ obj.errors.full_messages.inject([]) { |list, msg| list << "#{name} #{msg}"}
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  module Mks
2
2
  module Common
3
- VERSION = '1.0.10'.freeze
3
+ VERSION = '1.1.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mks_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.10
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henock L.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-27 00:00:00.000000000 Z
11
+ date: 2020-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -107,6 +107,7 @@ files:
107
107
  - bin/console
108
108
  - bin/setup
109
109
  - lib/mks/common/methodresponse.rb
110
+ - lib/mks/common/util.rb
110
111
  - lib/mks/common/version.rb
111
112
  - lib/mks_common.rb
112
113
  - mks_common.gemspec
@@ -130,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  requirements: []
132
133
  rubyforge_project:
133
- rubygems_version: 2.7.6
134
+ rubygems_version: 2.7.6.2
134
135
  signing_key:
135
136
  specification_version: 4
136
137
  summary: Common reusable classes for MKS