Class: SenseApi
- Inherits:
-
Object
- Object
- SenseApi
- 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
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#email ⇒ Object
Returns the value of attribute email.
-
#monitors ⇒ Object
Returns the value of attribute monitors.
-
#password ⇒ Object
Returns the value of attribute password.
-
#token ⇒ Object
Returns the value of attribute token.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Instance Method Summary collapse
- #fetch(url, depth = 0) ⇒ Object
- #first_monitor_id ⇒ Object
-
#initialize(email, password, token: nil, monitors: nil) ⇒ SenseApi
constructor
A new instance of SenseApi.
- #realtime(monitor_id = first_monitor_id) ⇒ Object
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_id ⇒ Object
Returns the value of attribute account_id.
9 10 11 |
# File 'lib/sense_api.rb', line 9 def account_id @account_id end |
#email ⇒ Object
Returns the value of attribute email.
9 10 11 |
# File 'lib/sense_api.rb', line 9 def email @email end |
#monitors ⇒ Object
Returns the value of attribute monitors.
9 10 11 |
# File 'lib/sense_api.rb', line 9 def monitors @monitors end |
#password ⇒ Object
Returns the value of attribute password.
9 10 11 |
# File 'lib/sense_api.rb', line 9 def password @password end |
#token ⇒ Object
Returns the value of attribute token.
9 10 11 |
# File 'lib/sense_api.rb', line 9 def token @token end |
#user_id ⇒ Object
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_id ⇒ Object
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
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. 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 |