Module: ClientApi
- Defined in:
- lib/client-api/base.rb,
lib/client-api/request.rb,
lib/client-api/version.rb,
lib/client-api/settings.rb,
lib/client-api/validator.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.1.2".freeze
Class Method Summary collapse
Instance Method Summary collapse
- #base_url ⇒ Object
- #basic_auth ⇒ Object
- #datatype(type, value) ⇒ Object
- #deep_traverse(hash, &block) ⇒ Object
- #headers ⇒ Object
- #is_num?(str) ⇒ Boolean
- #json_output ⇒ Object
- #payload(path) ⇒ Object (also: #schema_from_json)
- #time_out ⇒ Object
- #validate(res, *options) ⇒ Object
- #validate_json(actual, expected) ⇒ Object
- #validate_schema(param1, param2) ⇒ Object
Class Method Details
.configuration ⇒ Object
11 12 13 |
# File 'lib/client-api/settings.rb', line 11 def self.configuration RSpec.configuration end |
.configure ⇒ Object
5 6 7 8 9 |
# File 'lib/client-api/settings.rb', line 5 def self.configure RSpec.configure do |config| yield config end end |
Instance Method Details
#base_url ⇒ Object
15 16 17 |
# File 'lib/client-api/settings.rb', line 15 def base_url ClientApi.configuration.base_url || '' end |
#basic_auth ⇒ Object
23 24 25 |
# File 'lib/client-api/settings.rb', line 23 def basic_auth ClientApi.configuration.basic_auth || '' end |
#datatype(type, value) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/client-api/validator.rb', line 55 def datatype(type, value) if (type.downcase == 'string') || (type.downcase.== 'str') String elsif (type.downcase.== 'integer') || (type.downcase.== 'int') Integer elsif (type.downcase == 'symbol') || (type.downcase == 'sym') Symbol elsif (type.downcase == 'array') || (type.downcase == 'arr') Array elsif (type.downcase == 'object') || (type.downcase == 'obj') Object elsif (type.downcase == 'boolean') || (type.downcase == 'bool') value === true ? TrueClass : FalseClass elsif type.downcase == 'float' Float elsif type.downcase == 'hash' Hash elsif type.downcase == 'complex' Complex elsif type.downcase == 'rational' Rational elsif type.downcase == 'fixnum' Fixnum elsif type.downcase == 'falseclass' FalseClass elsif type.downcase == 'trueclass' TrueClass elsif type.downcase == 'bignum' Bignum else end end |
#deep_traverse(hash, &block) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/client-api/validator.rb', line 161 def deep_traverse(hash, &block) stack = hash.map {|k, v| [[k], v]} while not stack.empty? key, value = stack.pop yield(key, value) if value.is_a? Hash value.each do |k, v| if v.is_a?(String) then if v.empty? then v = nil end end stack.push [key.dup << k, v] end end end end |
#headers ⇒ Object
19 20 21 |
# File 'lib/client-api/settings.rb', line 19 def headers ClientApi.configuration.headers || '' end |
#is_num?(str) ⇒ Boolean
88 89 90 91 92 93 94 |
# File 'lib/client-api/validator.rb', line 88 def is_num?(str) if Float(str) true end rescue ArgumentError, TypeError false end |
#json_output ⇒ Object
27 28 29 |
# File 'lib/client-api/settings.rb', line 27 def json_output ClientApi.configuration.json_output || '' end |
#payload(path) ⇒ Object Also known as: schema_from_json
66 67 68 |
# File 'lib/client-api/base.rb', line 66 def payload(path) JSON.parse(File.read(path)) end |
#time_out ⇒ Object
31 32 33 |
# File 'lib/client-api/settings.rb', line 31 def time_out ClientApi.configuration.time_out || '' end |
#validate(res, *options) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/client-api/validator.rb', line 5 def validate(res, *) .map do |data| raise_error('key (or) operator is not given!') if data[:key].nil? && data[:operator].nil? raise_error('value (or) type is not given!') if data[:value].nil? && data[:type].nil? @resp = res key = data[:key].split("->") key.map do |method| method = method.to_i if is_num?(method) @resp = @resp.send(:[], method) end value ||= data[:value] operator = data[:operator] type = data[:type] if data[:type] || data[:type] != {} || !data[:type].empty? case operator when '=', '==', 'eql?', 'equal', 'equal?' # value validation expect(value).to eq(@resp) if value != nil # datatype validation if (type == "boolean" || type == "bool") && value.nil? expect(%w[TrueClass, FalseClass].any? {|bool| @resp.class.to_s.include? bool}).to be true else expect(datatype(type, value)).to eq(@resp.class) end when '!', '!=', '!eql?', 'not equal', '!equal?' # value validation expect(value).not_to eq(@resp) if value != nil # datatype validation if (type == "boolean" || type == "bool") && value.nil? expect(%w[TrueClass, FalseClass].any? {|bool| @resp.class.to_s.include? bool}).not_to be true else expect(datatype(type, value)).not_to eq(@resp.class) end else raise_error('operator not matching') end end end |
#validate_json(actual, expected) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/client-api/validator.rb', line 96 def validate_json(actual, expected) param1 = JSON.parse(actual.to_json) param2 = JSON.parse(expected.to_json) @actual_key, @actual_value = [], [] deep_traverse(param2) do |path, value| if !value.is_a?(Hash) key_path = path.map! {|k| k} @actual_key << key_path.join("->").to_s @actual_value << value end end Hash[@actual_key.zip(@actual_value)].map do |data| @resp = param1 key = data[0].split("->") key.map do |method| method = method.to_i if is_num?(method) @resp = @resp.send(:[], method) end value = data[1] @assert, @final_assert, @overall = [], [], [] if !value.is_a?(Array) expect(value).to eq(@resp) else @resp.each_with_index do |resp, i| value.to_a.each_with_index do |val1, j| val1.to_a.each_with_index do |val2, k| if resp.to_a.include? val2 @assert << true else @assert << false end end @final_assert << @assert @assert = [] if @resp.count == @final_assert.count @final_assert.each_with_index do |result, i| if result.count(true) == val1.count @overall << true break elsif @final_assert.count == i+1 expect(value).to eq(@resp) end end @final_assert = [] end end end if @overall.count(true) == value.count return else expect(value).to eq(@resp) end end end end |
#validate_schema(param1, param2) ⇒ Object
50 51 52 53 |
# File 'lib/client-api/validator.rb', line 50 def validate_schema(param1, param2) expected_schema = JSON::Validator.validate(param1, param2) expect(expected_schema).to be true end |