Class: Kontrast::ApiEndpointComparator

Inherits:
Object
  • Object
show all
Defined in:
lib/kontrast/api_endpoint_comparator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApiEndpointComparator

Returns a new instance of ApiEndpointComparator.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kontrast/api_endpoint_comparator.rb', line 8

def initialize

    @prod_client = Kontrast::ApiClient.new(
        'production',
        Kontrast.configuration.production_domain,
        Kontrast.configuration.production_oauth_app_uid,
        Kontrast.configuration.production_oauth_app_secret,
    )

    test_oauth_app = Kontrast.configuration.test_oauth_app_proc.call
    @test_client = Kontrast::ApiClient.new(
        'test',
        Kontrast.configuration.test_domain,
        test_oauth_app.uid,
        test_oauth_app.secret,
    )

    @image_index = 0

    @result = {}

    # This is where failed diffs will be stored
    @diffs = {}
end

Instance Attribute Details

#diffsObject (readonly)

Returns the value of attribute diffs.



6
7
8
# File 'lib/kontrast/api_endpoint_comparator.rb', line 6

def diffs
  @diffs
end

#prod_clientObject (readonly)

Returns the value of attribute prod_client.



6
7
8
# File 'lib/kontrast/api_endpoint_comparator.rb', line 6

def prod_client
  @prod_client
end

#test_clientObject (readonly)

Returns the value of attribute test_client.



6
7
8
# File 'lib/kontrast/api_endpoint_comparator.rb', line 6

def test_client
  @test_client
end

Instance Method Details

#compare(prod_data, test_data, test, key: nil) ⇒ Object



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/kontrast/api_endpoint_comparator.rb', line 58

def compare(prod_data, test_data, test, key: nil)

    if prod_data == test_data
        return true
    elsif is_image_string?(prod_data, key)
        # If it's an image, we need to compare both files
        if compare_images(prod_data, test_data, test)
            return true
        else
            diff_details = { index: @image_index - 1 }
            @diffs[test.to_s][:images] << diff_details
            return false
        end
    elsif prod_data.is_a?(Hash)
        return false if prod_data.keys != test_data.keys

        return prod_data.map do |key, value|
            compare(prod_data[key], test_data[key], test, key: key)
        end.all?
    elsif prod_data.is_a?(Array) # Make it more generic?
        return false if prod_data.length != test_data.length

        return prod_data.map.with_index do |value, i|
            compare(prod_data[i], test_data[i], test)
        end.all?
    else
        return false
    end
end

#compare_images(prod_image, test_image, test) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kontrast/api_endpoint_comparator.rb', line 97

def compare_images(prod_image, test_image, test)
    images = [
        {'env' => 'production', 'image' => prod_image},
        {'env' => 'test', 'image' => test_image},
    ]
    files = Workers.map(images) do |image|
        load_image_file(image['image'], test, image['env'])
    end

    image_helper = Kontrast::ImageHelper.new(files[0].path, files[1].path)

    diff = image_helper.compare(test.to_s, "diff_#{@image_index}.png")

    @image_index += 1
    return diff == 0
end

#diff(test) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kontrast/api_endpoint_comparator.rb', line 33

def diff(test)
    @image_index = 0
    @prod_client.headers = test.headers
    @test_client.headers = test.headers

    # Create the folder
    FileUtils.mkdir_p(File.join(Kontrast.path, test.to_s))

    Workers.map([@test_client, @prod_client]) do |client|
        client.fetch(test.path, save_file: true, folder_name: test.to_s)
    end

    @diffs[test.to_s] = {images: []}
    if !compare(@prod_client.responses[test.path], @test_client.responses[test.path], test)
        @diffs[test.to_s].merge!({
            type: 'api_endpoint',
            name: test.name,
            diff: 1,
        })
    else
        # Clear the diff
        @diffs.delete test.to_s
    end
end

#is_image_string?(image_string, key) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
# File 'lib/kontrast/api_endpoint_comparator.rb', line 88

def is_image_string?(image_string, key)
    # Either a URL or a local path
    if !key.nil? && key != ''
        return key.match(/(image|url)/) && image_string.is_a?(String)
    else
        return image_string.is_a?(String) && image_string.match(/^(http|\/)/)
    end
end

#load_image_file(image, test, prefix) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/kontrast/api_endpoint_comparator.rb', line 114

def load_image_file(image, test, prefix)
    if image.start_with?('http')
        extension = image.split('.')[-1]
        file_name = "#{prefix}_#{@image_index}.#{extension}"
        open(File.join(Kontrast.path, test.to_s, file_name), 'wb') do |file|
            file << open(image).read
        end
    else
        File.new(image)
    end
end