Class: Pickpoint::DevicesService

Inherits:
Object
  • Object
show all
Defined in:
lib/pickpoint/devices.rb

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ DevicesService

Returns a new instance of DevicesService.



45
46
47
# File 'lib/pickpoint/devices.rb', line 45

def initialize(transport)
  @t = transport
end

Instance Method Details

#command(uid, payload) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pickpoint/devices.rb', line 88

def command(uid, payload)
  path = "/v2/devices/#{URI.encode_www_form_component(uid)}/command"
  # pack("m0") = strict Base64 (no newlines); avoids the base64 gem (not default since 3.4)
  b64 = [payload.to_s.b].pack("m0")
  raw = @t.do(
    Transport::RequestOpts.new(
      method: "POST",
      path: path,
      body: { "payload" => b64 }
    )
  )
  body = JSON.parse(raw)
  DeviceCommandResult.new(delivered: (body["delivered"] || 0).to_i)
end

#create(input) ⇒ Object



71
72
73
74
# File 'lib/pickpoint/devices.rb', line 71

def create(input)
  raw = @t.do(Transport::RequestOpts.new(method: "POST", path: "/v2/devices", body: input.to_h))
  Device.from_hash(JSON.parse(raw))
end

#delete(uid) ⇒ Object



82
83
84
85
86
# File 'lib/pickpoint/devices.rb', line 82

def delete(uid)
  path = "/v2/devices/#{URI.encode_www_form_component(uid)}"
  @t.do(Transport::RequestOpts.new(method: "DELETE", path: path))
  nil
end

#get(uid) ⇒ Object



65
66
67
68
69
# File 'lib/pickpoint/devices.rb', line 65

def get(uid)
  path = "/v2/devices/#{URI.encode_www_form_component(uid)}"
  raw = @t.do(Transport::RequestOpts.new(method: "GET", path: path))
  Device.from_hash(JSON.parse(raw))
end

#list(query = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pickpoint/devices.rb', line 49

def list(query = nil)
  q = query || DeviceListQuery.new
  params = {}
  params["skip"] = q.skip.to_s if q.skip && q.skip.to_i.positive?
  params["take"] = q.take.to_s if q.take && q.take.to_i.positive?
  params["search"] = q.search if q.search && !q.search.empty?
  params["idle"] = "1" if q.idle

  raw = @t.do(Transport::RequestOpts.new(method: "GET", path: "/v2/devices", query: params))
  body = JSON.parse(raw)
  DeviceListResult.new(
    data: Array(body["data"]).map { |x| Device.from_hash(x) },
    total: (body["total"] || 0).to_i
  )
end

#update(uid, input) ⇒ Object



76
77
78
79
80
# File 'lib/pickpoint/devices.rb', line 76

def update(uid, input)
  path = "/v2/devices/#{URI.encode_www_form_component(uid)}"
  raw = @t.do(Transport::RequestOpts.new(method: "PATCH", path: path, body: input.to_h))
  Device.from_hash(JSON.parse(raw))
end