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,178 @@
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 'fileutils'
28
+ require 'handset_detection/cache'
29
+ require 'json'
30
+
31
+ # A file backed storage class
32
+ #
33
+ class Store
34
+ attr_reader :directory, :cache
35
+ @@instance = nil
36
+
37
+ # Singleton Constructor
38
+ #
39
+ # +param+ string $path Location of storage ROOT dir.
40
+ # +param+ boolean $createDirectory - Create storage directory if it does not exist
41
+ #
42
+ def initialize
43
+ @dirname = "hd40store"
44
+ @path = ""
45
+ @directory = ""
46
+ @cache = nil
47
+ @config = {}
48
+ end
49
+
50
+ # Get the Singleton
51
+ #
52
+ # +param+ void
53
+ # +return+ Object @@instance
54
+ #
55
+ def self.get_instance
56
+ @@instance = self.new if @@instance == nil
57
+ @@instance
58
+ end
59
+
60
+ # Sets the storage config options, optionally creating the storage directory.
61
+ #
62
+ # +param+ array $config An assoc array of config info.
63
+ # +param+ boolean $createDirectory
64
+ # +return+ void
65
+ #
66
+ def set_config(config, create_directory=false)
67
+ config.each { |key, value| @config[key] = value }
68
+ @path = @config.include?('filesdir') ? @config['filesdir'] : File.dirname(__FILE__)
69
+ @directory = File.join @path, @dirname
70
+ @cache = Cache.new(@config)
71
+
72
+ if create_directory
73
+ unless File.directory? @directory
74
+ unless FileUtils.mkdir_p @directory
75
+ raise("Error : Failed to create storage directory at #{@directory}. Check permissions.")
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ # Write data to cache & disk
82
+ #
83
+ # +param+ string $key
84
+ # +param+ array $data
85
+ # +return+ boolean true on success, false otherwise
86
+ #
87
+ def write(key, data)
88
+ return false if data.blank?
89
+ return false unless store(key, data)
90
+ @cache.write(key, data)
91
+ end
92
+
93
+ # Store data to disk
94
+ #
95
+ # +param+ string $key The search key (becomes the filename .. so keep it alphanumeric)
96
+ # +param+ array $data Data to persist (will be persisted in json format)
97
+ # +return+ boolean true on success, false otherwise
98
+ #
99
+ def store(key, data)
100
+ jsonstr = JSON.generate data
101
+ begin
102
+ File.open(File.join(@directory, "#{key}.json"), 'w') { |f| f.write jsonstr }
103
+ return true
104
+ rescue
105
+ end
106
+ false
107
+ end
108
+
109
+ # Read $data, try cache first
110
+ #
111
+ # +param+ sting $key Key to search for
112
+ # +return+ boolean true on success, false
113
+ #
114
+ def read(key)
115
+ reply = @cache.read(key)
116
+ return reply unless reply.blank?
117
+ reply = fetch(key)
118
+ return false if reply.blank?
119
+ return false unless @cache.write(key, reply)
120
+ return reply
121
+ end
122
+
123
+ # Fetch data from disk
124
+ #
125
+ # +param+ string $key.
126
+ # +return+ mixed
127
+ #
128
+ def fetch(key)
129
+ begin
130
+ jsonstr = File.read(File.join(@directory, "#{key}.json"))
131
+ return JSON.parse(jsonstr)
132
+ rescue
133
+ end
134
+ false
135
+ end
136
+
137
+ # Returns all devices inside one giant array
138
+ #
139
+ # Used by localDevice* functions to iterate over all devies
140
+ #
141
+ # +param+ void
142
+ # +return+ array All devices in one giant assoc array
143
+ #
144
+ def fetch_devices
145
+ data = {'devices' => []}
146
+ Dir.glob(File.join(@directory, 'Device*.json')).each do |file|
147
+ jsonstr = File.read file
148
+ return false if not jsonstr or jsonstr.blank?
149
+ data['devices'] << JSON.parse(jsonstr)
150
+ end
151
+ data
152
+ end
153
+
154
+ # Moves a json file into storage.
155
+ #
156
+ # +param+ string $srcAbsName The fully qualified path and file name eg /tmp/sjjhas778hsjhh
157
+ # +param+ string $destName The key name inside the cache eg Device_19.json
158
+ # +return+ boolean true on success, false otherwise
159
+ #
160
+ def move_in(src_abs_name, dest_name)
161
+ FileUtils.mv src_abs_name, File.join(@directory, dest_name)
162
+ end
163
+
164
+ # Cleans out the store - Use with caution
165
+ #
166
+ # +param+ void
167
+ # +return+ true on success, false otherwise
168
+ #
169
+ def purge
170
+ files = Dir.glob File.join(@directory, '*.json')
171
+ files.each do |file|
172
+ if File.file? file
173
+ return false unless File.unlink file
174
+ end
175
+ end
176
+ @cache.purge
177
+ end
178
+ end
@@ -0,0 +1,27 @@
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 'handset_detection/hd4'
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handset_detection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Handset Detection
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use the HandsetDetection.com API from Ruby.
14
+ email: hello@handsetdetection.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/handset_detection.rb
20
+ - lib/handset_detection/base.rb
21
+ - lib/handset_detection/cache.rb
22
+ - lib/handset_detection/cache/filesystem.rb
23
+ - lib/handset_detection/cache/memcached.rb
24
+ - lib/handset_detection/cache/none.rb
25
+ - lib/handset_detection/cache/rails.rb
26
+ - lib/handset_detection/device.rb
27
+ - lib/handset_detection/extra.rb
28
+ - lib/handset_detection/hd4.rb
29
+ - lib/handset_detection/store.rb
30
+ homepage: https://github.com/HandsetDetection/ruby-apikit/
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.5.1
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: API kit for HandsetDetection.com
54
+ test_files: []