Class: WurflHandset

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/wurfl_store/wurfl/wurflhandset.rb

Overview

A class that represents a handset based on information taken from the WURFL.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wurfl_id, useragent, fallback = nil) ⇒ WurflHandset

Constructor Parameters: wurfl_id: is the WURFL ID of the handset useragent: is the user agent of the handset fallback: is the fallback handset that this handset

uses for missing details.


52
53
54
55
56
57
58
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 52

def initialize (wurfl_id,useragent,fallback=nil) 
  # A hash to hold keys and values specific to this handset
  @capabilityhash = Hash::new 
  @wurfl_id = wurfl_id.to_s
  @user_agent = useragent.to_s
  @fallback = fallback
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



44
45
46
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 44

def fallback
  @fallback
end

#user_agentObject

Returns the value of attribute user_agent.



44
45
46
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 44

def user_agent
  @user_agent
end

#wurfl_idObject

Returns the value of attribute wurfl_id.



44
45
46
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 44

def wurfl_id
  @wurfl_id
end

Instance Method Details

#==(other) ⇒ Object

A method to do a simple equality check against two handsets. Parameter: other: Is the another WurflHandset to check against. Returns: true if the two handsets are equal in all values. false if they are not exactly equal in values, id and user agent. Note: for a more detailed comparison, use the compare method.



123
124
125
126
127
128
129
130
131
132
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 123

def ==(other)
  return false if other.nil? || other.class != WurflHandset
  if (self.wurfl_id == other.wurfl_id) && (self.user_agent == other.user_agent)
    other.each do |key,value|
      return false if value != self[key]
    end
    return true
  end
  return false
end

#[](key) ⇒ Object

Hash accessor Parameters: key: the WURFL key whose value is desired Returns: The value of the key, nil if the handset does not have the key.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 65

def [] (key)
  # Check if the handset actually has the key
  if @capabilityhash.key?(key)
    return @capabilityhash[key]
  else
    # The handset does not so check if the fallback handset does
    # Note: that this is actually a recursive call.
    if @fallback != nil
      return @fallback[key]
    end
  end
  # if it gets this far then no one has the key
  return nil
end

#[]=(key, val) ⇒ Object

Setter, A method to set a key and value of the handset.



91
92
93
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 91

def []= (key,val)
  @capabilityhash[key] = val.to_s
end

#compare(other) ⇒ Object

A method to compare a handset’s values against another handset. Parameters: other: is the another WurflHandset to compare against Returns: An array of the different values. Each entry in the Array is an Array of three values. The first value is the key in which both handsets have different values. The second is the other handset’s value for the key. The third is the handset id from where the other handset got it’s value.



143
144
145
146
147
148
149
150
151
152
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 143

def compare(other)
  differences = Array.new
  self.keys.each do |key|
    oval,oid = other.get_value_and_owner(key)
    if @capabilityhash[key].to_s != oval.to_s
      differences<< [key,oval,oid]
    end
  end
  return differences
end

#eachObject

A Method to iterate over all of the keys and values that the handset has. Note: this will abstract the hash iterator to handle all the lower level calls for the fallback values.



98
99
100
101
102
103
104
105
106
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 98

def each
  keys = self.keys
  keys.each do |key|
    # here is the magic that gives us the key and value of the handset
    # all the way up to the fallbacks end.  
    # Call the pass block with the key and value passed
    yield key, self[key]
  end
end

#get_value_and_owner(key) ⇒ Object

like the above accessor, but also to know who the value comes from Returns: the value and the id of the handset from which the value was obtained



84
85
86
87
88
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 84

def get_value_and_owner(key)
  return @capabilityhash[key],@wurfl_id if @capabilityhash.key?(key)
  return @fallback.get_value_and_owner(key) if @fallback != nil
  return nil,nil
end

#keysObject

A method to get all of the keys that the handset has.



109
110
111
112
113
114
# File 'lib/wurfl_store/wurfl/wurflhandset.rb', line 109

def keys
  # merge the unique keys of the handset and it's fallback
  return @capabilityhash.keys | @fallback.keys if @fallback != nil
  # no fallback so just return the handset's keys
  return @capabilityhash.keys
end