Class: SenseApi

Inherits:
Object
  • Object
show all
Defined in:
lib/sense_api.rb,
lib/sense_api/version.rb

Defined Under Namespace

Classes: SenseApiError

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password, token: nil, monitors: nil) ⇒ SenseApi

Returns a new instance of SenseApi.



13
14
15
16
17
18
19
20
# File 'lib/sense_api.rb', line 13

def initialize(email, password, token: nil, monitors: nil)
  self.email = email
  self.password = password
  self.token = token
  self.monitors = monitors

  login! unless token
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



9
10
11
# File 'lib/sense_api.rb', line 9

def 
  @account_id
end

#emailObject

Returns the value of attribute email.



9
10
11
# File 'lib/sense_api.rb', line 9

def email
  @email
end

#monitorsObject

Returns the value of attribute monitors.



9
10
11
# File 'lib/sense_api.rb', line 9

def monitors
  @monitors
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/sense_api.rb', line 9

def password
  @password
end

#tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/sense_api.rb', line 9

def token
  @token
end

#user_idObject

Returns the value of attribute user_id.



9
10
11
# File 'lib/sense_api.rb', line 9

def user_id
  @user_id
end

Instance Method Details

#fetch(url, depth = 0) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sense_api.rb', line 22

def fetch(url, depth = 0)
  uri = URI(url)
  req = Net::HTTP::Get.new(uri)

  req['Authorization'] = "bearer #{token}"
  req['Sense-Client-Version'] = '1.17.1-20c25f9'
  req['X-Sense-Protocol'] = '3'
  req['User-Agent'] = 'okhttp/3.8.0'

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE  
  res = http.request(req)

  case res
  when Net::HTTPRedirection
    if depth < 2
      fetch(res['Location'], depth + 1)
    else
      raise SenseApiError, "Too many redirects"
    end
  when Net::HTTPSuccess
    JSON.parse(res.body)
  else
    raise SenseApiError, "Error: #{res.value}" 
  end
end

#first_monitor_idObject



50
51
52
# File 'lib/sense_api.rb', line 50

def first_monitor_id
  monitors.first["id"]
end

#realtime(monitor_id = first_monitor_id) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sense_api.rb', line 54

def realtime(monitor_id = first_monitor_id)
  raise ArgumentError, "block required" unless block_given?

  exiting = false
  EM.run do
    ws = WebSocket::EventMachine::Client.connect(
      uri: "wss://clientrt.sense.com/monitors/#{monitor_id}/realtimefeed",
      headers: {
        'Authorization' => "bearer #{token}",
        'Sense-Client-Version' => '1.17.1-20c25f9',
        'X-Sense-Protocol' => '3',
        'User-Agent' => 'okhttp/3.8.0'
      },
      tls_options: { fail_if_no_peer_cert: false, verify_peer: false }
    )

    ws.onmessage do |msg, type|
      if yield(JSON.parse(msg)) == :exit
        exiting = true
        ws.close
      end
    end

    ws.onclose do |code, reason|
      raise SenseApiError, "Connection closed: #{code} #{reason}" unless exiting
      EM.stop_event_loop
    end
  end
end