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   50867 use Mojo::Base 'Slovo::Command', -signatures;
  2         5  
  2         13  
3 2     2   1690 use Mojo::File qw(path);
  2         4  
  2         107  
4 2     2   12 use Mojo::Loader qw(data_section file_is_binary);
  2         4  
  2         82  
5 2     2   22 use Mojo::Util qw(encode decode getopt dumper);
  2         4  
  2         101  
6 2     2   368 use YAML::XS qw(Dump DumpFile LoadFile);
  2         2437  
  2         109  
7 2     2   37 use Mojo::JSON qw(to_json);
  2         4  
  2         1891  
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 82992 sub run ($self, @args) {
  5         10  
  5         12  
  5         6  
14 5   100     19 my $action = shift @args || 'list';
15 5         10 my $a_pattern = '^(?:' . join('|', @{$self->actions}) . ')$';
  5         17  
16             $action =~ $a_pattern
17             || STDERR->say('Only '
18 5 50 0     62 . 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         2077 my $file_actions = join('|', @{$self->actions}[0 .. 2]);
  5         17  
30 5         30 my $where_actions = join('|', @{$self->actions}[3 .. 4]);
  5         10  
31 5 100       78 if ($action =~ /$file_actions/) {
    50          
32 4 100 50     21 $file
33             || STDERR->say(
34             'Please profide a YAML file to read data from' . $/ . $/ . $self->usage)
35             && return;
36 2 50 0     12 $file =~ /\.(ya?ml|json)$/
37             || STDERR->say(
38             'Only YAML and json files are supported right now.' . $/ . $/ . $self->usage)
39             && return;
40 2         18 $action = "_$action";
41 2         8 $self->$action($file);
42             }
43             elsif ($action =~ /$where_actions/) {
44 1 50 33     5 $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         107 return;
51             }
52              
53 1     1   2 sub _create ($self, $file) {
  1         2  
  1         2  
  1         3  
54 1         5 my $products = LoadFile $file;
55 1         309 my $db = $self->app->dbx->db;
56              
57             # INSERT
58 1         267 for (@$products) {
59             do {
60 4         6906 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         57 properties => to_json($_->{properties})});
70             } unless $db->select('products', ['id'], {alias => $_->{alias}, sku => $_->{sku},})
71 4 50       6044 ->hash;
72             }
73 1         1987 return;
74             }
75              
76 1     1   3 sub _update ($self, $file) {
  1         2  
  1         2  
  1         1  
77 1         5 my $products = LoadFile $file;
78 1         270 my $db = $self->app->dbx->db;
79              
80             # UPDATE all the products found in the file
81 1         287 for (@$products) {
82 2         3781 say encode utf8 => "Updating $_->{alias}, $_->{sku}";
83 2         31 $_->{properties} = to_json($_->{properties});
84 2         61 $db->update('products', $_, {sku => $_->{sku}});
85             }
86 1         3202 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         2  
  1         2  
  1         2  
  1         2  
  1         2  
94 1         5 STDOUT->say('Action list - Not implemented' . $/, $self->usage);
95 1         9544 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