Class: Codelocks::NetCode

Inherits:
Object
  • Object
show all
Defined in:
lib/codelocks/net_code.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ NetCode


34
35
36
37
38
39
40
41
42
43
# File 'lib/codelocks/net_code.rb', line 34

def initialize(opts = {})
  self.opts = {
    lock_model: nil || "K3CONNECT",
    lock_id: nil,
    start: nil,
    duration: 0,
    urm: false,
    identifier: nil
  }.merge!(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


45
46
47
48
# File 'lib/codelocks/net_code.rb', line 45

def method_missing(method, *args, &block)
  return opts[method] if opts.include?(method)
  super
end

Instance Attribute Details

#optsObject

Returns the value of attribute opts


32
33
34
# File 'lib/codelocks/net_code.rb', line 32

def opts
  @opts
end

Class Method Details

.generate_netcode(opts = {}) ⇒ Codelocks::NetCode::Response

Predefined method for generating a new NetCode


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/codelocks/net_code.rb', line 15

def generate_netcode(opts = {})
  netcode = new(opts)

  if !netcode.identifier
    raise CodelocksError.new("Either a lock identifier or an access key must be provided")
  end

  Request.create("netcode/#{netcode.lock_id}",
    "id": netcode.lock_id,
    "start": netcode.start_datetime,
    "duration": netcode.duration_id,
    "lockmodel": netcode.lock_model,
    "identifier": netcode.identifier
  )
end

Instance Method Details

#duration_idInteger

NetCode duration ID


90
91
92
93
94
95
96
# File 'lib/codelocks/net_code.rb', line 90

def duration_id
  if urm?
    urm_duration
  else
    base_duration
  end
end

#identifierString

Return either a supplied identifier or the predefined access key


54
55
56
# File 'lib/codelocks/net_code.rb', line 54

def identifier
  opts[:identifier] || Codelocks.access_key
end

#start_dateString

String representing the start date in YYYY-MM-DD format


62
63
64
# File 'lib/codelocks/net_code.rb', line 62

def start_date
  start.strftime("%Y-%m-%d") unless start.nil?
end

#start_datetimeString

Full date time formatted for API use


82
83
84
# File 'lib/codelocks/net_code.rb', line 82

def start_datetime
  [start_date, start_time].join(" ")
end

#start_timeString

String representing the start time. Hour has a leading zero


70
71
72
73
74
75
76
# File 'lib/codelocks/net_code.rb', line 70

def start_time
  if urm? && duration_id >= 31 # URM enabled and >= 24 hours duration
    "00:00"
  else
    start.strftime("%H:%M") unless start.nil?
  end
end

#urm?Boolean

Are URM codes enabled?


102
103
104
# File 'lib/codelocks/net_code.rb', line 102

def urm?
  !!urm
end