File Coverage

blib/lib/BusyBird/Config.pm
Criterion Covered Total %
statement 55 55 100.0
branch 12 16 75.0
condition n/a
subroutine 14 14 100.0
pod 3 3 100.0
total 84 88 95.4


line stmt bran cond sub pod time code
1             package BusyBird::Config;
2 11     11   48 use strict;
  11         19  
  11         349  
3 11     11   102 use warnings;
  11         16  
  11         316  
4 11     11   47 use Carp;
  11         14  
  11         547  
5 11     11   49 use BusyBird::Log qw(bblog);
  11         14  
  11         454  
6 11     11   86 use BusyBird::SafeData qw(safed);
  11         18  
  11         439  
7 11     11   49 use BusyBird::Util qw(config_file_path);
  11         16  
  11         500  
8 11     11   3923 use URI::Escape qw(uri_escape);
  11         14381  
  11         682  
9 11     11   4728 use Encode ();
  11         70503  
  11         243  
10 11     11   5416 use Tie::IxHash;
  11         20706  
  11         289  
11 11     11   4403 use BusyBird::StatusStorage::SQLite;
  11         68  
  11         428  
12 11     11   6445 use File::ShareDir ();
  11         59815  
  11         12127  
13              
14             my %_DEFAULTS = ();
15              
16             $_DEFAULTS{timeline} = {
17             time_zone => sub { "local" },
18             time_format => sub { '%x (%a) %X %Z' },
19             time_locale => sub { $ENV{LC_TIME} or "C" },
20             post_button_url => sub { "https://twitter.com/intent/tweet" },
21              
22             status_permalink_builder => sub { return sub {
23             my ($status) = @_;
24             my $ss = safed($status);
25             my $permalink_in_status = $ss->val(qw(busybird status_permalink));
26             return $permalink_in_status if defined $permalink_in_status;
27             my $id = $ss->val(qw(busybird original id))
28             || $ss->val(qw(busybird original id_str))
29             || $ss->val("id")
30             || $ss->val("id_str");
31             my $username = $ss->val(qw(user screen_name));
32             if(defined($id) && defined($username) && $id =~ /^\d+$/) {
33             return qq{https://twitter.com/$username/status/$id};
34             }
35             return undef;
36             } },
37              
38             urls_entity_url_builder => sub { sub { my ($text, $entity) = @_; return $entity->{url} }},
39             urls_entity_text_builder => sub { sub { my ($text, $entity) = @_; return $entity->{display_url} }},
40            
41             media_entity_url_builder => sub { sub { my ($text, $entity) = @_; return $entity->{url} } },
42             media_entity_text_builder => sub { sub { my ($text, $entity) = @_; return $entity->{display_url} }},
43            
44             user_mentions_entity_url_builder => sub { sub {
45             my ($text, $entity, $status) = @_;
46             my $screen_name = $entity->{screen_name};
47             $screen_name = "" if not defined $screen_name;
48             return qq{https://twitter.com/$screen_name};
49             }},
50             user_mentions_entity_text_builder => sub { sub { my $text = shift; return $text }},
51            
52             hashtags_entity_url_builder => sub { sub {
53             my ($text, $entity, $status) = @_;
54             my $query_hashtag = uri_escape('#' . Encode::encode('utf8', $entity->{text}));
55             return qq{https://twitter.com/search?q=$query_hashtag&src=hash};
56             }},
57             hashtags_entity_text_builder => sub { sub { my $text = shift; return $text }},
58            
59             timeline_web_notifications => sub { 'simple' },
60             hidden => sub { 0 },
61              
62             attached_image_urls_builder => sub {
63             return sub {
64             my ($status) = @_;
65             tie my %url_set, "Tie::IxHash";
66             my $ss = safed($status);
67             my @entities = map { $ss->array($_, "media") } qw(entities extended_entities);
68             foreach my $entity (@entities) {
69             my $sentity = safed($entity);
70             my $url = $sentity->val("media_url");
71             my $type = $sentity->val("type");
72             if(defined($url) && (!defined($type) || lc($type) eq "photo")) {
73             $url_set{$url} = 1 if defined $url;
74             }
75             }
76             return keys %url_set;
77             };
78             },
79             attached_image_max_height => sub { 360 },
80             attached_image_show_default => sub { "hidden" },
81             acked_statuses_load_count => sub { 20 },
82             default_level_threshold => sub { 0 },
83             };
84              
85             $_DEFAULTS{global} = {
86             %{$_DEFAULTS{timeline}},
87              
88             default_status_storage => sub {
89             BusyBird::StatusStorage::SQLite->new(path => config_file_path("statuses.sqlite3"));
90             },
91            
92             sharedir_path => sub { File::ShareDir::dist_dir("BusyBird") },
93             timeline_list_pager_entry_max => sub { 7 },
94             timeline_list_per_page => sub { 30 },
95             };
96              
97             sub new {
98 292     292 1 2961 my ($class, %args) = @_;
99 292         519 my $type = $args{type};
100 292 50       846 croak "type parameter is mandatory" if not defined $type;
101 292         612 my $default_generators = $_DEFAULTS{$type};
102 292 50       669 croak "Unknown config type: $type" if not defined $default_generators;
103 292 50       726 croak "with_default parameter is mandatory" if not defined $args{with_default};
104 292         1235 my $self = bless {
105             default_generators => $default_generators,
106             with_default => $args{with_default},
107             config => {},
108             }, $class;
109 292         22844 return $self;
110             }
111              
112             sub get_config {
113 904     904 1 923 my ($self, $key) = @_;
114 904 50       1463 croak "key parameter is mandatory" if not defined $key;
115 904 100       3201 return $self->{config}{$key} if exists $self->{config}{$key};
116 439 100       1124 return undef if !$self->{with_default};
117            
118 122         228 my $gen = $self->{default_generators}{$key};
119 122 100       225 return undef if not defined $gen;
120 120         251 my $value = $gen->();
121 120         5499 $self->set_config($key, $value);
122 120         397 return $value;
123             }
124              
125             sub set_config {
126 208     208 1 466 my ($self, %configs) = @_;
127 208         502 foreach my $key (keys %configs) {
128 216 100       741 if(!exists($self->{default_generators}{$key})) {
129 9         34 bblog("warn", "Unknown config parameter: $key");
130             }
131 216         913 $self->{config}{$key} = $configs{$key};
132             }
133             }
134              
135             1;
136             __END__