Class: ScanBeacon::Beacon::Field

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/scan_beacon/beacon/field.rb

Constant Summary collapse

ENCODING =
"ASCII-8BIT".freeze
NULL_BYTE =
"\x00".force_encoding(ENCODING).freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Field

Returns a new instance of Field.



8
9
10
# File 'lib/scan_beacon/beacon/field.rb', line 8

def initialize(opts = {})
  self.set_data(opts)
end

Class Method Details

.field_with_length(id, length) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/scan_beacon/beacon/field.rb', line 12

def self.field_with_length(id, length)
  return id if id.is_a? self
  if id.is_a? String
    self.new hex: id, length: length
  elsif id.is_a? Integer
    self.new number: id, length: length
  end
end

Instance Method Details

#<=>(other) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/scan_beacon/beacon/field.rb', line 84

def <=> (other)
  if other.is_a? self.class
    self.bytes <=> other.bytes
  else
    self.value <=> other
  end
end

#bytesObject



54
55
56
# File 'lib/scan_beacon/beacon/field.rb', line 54

def bytes
  @data
end

#hashObject



94
95
96
# File 'lib/scan_beacon/beacon/field.rb', line 94

def hash
  @data.hash
end

#inspectObject



50
51
52
# File 'lib/scan_beacon/beacon/field.rb', line 50

def inspect
  "<Beacon::Field value=#{self.value.inspect}>"
end

#set_data(opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scan_beacon/beacon/field.rb', line 21

def set_data(opts = {})
  bytes = opts[:bytes]
  hex = opts[:hex]
  number = opts[:number]
  length = opts[:length]
  if bytes
    @data = bytes.force_encoding(ENCODING)
  elsif hex
    # zero pad hex if needed
    hex = "0"*(length*2-hex.size) + hex if length and hex.size < length*2
    @data = [hex].pack("H*")
  elsif number
    raise ArgumentError.new("Must also give a field length when you give a number") if length.nil?
    set_data(hex: number.to_s(16), length: length)
  end
end

#to_hexObject



80
81
82
# File 'lib/scan_beacon/beacon/field.rb', line 80

def to_hex
  @data.unpack("H*")[0]
end

#to_iObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/scan_beacon/beacon/field.rb', line 58

def to_i
  size = @data.size
  case size
  when 0
    nil
  when 1
    @data.unpack("C")[0]
  when 2
    @data.unpack("S>")[0]
  when 3
    (NULL_BYTE + @data).unpack("L>")[0]
  when 4
    @data.unpack("L>")[0]
  when 5,6,7
    (NULL_BYTE*(8-size) + @data).unpack("Q>")[0]
  when 8
    @data.unpack("Q>")[0]
  else
    @data[-8..-1].unpack("Q>")[0]
  end
end

#to_sObject



46
47
48
# File 'lib/scan_beacon/beacon/field.rb', line 46

def to_s
  value.to_s
end

#valueObject



38
39
40
41
42
43
44
# File 'lib/scan_beacon/beacon/field.rb', line 38

def value
  if @data.size < 6
    self.to_i
  else
    self.to_hex
  end
end