Module: ClientApi

Included in:
Api, Request
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

Classes: Api, Request

Constant Summary collapse

VERSION =
"0.2.5".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configurationObject


9
10
11
# File 'lib/client-api/settings.rb', line 9

def self.configuration
  RSpec.configuration
end

.configureObject


3
4
5
6
7
# File 'lib/client-api/settings.rb', line 3

def self.configure
  RSpec.configure do |config|
    yield config
  end
end

Instance Method Details

#base_urlObject


13
14
15
# File 'lib/client-api/settings.rb', line 13

def base_url
  ClientApi.configuration.base_url || ''
end

#basic_authObject


21
22
23
# File 'lib/client-api/settings.rb', line 21

def basic_auth
  ClientApi.configuration.basic_auth || ''
end

#datatype(type, value) ⇒ Object


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
# File 'lib/client-api/validator.rb', line 116

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 == 'falseclass') || (type.downcase == 'false')
    FalseClass
  elsif (type.downcase == 'trueclass') || (type.downcase == 'true')
    TrueClass
  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 == 'bignum'
    Bignum
  else
  end
end

#deep_traverse(hash, &block) ⇒ Object


221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/client-api/validator.rb', line 221

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

#headersObject


17
18
19
# File 'lib/client-api/settings.rb', line 17

def headers
  ClientApi.configuration.headers || ''
end

#is_num?(str) ⇒ Boolean

Returns:

  • (Boolean)

149
150
151
152
153
154
155
# File 'lib/client-api/validator.rb', line 149

def is_num?(str)
  if Float(str)
    true
  end
rescue ArgumentError, TypeError
  false
end

#json_outputObject


25
26
27
# File 'lib/client-api/settings.rb', line 25

def json_output
  ClientApi.configuration.json_output || ''
end

#payload(path) ⇒ Object Also known as: schema_from_json


92
93
94
# File 'lib/client-api/base.rb', line 92

def payload(path)
  JSON.parse(File.read(path))
end

#time_outObject


29
30
31
# File 'lib/client-api/settings.rb', line 29

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
49
50
51
52
53
54
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
# File 'lib/client-api/validator.rb', line 5

def validate(res, *options)
  options.map do |data|
    raise('key is not given!') if data[:key].nil?
    raise('operator is not given!') if data[:operator].nil?
    raise('value (or) type is not given!') if data[:value].nil? && data[:type].nil?

    @resp = JSON.parse(res.to_json)
    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]

    case operator
    when '=', '==', 'eql?', 'equal', 'equal?'
      # value validation
      expect(value).to eq(@resp), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  didn't match \n[value]: \"#{data[:value]}\"\n"} if value != nil

      # datatype validation
      if (type == 'boolean') || (type == 'bool')
        expect(%w[TrueClass, FalseClass].any? {|bool| bool.include? @resp.class.to_s}).to eq(true), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      elsif ((type != 'boolean') || (type != 'bool')) && (type != nil)
        expect(datatype(type, value)).to eq(@resp.class), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      end

    when '!', '!=', '!eql?', 'not equal', '!equal?'
      # value validation
      expect(value).not_to eq(@resp), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  didn't match \n[value]: \"#{data[:value]}\"\n"} if value != nil

      # datatype validation
      if (type == 'boolean') || (type == 'bool')
        expect(%w[TrueClass, FalseClass].any? {|bool| bool.include? @resp.class.to_s}).not_to eq(true), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      elsif ((type != 'boolean') || (type != 'bool')) && (type != nil)
        expect(datatype(type, value)).not_to eq(@resp.class), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      end

    when '>', '>=', '<', '<=', 'greater than', 'greater than or equal to', 'less than', 'less than or equal to', 'lesser than', 'lesser than or equal to'
      message = 'is not greater than (or) equal to' if operator == '>=' || operator == 'greater than or equal to'
      message = 'is not greater than' if operator == '>' || operator == 'greater than'
      message = 'is not lesser than' if operator == '<=' || operator == 'less than or equal to'
      message = 'is not lesser than (or) equal to' if operator == '<' || operator == 'less than' || operator == 'lesser than'

      # value validation
      expect(@resp.to_i.public_send(operator, value)).to eq(true), "[key]: \"#{data[:key]}\"".blue + "\n  #{message} \n[value]: \"#{data[:value]}\"\n" if value != nil

      # datatype validation
      expect(datatype(type, value)).to eq(@resp.class), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"} if type != nil

    when 'contains', 'has', 'contains?', 'has?', 'include', 'include?'
      # value validation
      expect(@resp.to_s).to include(value.to_s), lambda {"[key]: \"#{data[:key]} => #{@resp}\"".blue + "\n  not contains \n[value]: \"#{data[:value]}\"\n"} if value != nil

      # datatype validation
      if (type == 'boolean') || (type == 'bool')
        expect(%w[TrueClass, FalseClass].any? {|bool| bool.include? @resp.class.to_s}).to eq(true), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      elsif ((type != 'boolean') || (type != 'bool')) && (type != nil)
        expect(datatype(type, value)).to eq(@resp.class), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      end

    when 'not contains', '!contains', 'not include', '!include'
      # value validation
      expect(@resp.to_s).not_to include(value.to_s), lambda {"[key]: \"#{data[:key]} => #{@resp}\"".blue + "\n  should not contain \n[value]: \"#{data[:value]}\"\n"} if value != nil

      # datatype validation
      if (type == 'boolean') || (type == 'bool')
        expect(%w[TrueClass, FalseClass].any? {|bool| bool.include? @resp.class.to_s}).not_to eq(true), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      elsif ((type != 'boolean') || (type != 'bool')) && (type != nil)
        expect(datatype(type, value)).not_to eq(@resp.class), lambda {"[key]: \"#{data[:key]}\"".blue + "\n  datatype shouldn't be \n[type]: \"#{data[:type]}\"\n"}
      end

    else
      raise('operator not matching')
    end
  end
end

#validate_headers(res, *options) ⇒ Object


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/client-api/validator.rb', line 91

def validate_headers(res, *options)
  options.map do |data|
    raise('key is not given!') if data[:key].nil?
    raise('operator is not given!') if data[:operator].nil?
    raise('value is not given!') if data[:value].nil?

    @resp_header = JSON.parse(res.to_json)

    key = data[:key]
    value = data[:value]
    operator = data[:operator]

    case operator
    when '=', '==', 'eql?', 'equal', 'equal?'
      expect(value).to eq(@resp_header[key][0]), lambda {"\"#{key}\" => \"#{value}\"".blue + "\n  didn't match \n\"#{key}\" => \"#{@resp_header[key][0]}\"\n"}

    when '!', '!=', '!eql?', 'not equal', '!equal?'
      expect(value).not_to eq(@resp_header[key][0]), lambda {"\"#{key}\" => \"#{value}\"".blue + "\n  shouldn't match \n\"#{key}\" => \"#{@resp_header[key][0]}\"\n"}

    else
      raise('operator not matching')
    end
  end
end

#validate_json(actual, expected) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/client-api/validator.rb', line 157

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
      else
        expect(value).to eq(@resp)
      end

    end
  end
end

#validate_schema(param1, param2) ⇒ Object


86
87
88
89
# File 'lib/client-api/validator.rb', line 86

def validate_schema(param1, param2)
  expected_schema = JSON::Validator.validate(param1, param2)
  expect(expected_schema).to eq(true)
end