handset_detection 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,70 @@
1
+ #--
2
+ # Copyright (c) Richard Uren 2016 <richard@teleport.com.au>
3
+ # All Rights Reserved
4
+ #
5
+ # LICENSE: Redistribution and use in source and binary forms, with or
6
+ # without modification, are permitted provided that the following
7
+ # conditions are met: Redistributions of source code must retain the
8
+ # above copyright notice, this list of conditions and the following
9
+ # disclaimer. Redistributions in binary form must reproduce the above
10
+ # copyright notice, this list of conditions and the following disclaimer
11
+ # in the documentation and/or other materials provided with the
12
+ # distribution.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
+ # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16
+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17
+ # NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
20
+ # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22
+ # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23
+ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
24
+ # DAMAGE.
25
+ #++
26
+
27
+ require 'dalli'
28
+
29
+ class Memcached
30
+ # Construct a new Memcached cache object.
31
+ #
32
+ def initialize(config={})
33
+ if config.include?('cache') and config['cache'].include?('memcached') and not config['cache']['memcached']['servers'].blank?
34
+ servers = config['cache']['memcached']['servers']
35
+ else
36
+ servers = ['localhost:11211']
37
+ end
38
+
39
+ if config.include?('cache') and config['cache'].include?('memcached') and not config['cache']['memcached']['options'].blank?
40
+ options = config['cache']['memcached']['options']
41
+ else
42
+ options = { 'value_max_bytes' => 4000000 }
43
+ end
44
+ options = options.map {|k, v| [k.to_sym, v]}.to_h
45
+ @cache = Dalli::Client.new(servers, options)
46
+ end
47
+
48
+ # Get key
49
+ #
50
+ def get(key)
51
+ @cache.get key
52
+ end
53
+
54
+ # Set key
55
+ #
56
+ def set(key, data, ttl)
57
+ @cache.set key, data, ttl
58
+ end
59
+
60
+ # Delete key
61
+ #
62
+ def del(key)
63
+ @cache.delete key
64
+ end
65
+
66
+ # Flush cache
67
+ def flush
68
+ @cache.flush
69
+ end
70
+ end
@@ -0,0 +1,60 @@
1
+ #--
2
+ # Copyright (c) Richard Uren 2016 <richard@teleport.com.au>
3
+ # All Rights Reserved
4
+ #
5
+ # LICENSE: Redistribution and use in source and binary forms, with or
6
+ # without modification, are permitted provided that the following
7
+ # conditions are met: Redistributions of source code must retain the
8
+ # above copyright notice, this list of conditions and the following
9
+ # disclaimer. Redistributions in binary form must reproduce the above
10
+ # copyright notice, this list of conditions and the following disclaimer
11
+ # in the documentation and/or other materials provided with the
12
+ # distribution.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
+ # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16
+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17
+ # NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
20
+ # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22
+ # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23
+ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
24
+ # DAMAGE.
25
+ #++
26
+
27
+ class None
28
+ # Construct a new None cache object.
29
+ #
30
+ def initialize(config={})
31
+ end
32
+
33
+ # Get key
34
+ #
35
+ def get(key)
36
+ false
37
+ end
38
+
39
+ # Set key
40
+ #
41
+ def set(key, data, ttl)
42
+ false
43
+ end
44
+
45
+ # Delete key
46
+ #
47
+ def del(key)
48
+ false
49
+ end
50
+
51
+ # Flush cache
52
+ def flush
53
+ false
54
+ end
55
+
56
+ # Get fully qualified path to file
57
+ def get_file_path(key)
58
+ false
59
+ end
60
+ end
@@ -0,0 +1,59 @@
1
+ #--
2
+ # Copyright (c) Richard Uren 2016 <richard@teleport.com.au>
3
+ # All Rights Reserved
4
+ #
5
+ # LICENSE: Redistribution and use in source and binary forms, with or
6
+ # without modification, are permitted provided that the following
7
+ # conditions are met: Redistributions of source code must retain the
8
+ # above copyright notice, this list of conditions and the following
9
+ # disclaimer. Redistributions in binary form must reproduce the above
10
+ # copyright notice, this list of conditions and the following disclaimer
11
+ # in the documentation and/or other materials provided with the
12
+ # distribution.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
+ # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16
+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17
+ # NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
20
+ # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22
+ # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23
+ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
24
+ # DAMAGE.
25
+ #++
26
+
27
+ class RailsCache
28
+ # Construct a new Rails cache object.
29
+ #
30
+ def initialize(config={})
31
+ unless defined? Rails
32
+ raise "Cannot access Rails."
33
+ end
34
+ @cache = Rails.cache
35
+ end
36
+
37
+ # Get key
38
+ #
39
+ def get(key)
40
+ @cache.read key
41
+ end
42
+
43
+ # Set key
44
+ #
45
+ def set(key, data, ttl)
46
+ @cache.write key, data, expires_in: ttl
47
+ end
48
+
49
+ # Delete key
50
+ #
51
+ def del(key)
52
+ @cache.delete key
53
+ end
54
+
55
+ # Flush cache
56
+ def flush
57
+ @cache.clear
58
+ end
59
+ end
@@ -0,0 +1,105 @@
1
+ #--
2
+ # Copyright (c) Richard Uren 2016 <richard@teleport.com.au>
3
+ # All Rights Reserved
4
+ #
5
+ # LICENSE: Redistribution and use in source and binary forms, with or
6
+ # without modification, are permitted provided that the following
7
+ # conditions are met: Redistributions of source code must retain the
8
+ # above copyright notice, this list of conditions and the following
9
+ # disclaimer. Redistributions in binary form must reproduce the above
10
+ # copyright notice, this list of conditions and the following disclaimer
11
+ # in the documentation and/or other materials provided with the
12
+ # distribution.
13
+ #
14
+ # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15
+ # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16
+ # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17
+ # NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
18
+ # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19
+ # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
20
+ # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21
+ # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
22
+ # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23
+ # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
24
+ # DAMAGE.
25
+ #++
26
+
27
+ require 'active_support/core_ext/object/blank'
28
+ require 'handset_detection/cache/filesystem'
29
+ require 'handset_detection/cache/memcached'
30
+ require 'handset_detection/cache/rails'
31
+ require 'handset_detection/cache/none'
32
+
33
+ # Cache class for HandsetDetection
34
+ #
35
+ # Notes :
36
+ # * - Cache objects may be > 1Mb when serialized so ensure memcache or memcached can handle it.
37
+ #
38
+ class Cache
39
+ def initialize(config={})
40
+ @prefix = nil
41
+ @ttl = nil
42
+ @cache = nil
43
+ set_config config
44
+ end
45
+
46
+ # Set config file
47
+ #
48
+ # +param+ array $config An assoc array of config data
49
+ # +return+ true on success, false otherwise
50
+ #
51
+ def set_config(config)
52
+ @config = config
53
+ @prefix = (config.include?('cache') and config['cache'].include?('prefix')) ? config['cache']['prefix'] : 'hd40'
54
+ @duration = (config.include?('cache') and config['cache'].include?('ttl')) ? config['cache']['ttl'] : 7200
55
+ if config.include?('cache') and config['cache'].include?('memcached')
56
+ @cache = Memcached.new(@config)
57
+ elsif config.include?('cache') and config['cache'].include?('rails')
58
+ @cache = RailsCache.new(@config)
59
+ elsif config.include?('cache') and config['cache'].include?('none')
60
+ @cache = None.new(@config)
61
+ elsif defined? Rails
62
+ @cache = RailsCache.new(@config)
63
+ else
64
+ @cache = FileSystem.new(@config)
65
+ end
66
+ true
67
+ end
68
+
69
+ # Fetch a cache key
70
+ #
71
+ # +param+ string $key
72
+ # +return+ value on success, null otherwise
73
+ #
74
+ def read(key)
75
+ @cache.get @prefix + key
76
+ end
77
+
78
+ # Store a data at $key
79
+ #
80
+ # +param+ string $key
81
+ # +param+ mixed $data
82
+ # +return+ true on success, false otherwise
83
+ #
84
+ def write(key, data)
85
+ @cache.set @prefix + key, data, @duration
86
+ end
87
+
88
+ # Remove a cache key (and its data)
89
+ #
90
+ # +param+ string $key
91
+ # +return+ true on success, false otherwise
92
+ #
93
+ def delete(key)
94
+ @cache.del @prefix + key
95
+ end
96
+
97
+ # Flush the whole cache
98
+ #
99
+ # +param+ void
100
+ # +return+ true on success, false otherwise
101
+ #
102
+ def purge
103
+ @cache.flush
104
+ end
105
+ end