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.3".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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/client-api/validator.rb', line 68

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/client-api/validator.rb', line 173

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)


101
102
103
104
105
106
107
# File 'lib/client-api/validator.rb', line 101

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



83
84
85
# File 'lib/client-api/base.rb', line 83

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

def validate(res, *options)
  options.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 = 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] if data[:type] || data[:type] != {} || !data[:type].empty?

    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') && value.nil?
        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 != nil) && (value != 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') && value.nil?
        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 != nil) && (value != 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

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

#validate_json(actual, expected) ⇒ Object



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
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/client-api/validator.rb', line 109

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



63
64
65
66
# File 'lib/client-api/validator.rb', line 63

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