Class: NinjaBlocks::Device

Inherits:
Base
  • Object
show all
Defined in:
lib/ninja_blocks/device.rb

Instance Method Summary collapse

Methods inherited from Base

#connection, #delete, #execute, #get, #post, #put, #put_json, #token

Instance Method Details

#actuate(guid, da) ⇒ Object



77
78
79
80
# File 'lib/ninja_blocks/device.rb', line 77

def actuate(guid, da)
  json = JSON.dump('DA'=> da)
  put_json("https://api.ninja.is/rest/v0/device/#{guid}", json)
end

#actuate_local(local_ip, guid, da) ⇒ Object



82
83
84
85
86
# File 'lib/ninja_blocks/device.rb', line 82

def actuate_local(local_ip, guid, da)
  json = JSON.dump('DA'=> "#{da}")
  puts "http://#{local_ip}:8000/rest/v0/device/#{guid}"
  put_json("http://#{local_ip}:8000/rest/v0/device/#{guid}", json)
end

#available_devicesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ninja_blocks/device.rb', line 59

def available_devices
  hash_of_response = get("https://api.ninja.is/rest/v0/devices")

  devices_types = []
  
  hash_of_response["data"].each do |d|
    device_hash = {}
    device_hash["guid"] = d[0]
    device_hash = device_hash.merge(d[1]) 
    devices_types << device_hash['device_type'] unless device_hash['device_type'].nil?
    devices_types = devices_types.uniq
  end
  
  devices_types.each do |t|
    puts t
  end
end

#available_typesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ninja_blocks/device.rb', line 41

def available_types
  hash_of_response = get("https://api.ninja.is/rest/v0/devices")

  devices_types = []
  
  hash_of_response["data"].each do |d|
    device_hash = {}
    device_hash["guid"] = d[0]
    device_hash = device_hash.merge(d[1]) 
    devices_types << device_hash['device_type'] unless device_hash['device_type'].nil?
    devices_types = devices_types.uniq
  end
  
  devices_types.each do |t|
    puts t
  end
end

#data(guid, from, to) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/ninja_blocks/device.rb', line 97

def data(guid, from, to)
  
  from = (Chronic.parse(from).utc.to_i) *1000
  to = (Chronic.parse(to).utc.to_i) *1000 
  
  get("https://api.ninja.is/rest/v0/device/#{guid}/data", "from=#{from}&to=#{to}")
end

#last_heartbeat(guid) ⇒ Object



105
106
107
# File 'lib/ninja_blocks/device.rb', line 105

def last_heartbeat(guid)
  get("https://api.ninja.is/rest/v0/device/#{guid}/heartbeat")
end

#list(*filter_by) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ninja_blocks/device.rb', line 4

def list(*filter_by)
  
  hash_of_response = get("https://api.ninja.is/rest/v0/devices")

  devices = []

  

  unless filter_by.nil?
    filter_by = filter_by[0] if filter_by.kind_of?(Array)
  end
  unless filter_by.nil? || filter_by[:device_type].empty?

    hash_of_response["data"].each do |d|
      device_hash = {}
      device_hash["guid"] = d[0]
      device_hash = device_hash.merge(d[1]) 
      devices << device_hash
      devices = devices.reject { |d| d['device_type'] != filter_by[:device_type] }
    end
  
  else
    hash_of_response["data"].each do |d|
      device_hash = {}
      device_hash["guid"] = d[0]
      device_hash = device_hash.merge(d[1]) 
      devices << device_hash
      devices = devices.reject { |d| d['device_type'] == nil }
      devices = devices.sort_by { |k| k["device_type"] }
    end
    
  end
    
  devices
end

#subscribe(guid, url) ⇒ Object



88
89
90
91
# File 'lib/ninja_blocks/device.rb', line 88

def subscribe(guid, url)
  json = JSON.dump('url'=> url)
  post("https://api.ninja.is/rest/v0/device/#{guid}/callback", json)
end

#unsubscribe(guid) ⇒ Object



93
94
95
# File 'lib/ninja_blocks/device.rb', line 93

def unsubscribe(guid)
  delete("https://api.ninja.is/rest/v0/device/#{guid}/callback")
end