File Coverage

blib/lib/Slovo/Command/prodan/products.pm
Criterion Covered Total %
statement 70 78 89.7
branch 9 14 64.2
condition 4 11 36.3
subroutine 10 11 90.9
pod 1 1 100.0
total 94 115 81.7


line stmt bran cond sub pod time code
1             package Slovo::Command::prodan::products;
2 2     2   45644 use Mojo::Base 'Slovo::Command', -signatures;
  2         5  
  2         11  
3 2     2   1473 use Mojo::File qw(path);
  2         4  
  2         83  
4 2     2   10 use Mojo::Loader qw(data_section file_is_binary);
  2         4  
  2         75  
5 2     2   21 use Mojo::Util qw(encode decode getopt dumper);
  2         4  
  2         88  
6 2     2   368 use YAML::XS qw(Dump DumpFile LoadFile);
  2         2222  
  2         95  
7 2     2   11 use Mojo::JSON qw(to_json);
  2         20  
  2         1607  
8             has description => 'Manage products on the command line';
9              
10             has usage => sub { shift->extract_usage };
11             has actions => sub { [qw(create update dump delete list)] };
12              
13 5     5 1 71611 sub run ($self, @args) {
  5         11  
  5         9  
  5         8  
14 5   100     17 my $action = shift @args || 'list';
15 5         10 my $a_pattern = '^(?:' . join('|', @{$self->actions}) . ')$';
  5         11  
16             $action =~ $a_pattern
17             || STDERR->say('Only '
18 5 50 0     56 . join(',', @{$self->actions})
  0         0  
19             . ' actions are supported.'
20             . $/
21             . $/
22             . $self->usage)
23             && return;
24 5         24 getopt \@args,
25             'f|file=s' => \(my $file = ''),
26             'w|where=s' => \(my $where = ''),
27             'l|limit=i' => \(my $limit = 100),
28             'o|ofset=s' => \(my $ofset = 0);
29 5         1810 my $file_actions = join('|', @{$self->actions}[0 .. 2]);
  5         14  
30 5         28 my $where_actions = join('|', @{$self->actions}[3 .. 4]);
  5         9  
31 5 100       69 if ($action =~ /$file_actions/) {
    50          
32 4 100 50     19 $file
33             || STDERR->say(
34             'Please profide a YAML file to read data from' . $/ . $/ . $self->usage)
35             && return;
36 2 50 0     11 $file =~ /\.(ya?ml|json)$/
37             || STDERR->say(
38             'Only YAML and json files are supported right now.' . $/ . $/ . $self->usage)
39             && return;
40 2         13 $action = "_$action";
41 2         9 $self->$action($file);
42             }
43             elsif ($action =~ /$where_actions/) {
44 1 50 33     4 $action eq 'delete'
45             && STDERR->say('Please provide a WHERE clause for DELETE!')
46             && return;
47 1         2 $action = "_$action";
48 1         4 $self->$action($where, $limit, $ofset);
49             }
50 3         96 return;
51             }
52              
53 1     1   2 sub _create ($self, $file) {
  1         2  
  1         1  
  1         2  
54 1         4 my $products = LoadFile $file;
55 1         272 my $db = $self->app->dbx->db;
56              
57             # INSERT
58 1         272 for (@$products) {
59             do {
60 4         6112 say encode utf8 => "Inserting $_->{alias}, $_->{sku}";
61             $db->insert(
62             'products' => {
63             alias => $_->{alias},
64             sku => $_->{sku},
65             title => $_->{title},
66             p_type => $_->{p_type},
67              
68             # The data is NOT encoded to UTF8 by to_json
69 4         54 properties => to_json($_->{properties})});
70             } unless $db->select('products', ['id'], {alias => $_->{alias}, sku => $_->{sku},})
71 4 50       5356 ->hash;
72             }
73 1         1747 return;
74             }
75              
76 1     1   3 sub _update ($self, $file) {
  1         2  
  1         1  
  1         2  
77 1         5 my $products = LoadFile $file;
78 1         237 my $db = $self->app->dbx->db;
79              
80             # UPDATE all the products found in the file
81 1         299 for (@$products) {
82 2         3236 say encode utf8 => "Updating $_->{alias}, $_->{sku}";
83 2         27 $_->{properties} = to_json($_->{properties});
84 2         51 $db->update('products', $_, {sku => $_->{sku}});
85             }
86 1         2792 return;
87             }
88              
89 0     0   0 sub _delete ($self, $where, $limit, $offset) {
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
90 0         0 Carp::croak "Action delete - Not implemented";
91             }
92              
93 1     1   2 sub _list ($self, $where, $limit, $offset) {
  1         1  
  1         2  
  1         1  
  1         1  
  1         2  
94 1         4 STDOUT->say('Action list - Not implemented' . $/, $self->usage);
95 1         8512 return;
96             }
97             1;
98              
99             =encoding utf8
100              
101             =head1 NAME
102              
103             Slovo::Command::prodan::products - manage products on the command line
104              
105             =head1 SYNOPSIS
106              
107             slovo prodan products create --from ./products.yaml
108             slovo prodan products update --from ./products.yaml
109             slovo prodan products list --where "alias like'%лечителката%'"
110             slovo prodan products delete --where "alias like'%лечителката%'"
111              
112             =head1 DESCRIPTION
113              
114             Slovo::Command::prodan::products is a command to easily create, list, update or
115             delete a bunch of products on the command line. For now only adding products
116             from (and dumping to) YAML files is supported. In the future CSV and XLS files
117             may be supported too.
118              
119             The idea is that YAML is very human friendly and a user can edit such a file
120             and then feed it to this command to create or update the items in this file.
121             Example files with product items can be found in the test folder of this
122             distribution.
123              
124             This command is still alfa quality and its functionality may change often.
125              
126             =head1 SEE ALSO
127              
128             L,
129             L,
130             L
131              
132              
133             =cut
134