Class: Sensorpush::Client
- Inherits:
-
Object
- Object
- Sensorpush::Client
- 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
-
#accesstoken ⇒ Object
Returns the value of attribute accesstoken.
-
#password ⇒ Object
Returns the value of attribute password.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #samples(id, options = {}) ⇒ Object
- #sensors ⇒ Object
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( = {}) self.username = [:username] self.password = [:password] self.accesstoken = [:accesstoken] end |
Instance Attribute Details
#accesstoken ⇒ Object
Returns the value of attribute accesstoken.
15 16 17 |
# File 'lib/sensorpush/client.rb', line 15 def accesstoken @accesstoken end |
#password ⇒ Object
Returns the value of attribute password.
14 15 16 |
# File 'lib/sensorpush/client.rb', line 14 def password @password end |
#username ⇒ Object
Returns the value of attribute username.
13 14 15 |
# File 'lib/sensorpush/client.rb', line 13 def username @username end |
Instance Method Details
#authenticate ⇒ Object
23 24 25 26 27 28 |
# File 'lib/sensorpush/client.rb', line 23 def authenticate = self.accesstoken = get_token() if !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, = {}) uri = URI(BASE_URL + "/samples") body = { sensors: [id] } body.merge!({ limit: [:limit] }) unless [:limit].nil? body.merge!({ startTime: [:start_time].to_s }) unless [:start_time].nil? body.merge!({ endTime: [:end_time].to_s }) unless [: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 |
#sensors ⇒ Object
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 |