File: add-field.pl

package info (click to toggle)
libhttp-browserdetect-perl 3.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,220 kB
  • sloc: perl: 3,083; makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,845 bytes parent folder | download | duplicates (2)
1
2
3
4
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
#!/usr/bin/perl -w

# Script for adding fields to existing regression tests
#
# Recommended use:
#
# $ perl -I../lib add-field.pl useragents.json > new-useragents.json
#
# This will add a field to existing regression tests, based on what
# the code currently returns for a particular test.
#
# As currently written, this script adds the "browser" method to
# existing tests.

use strict;

use JSON::PP   ();
use Path::Tiny qw( path );

use HTTP::BrowserDetect ();

my $json_text = path( $ARGV[0] )->slurp;
my $tests     = JSON::PP->new->ascii->decode($json_text);

foreach my $ua ( sort keys %{$tests} ) {
    my $test   = $tests->{$ua};
    my $detect = HTTP::BrowserDetect->new($ua);
    foreach my $field (
        qw(browser browser_string device device_string
        engine engine_beta engine_minor engine_major engine_version
        os os_beta os_major os_minor os_version os_string
        robot robot_string
        robot_version robot_major robot_minor robot_beta)
    ) {
        no strict 'refs';

        my $field_name = $field;
        my $value      = $detect->$field;

        if ( $field_name eq 'device_string' ) {
            if ( defined( $test->{device_name} ) ) {
                $field_name = 'device_name';
            }
        }
        elsif ( $field_name eq 'robot_string' ) {
            if ( defined( $test->{robot_name} ) ) {
                $field_name = 'robot_name';
            }
        }
        elsif ( $field_name eq 'robot' ) {
            next unless $value || defined( $test->{$field_name} );
        }

        if ( defined($value) || exists( $test->{$field_name} ) ) {
            $test->{$field_name} = $value;
        }
        else {
            delete $test->{$field_name};
        }
    }
}

my $json   = JSON::PP->new->canonical->pretty;
my $output = $json->encode($tests);
print "$output\n";