Class: ScanBeacon::EddystoneUrlBeacon

Inherits:
Beacon
  • Object
show all
Defined in:
lib/scan_beacon/eddystone_url_beacon.rb

Overview

Convenience class for constructing & advertising Eddystone-URL frames

Constant Summary collapse

SCHEMES =
{"http://www."  => "\x00",
"https://www." => "\x01",
"http://"      => "\x02",
"https://"     => "\x03"}
EXPANSIONS =
{".com/"  => "\x00",
".org/"  => "\x01",
".edu/"  => "\x02",
".net/"  => "\x03",
".info/" => "\x04",
".biz/"  => "\x05",
".gov/"  => "\x06",
".com"   => "\x07",
".org"   => "\x08",
".edu"   => "\x09",
".net"   => "\x0a",
".info"  => "\x0b",
".biz"   => "\x0c",
".gov"   => "\x0d"}

Instance Attribute Summary

Attributes inherited from Beacon

#beacon_types, #data, #ids, #mac, #mfg_id, #power, #rssis, #service_uuid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Beacon

#==, #ad_count, #add_rssi, #add_type, #major, #minor, #rssi, #uuid

Constructor Details

#initialize(opts = {}) ⇒ EddystoneUrlBeacon

Returns a new instance of EddystoneUrlBeacon.



25
26
27
28
29
30
# File 'lib/scan_beacon/eddystone_url_beacon.rb', line 25

def initialize(opts = {})
  opts[:service_uuid] ||= 0xFEAA
  opts[:beacon_type] ||= :eddystone_url
  super opts
  self.url = opts[:url] if opts[:url]
end

Class Method Details

.compress_url(url) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
# File 'lib/scan_beacon/eddystone_url_beacon.rb', line 45

def self.compress_url(url)
  scheme, scheme_code = SCHEMES.find {|k, v| url.start_with? k}
  raise ArgumentError, "Invalid URL" if scheme.nil?
  compressed_url = scheme_code + url[scheme.size..-1]
  EXPANSIONS.each do |k,v|
    compressed_url.gsub! k,v
  end
  raise ArgumentError, "URL too long" if compressed_url.size > 18
  compressed_url.force_encoding("ASCII-8BIT").unpack("H*")[0]
end

.decompress_url(hex) ⇒ Object

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/scan_beacon/eddystone_url_beacon.rb', line 56

def self.decompress_url(hex)
  compressed_url_string = [hex].pack("H*")
  scheme_code = compressed_url_string[0]
  scheme, scheme_code = SCHEMES.find {|k,v| v == scheme_code}
  raise ArgumentError, "Invalid URL" if scheme.nil?
  decompressed_url = scheme + compressed_url_string[1..-1]
  EXPANSIONS.each do |k,v|
    decompressed_url.gsub! v,k
  end
  decompressed_url
end

.from_beacon(beacon) ⇒ Object



32
33
34
# File 'lib/scan_beacon/eddystone_url_beacon.rb', line 32

def self.from_beacon(beacon)
  new(ids: beacon.ids, power: beacon.power, rssis: beacon.rssis)
end

Instance Method Details

#inspectObject



68
69
70
# File 'lib/scan_beacon/eddystone_url_beacon.rb', line 68

def inspect
  "<EddystoneUrlBeacon url=\"#{url}\" rssi=#{rssi}, scans=#{ad_count}, power=#{@power}>"
end

#urlObject



36
37
38
# File 'lib/scan_beacon/eddystone_url_beacon.rb', line 36

def url
  @url ||= self.class.decompress_url( self.ids[0].to_s )
end

#url=(new_url) ⇒ Object



40
41
42
43
# File 'lib/scan_beacon/eddystone_url_beacon.rb', line 40

def url=(new_url)
  @url = new_url
  self.ids = [self.class.compress_url(@url)]
end