Class: Sensorpush::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/sensorpush/client.rb

Constant Summary collapse

BASE_URL =
"https://api.sensorpush.com/api/v1"
BASE_HEADERS =
{ 'accept': "application/json", 'Content-Type': "application/json" }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
# File 'lib/sensorpush/client.rb', line 17

def initialize(options = {})
  self.username = options[:username]
  self.password = options[:password]
  self.accesstoken = options[:accesstoken]
end

Instance Attribute Details

#accesstokenObject

Returns the value of attribute accesstoken.



15
16
17
# File 'lib/sensorpush/client.rb', line 15

def accesstoken
  @accesstoken
end

#passwordObject

Returns the value of attribute password.



14
15
16
# File 'lib/sensorpush/client.rb', line 14

def password
  @password
end

#usernameObject

Returns the value of attribute username.



13
14
15
# File 'lib/sensorpush/client.rb', line 13

def username
  @username
end

Instance Method Details

#authenticateObject



23
24
25
26
27
28
# File 'lib/sensorpush/client.rb', line 23

def authenticate
  authorization = authorize
  self.accesstoken = get_token(authorization) if authorization

  !accesstoken.nil?
end

#samples(id, options = {}) ⇒ Object



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

def samples(id, options = {})
  uri = URI(BASE_URL + "/samples")
  body = { sensors: [id] }
  body.merge!({ limit: options[:limit] }) unless options[:limit].nil?
  body.merge!({ startTime: options[:start_time].to_s }) unless options[:start_time].nil?
  body.merge!({ endTime: options[:end_time].to_s }) unless options[:end_time].nil?

  response = Net::HTTP.post(uri, body.to_json, headers)
  json_object = JSON.parse(response.body)

  samples_json_array = json_object.dig("sensors", id)
  samples = samples_json_array&.collect do |hash|
    Sensorpush::Sample.new(hash)
  end
  samples || []
end

#sensorsObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sensorpush/client.rb', line 30

def sensors
  uri = URI(BASE_URL + "/devices/sensors")
  body = {}

  response = Net::HTTP.post(uri, body.to_json, headers)
  json_object = JSON.parse(response.body)

  json_object.collect do |_key, hash|
    Sensorpush::Sensor.new(hash)
  end
end