Class: Store

Inherits:
Object show all
Defined in:
lib/handset_detection/store.rb

Overview

A file backed storage class

Constant Summary collapse

@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Constructor



39
40
41
42
43
44
45
# File 'lib/handset_detection/store.rb', line 39

def initialize
  @dirname = "hd40store"
  @path = ""
  @directory = ""
  @cache = nil
  @config = {} 
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



34
35
36
# File 'lib/handset_detection/store.rb', line 34

def cache
  @cache
end

#directoryObject (readonly)

Returns the value of attribute directory.



34
35
36
# File 'lib/handset_detection/store.rb', line 34

def directory
  @directory
end

Class Method Details

.get_instanceObject

Get the Singleton

param void return Object @@instance



52
53
54
55
# File 'lib/handset_detection/store.rb', line 52

def self.get_instance
  @@instance = self.new if @@instance == nil
  @@instance
end

Instance Method Details

#fetch(key) ⇒ Object

Fetch data from disk

param string $key. return mixed



125
126
127
128
129
130
131
132
# File 'lib/handset_detection/store.rb', line 125

def fetch(key)
  begin
    jsonstr = File.read(File.join(@directory, "#{key}.json"))
    return JSON.parse(jsonstr)
  rescue
  end
  false
end

#fetch_devicesObject

Returns all devices inside one giant array

Used by localDevice* functions to iterate over all devies

param void return array All devices in one giant assoc array



141
142
143
144
145
146
147
148
149
# File 'lib/handset_detection/store.rb', line 141

def fetch_devices
  data = {'devices' => []} 
  Dir.glob(File.join(@directory, 'Device*.json')).each do |file|
    jsonstr = File.read file
    return false if not jsonstr or jsonstr.blank?
    data['devices'] << JSON.parse(jsonstr)
  end 
  data
end

#move_in(src_abs_name, dest_name) ⇒ Object

Moves a json file into storage.

param string $srcAbsName The fully qualified path and file name eg /tmp/sjjhas778hsjhh param string $destName The key name inside the cache eg Device_19.json return boolean true on success, false otherwise



157
158
159
# File 'lib/handset_detection/store.rb', line 157

def move_in(src_abs_name, dest_name)
  FileUtils.mv src_abs_name, File.join(@directory, dest_name)
end

#purgeObject

Cleans out the store - Use with caution

param void return true on success, false otherwise



166
167
168
169
170
171
172
173
174
# File 'lib/handset_detection/store.rb', line 166

def purge
  files = Dir.glob File.join(@directory, '*.json')
  files.each do |file|
    if File.file? file
      return false unless File.unlink file
    end
  end 
  @cache.purge
end

#read(key) ⇒ Object

Read $data, try cache first

param sting $key Key to search for return boolean true on success, false



111
112
113
114
115
116
117
118
# File 'lib/handset_detection/store.rb', line 111

def read(key)
  reply = @cache.read(key)
  return reply unless reply.blank?
  reply = fetch(key)
  return false if reply.blank?
  @cache.write(key, reply)
  return reply
end

#set_config(config, create_directory = false) ⇒ Object

Sets the storage config options, optionally creating the storage directory.

param array $config An assoc array of config info. param boolean $createDirectory return void



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/handset_detection/store.rb', line 63

def set_config(config, create_directory=false)
  config.each { |key, value| @config[key] = value }
  @path = @config.include?('filesdir') ? @config['filesdir'] : File.dirname(__FILE__) 
  @directory = File.join @path, @dirname
  @cache = Cache.new(@config)

  if create_directory 
    unless File.directory? @directory
      unless FileUtils.mkdir_p @directory
        raise("Error : Failed to create storage directory at #{@directory}. Check permissions.")
      end
    end
  end
end

#store(key, data) ⇒ Object

Store data to disk

param string $key The search key (becomes the filename .. so keep it alphanumeric) param array $data Data to persist (will be persisted in json format) return boolean true on success, false otherwise



96
97
98
99
100
101
102
103
104
# File 'lib/handset_detection/store.rb', line 96

def store(key, data)
  jsonstr = JSON.generate data
  begin
    File.open(File.join(@directory, "#{key}.json"), 'w') { |f| f.write jsonstr }
    return true
  rescue
  end
  false
end

#write(key, data) ⇒ Object

Write data to cache & disk

param string $key param array $data return boolean true on success, false otherwise



84
85
86
87
88
# File 'lib/handset_detection/store.rb', line 84

def write(key, data)
  return false if data.blank?
  return false unless store(key, data)
  @cache.write(key, data)
end