Class: Wurfl::Handset

Inherits:
Object
  • Object
show all
Defined in:
lib/wurfl/handset.rb

Overview

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

Defined Under Namespace

Classes: NullHandset

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wurfl_id, useragent, fallback = nil, actual_device = nil) ⇒ Handset

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.


19
20
21
22
23
24
25
# File 'lib/wurfl/handset.rb', line 19

def initialize (wurfl_id, useragent, fallback = nil, actual_device = nil) 
  @capabilities = {}
  @wurfl_id = wurfl_id
  @user_agent = useragent
  @actual_device = actual_device
  @fallback = fallback
end

Instance Attribute Details

#fallbackObject



31
32
33
# File 'lib/wurfl/handset.rb', line 31

def fallback
 @fallback || NullHandset.instance
end

#user_agentObject

Returns the value of attribute user_agent.



10
11
12
# File 'lib/wurfl/handset.rb', line 10

def user_agent
  @user_agent
end

#wurfl_idObject

Returns the value of attribute wurfl_id.



10
11
12
# File 'lib/wurfl/handset.rb', line 10

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.



68
69
70
71
72
73
# File 'lib/wurfl/handset.rb', line 68

def ==(other)
  other.instance_of?(Wurfl::Handset) && 
    self.wurfl_id == other.wurfl_id && 
    self.user_agent == other.user_agent &&
    other.keys.all? {|key| other[key] == self[key] }
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.



40
41
42
# File 'lib/wurfl/handset.rb', line 40

def [] (key)
  @capabilities.key?(key) ? @capabilities[key] : fallback[key]
end

#[]=(key, val) ⇒ Object

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



52
53
54
# File 'lib/wurfl/handset.rb', line 52

def []= (key,val)
  @capabilities[key] = val
end

#actual_device?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/wurfl/handset.rb', line 27

def actual_device?
  @actual_device
end

#differences(other) ⇒ Object



75
76
77
78
# File 'lib/wurfl/handset.rb', line 75

def differences(other)
  keys = (self.keys | other.keys)
  keys.find_all {|k| self[k] != other[k]}
end

#keysObject

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



57
58
59
# File 'lib/wurfl/handset.rb', line 57

def keys
  @capabilities.keys | fallback.keys
end

#owner(key) ⇒ Object

Returns: the wurfl id of the handset from which the value of a capability is obtained



47
48
49
# File 'lib/wurfl/handset.rb', line 47

def owner(key)
  @capabilities.key?(key) ? @wurfl_id : fallback.owner(key)
end