| 1 | |||
| 2 | package Google::Chart::Type::QRcode; | ||
| 3 | use Moose; | ||
| 4 | use Moose::Util::TypeConstraints; | ||
| 5 | use Encode (); | ||
| 6 | use namespace::clean -except => qw(meta); | ||
| 7 | |||
| 8 | extends 'Google::Chart'; | ||
| 9 | |||
| 10 | enum 'Google::Chart::Type::QRcode::Encoding' => qw(shift_jis utf-8 iso-8859-1); | ||
| 11 | enum 'Google::Chart::Type::QRcode::ECLevel' => qw(L M Q H); | ||
| 12 | |||
| 13 | coerce 'Google::Chart::Type::QRcode::Encoding' | ||
| 14 | => from 'Str' | ||
| 15 | => via { | ||
| 16 | s/^Shift[-_]JIS$/shift_jis/; | ||
| 17 | s/^UTF-?8$/utf-8/; | ||
| 18 | return lc $_; | ||
| 19 | } | ||
| 20 | ; | ||
| 21 | |||
| 22 | has '+width' => ( | ||
| 23 | default => 150 | ||
| 24 | ); | ||
| 25 | |||
| 26 | has '+height' => ( | ||
| 27 | default => 150 | ||
| 28 | ); | ||
| 29 | |||
| 30 | has text => ( | ||
| 31 | is => 'rw', | ||
| 32 | isa => 'Str', | ||
| 33 | required => 1 | ||
| 34 | ); | ||
| 35 | |||
| 36 | has qrcode_encoding => ( | ||
| 37 | is => 'rw', | ||
| 38 | isa => 'Google::Chart::Type::QRcode::Encoding', | ||
| 39 | required => 1, | ||
| 40 | default => 'utf-8', | ||
| 41 | coerce => 1 | ||
| 42 | ); | ||
| 43 | |||
| 44 | has eclevel => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Google::Chart::Type::QRcode::ECLevel', | ||
| 47 | ); | ||
| 48 | |||
| 49 | has margin => ( | ||
| 50 | is => 'rw', | ||
| 51 | isa => 'Num' | ||
| 52 | ); | ||
| 53 | |||
| 54 | sub _build_type { 'qr' } | ||
| 55 | |||
| 56 | override prepare_query => sub { | ||
| 57 | my $self = shift; | ||
| 58 | my @query = super(); | ||
| 59 | |||
| 60 | push @query, (cht => 'qr'); | ||
| 61 | push @query, (choe => $self->qrcode_encoding); | ||
| 62 | push @query, (chl => Encode::is_utf8($self->text) ? | ||
| 63 | Encode::decode_utf8($self->text) : $self->text); | ||
| 64 | if ($self->eclevel || $self->margin ) { | ||
| 65 | push @query, (chld => | ||
| 66 | join( '|', $self->eclevel || '', $self->margin || '') | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | return @query; | ||
| 71 | }; | ||
| 72 | |||
| 73 | __PACKAGE__->meta->make_immutable(); | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | __END__ |