Module: WurflDevice::Cache

Defined in:
lib/wurfl_device/cache.rb

Defined Under Namespace

Classes: Devices, Entries, Status, UserAgents, UserAgentsManufacturers, UserAgentsMatched

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.storageObject



84
85
86
# File 'lib/wurfl_device/cache.rb', line 84

def storage
  @storage ||= Redis.new(:db => 7)
end

Class Method Details

.build_capabilities(device_id) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/wurfl_device/cache.rb', line 165

def build_capabilities(device_id)
  capabilities = Capability.new

  actual_capabilities = MessagePack.unpack(Devices.get(device_id).force_encoding('US-ASCII')) rescue nil

  return capabilities if actual_capabilities.nil?

  capabilities['fall_back_tree'] ||= Array.new

  if !actual_capabilities['fall_back'].nil? && !actual_capabilities['fall_back'].empty? && actual_capabilities['fall_back'] != 'root'
    fall_back = build_capabilities(actual_capabilities['fall_back'])
    if !fall_back.nil? && !fall_back['id'].nil? && !fall_back['id'].empty?
      capabilities['fall_back_tree'].unshift(fall_back['id'])
      fall_back.each_pair do |key, value|
        if value.kind_of?(Hash)
          capabilities[key] ||= Capability.new
          value.each_pair do |k, v|
            capabilities[key][k] = v
          end
        elsif value.is_a?(Array)
          capabilities[key] ||= Array.new
          capabilities[key] |= value
        else
          capabilities[key] = value
        end
      end
    end
  end

  actual_capabilities.each_pair do |key, value|
    if value.kind_of?(Hash)
      capabilities[key] ||= Capability.new
      value.each_pair do |k, v|
        capabilities[key][k] = v
      end
    else
      capabilities[key] = value
    end
  end

  capabilities
end

.clearObject



88
89
90
91
92
93
# File 'lib/wurfl_device/cache.rb', line 88

def clear
  Status.clear
  Devices.clear
  UserAgents.clear
  UserAgentsManufacturers.clear
end

.initialize_cache(xml_file) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/wurfl_device/cache.rb', line 99

def initialize_cache(xml_file)
  version_list = Array.new

  Status.uninitialize!
  XmlLoader.load_xml_file(xml_file) do |capabilities|
    version = capabilities.delete(:version)
    version_list << version unless version.nil?

    device_id = capabilities['id']
    next if device_id.nil? || device_id.empty?

    user_agent = capabilities['user_agent']
    user_agent = Settings::GENERIC if user_agent.nil? || user_agent.empty?
    user_agent.strip!

    fall_back = capabilities['fall_back']
    fall_back = '' if fall_back.nil?
    fall_back.strip!

    user_agent = UserAgent.new user_agent
    UserAgents.set user_agent, device_id
    unless user_agent =~ /^DO_NOT_MATCH/i
      UserAgentsManufacturers.hset user_agent.manufacturer, user_agent, device_id
    end

    Devices.set device_id, capabilities.to_msgpack
  end

  version_list.each do |ver|
    Status.hset 'version', ver, 1
  end
  Status.set 'last_updated', Time.now

  Status.initialize!
end

.initialized?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/wurfl_device/cache.rb', line 95

def initialized?
  Status.initialized?
end

.rebuild_user_agentsObject



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/wurfl_device/cache.rb', line 135

def rebuild_user_agents
  UserAgents.clear
  UserAgentsManufacturers.clear
  Devices.entries.each do |device_id|
    device = MessagePack.unpack(Devices.get(device_id).force_encoding('US-ASCII'))
    user_agent = UserAgent.new device['user_agent']
    UserAgents.set user_agent, device_id
    unless user_agent =~ /^DO_NOT_MATCH/i
      UserAgentsManufacturers.hset user_agent.manufacturer, user_agent, device_id
    end
  end
  Status.set 'last_updated', Time.now
end

.update_actual_capabilities(user_agent, capabilities) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/wurfl_device/cache.rb', line 149

def update_actual_capabilities(user_agent, capabilities)
  capabilities.each_pair do |key, value|
    if value.kind_of?(Hash)
      value.each_pair do |k, v|
        UserAgents.set user_agent, "#{key}:#{k}", MessagePack.pack(v)
      end
    else
      if ['user_agent', 'fall_back', 'id'].include?(key)
        UserAgents.set user_agent, key, value
      else
        UserAgents.set user_agent, key, MessagePack.pack(value)
      end
    end
  end
end