Class: Kakaxi::Client
- Inherits:
-
Object
- Object
- Kakaxi::Client
- Defined in:
- lib/kakaxi/client.rb
Constant Summary collapse
- COMMON_HEADER =
{ 'Content-Type' => 'application/json' }
- BASE_URL =
'https://kakaxi-data.me/api/v1/'
Instance Attribute Summary collapse
-
#current_device ⇒ Object
readonly
Returns the value of attribute current_device.
-
#devices ⇒ Object
readonly
Returns the value of attribute devices.
-
#farm ⇒ Object
readonly
Returns the value of attribute farm.
-
#humidities ⇒ Object
readonly
Returns the value of attribute humidities.
-
#interval_photo_data ⇒ Object
readonly
Returns the value of attribute interval_photo_data.
-
#interval_photos ⇒ Object
readonly
Returns the value of attribute interval_photos.
-
#rainfalls ⇒ Object
readonly
Returns the value of attribute rainfalls.
-
#solar_radiations ⇒ Object
readonly
Returns the value of attribute solar_radiations.
-
#temp_data ⇒ Object
readonly
Returns the value of attribute temp_data.
-
#temps ⇒ Object
readonly
Returns the value of attribute temps.
-
#timelapses ⇒ Object
readonly
Returns the value of attribute timelapses.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#initialize(email, password) ⇒ Client
constructor
A new instance of Client.
- #load_humidities(days: 5) ⇒ Object
- #load_interval_photos(pagination: false, start_date: Date.today, end_date: Date.today + 7) ⇒ Object
- #load_rainfalls(days: 5, unit: 'inch') ⇒ Object
- #load_solar_radiations(days: 5, unit: 'watt') ⇒ Object
- #load_temps(days: 5, unit: 'celsius') ⇒ Object
- #load_timelapses(days: 5) ⇒ Object
- #set_current_device(index: 0, id: nil, name: nil) ⇒ Object
Constructor Details
#initialize(email, password) ⇒ Client
Returns a new instance of Client.
14 15 16 17 18 19 20 21 |
# File 'lib/kakaxi/client.rb', line 14 def initialize(email, password) @email = email @password = password @token = get_token @farm = get_farm @devices = get_devices @current_device = @devices[0] end |
Instance Attribute Details
#current_device ⇒ Object (readonly)
Returns the value of attribute current_device.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def current_device @current_device end |
#devices ⇒ Object (readonly)
Returns the value of attribute devices.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def devices @devices end |
#farm ⇒ Object (readonly)
Returns the value of attribute farm.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def farm @farm end |
#humidities ⇒ Object (readonly)
Returns the value of attribute humidities.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def humidities @humidities end |
#interval_photo_data ⇒ Object (readonly)
Returns the value of attribute interval_photo_data.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def interval_photo_data @interval_photo_data end |
#interval_photos ⇒ Object (readonly)
Returns the value of attribute interval_photos.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def interval_photos @interval_photos end |
#rainfalls ⇒ Object (readonly)
Returns the value of attribute rainfalls.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def rainfalls @rainfalls end |
#solar_radiations ⇒ Object (readonly)
Returns the value of attribute solar_radiations.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def solar_radiations @solar_radiations end |
#temp_data ⇒ Object (readonly)
Returns the value of attribute temp_data.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def temp_data @temp_data end |
#temps ⇒ Object (readonly)
Returns the value of attribute temps.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def temps @temps end |
#timelapses ⇒ Object (readonly)
Returns the value of attribute timelapses.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def timelapses @timelapses end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
10 11 12 |
# File 'lib/kakaxi/client.rb', line 10 def token @token end |
Instance Method Details
#load_humidities(days: 5) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kakaxi/client.rb', line 39 def load_humidities(days: 5) uri = URI.parse(BASE_URL + "kakaxi_devices/#{current_device.id}/indicators/humidity?days=#{days}") humidities = get(uri)['data'] Struct.new('HumidityMetaData', :size, :device_id) @humidities = humidities.map do |humidity| Kakaxi::Data::Humidity.new( value: humidity['humidity'], recorded_at: humidity['recorded_at'], interpolation: humidity['interpolation'], deficit: humidity['humidity_deficit'] ) end @humidity_data = Struct::HumidityMetaData.new(@humidities.length, @current_device.id) end |
#load_interval_photos(pagination: false, start_date: Date.today, end_date: Date.today + 7) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/kakaxi/client.rb', line 95 def load_interval_photos(pagination: false, start_date: Date.today, end_date: Date.today + 7) uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/interval_photos") params = { pagination: false, start_date: start_date, end_date: end_date } uri.query = URI.encode_www_form(params) interval_photos = get(uri) Struct.new('IntervalPhotoMetaData', :size, :pagination, :start_date, :end_date) @interval_photos = interval_photos.map do |photo| Kakaxi::IntervalPhoto.new( id: photo['id'], url: photo['url'], taken_at: photo['takenAt'] ) end @interval_photo_data = Struct::IntervalPhotoMetaData.new(@interval_photos.size, pagination, start_date, end_date) end |
#load_rainfalls(days: 5, unit: 'inch') ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/kakaxi/client.rb', line 68 def load_rainfalls(days: 5, unit: 'inch') uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/rainfall?days=#{days}&unit=#{unit}") rainfalls = get(uri)['data'] Struct.new('RainfallMetaData', :size, :unit, :device_id) @rainfalls = rainfalls.map do |rainfall| Kakaxi::Data::Rainfall.new(value: rainfall['amount'], recorded_at: rainfall['recorded_at'], interpolation: rainfall['interpolation']) end @rainfall_data = Struct::RainfallMetaData.new(@rainfalls.length, unit, @current_device.id) end |
#load_solar_radiations(days: 5, unit: 'watt') ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/kakaxi/client.rb', line 54 def load_solar_radiations(days: 5, unit: 'watt') uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/solar_radiation?days=#{days}&unit=#{unit}") solar_radiations = get(uri)['data'] Struct.new('SolarRadiationMetaData', :size, :unit, :device_id) @solar_radiations = solar_radiations.map do |solar_radiation| Kakaxi::Data::SolarRadiation.new( value: solar_radiation['amount'], recorded_at: solar_radiation['recorded_at'], interpolation: solar_radiation['interpolation'] ) end @solar_radiation_data = Struct::SolarRadiationMetaData.new(@solar_radiations.length, unit, @current_device.id) end |
#load_temps(days: 5, unit: 'celsius') ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/kakaxi/client.rb', line 29 def load_temps(days: 5, unit: 'celsius') uri = URI.parse(BASE_URL + "kakaxi_devices/#{current_device.id}/indicators/temperature?days=#{days}&unit=#{unit}") temps = get(uri)['data'] Struct.new('TempMetaData', :size, :unit, :device_id) @temps = temps.map do |temp| Kakaxi::Data::Temperature.new(value: temp['temperature'], recorded_at: temp['recorded_at'], interpolation: temp['interpolation']) end @temp_data = Struct::TempMetaData.new(@temps.length, unit, @current_device.id) end |
#load_timelapses(days: 5) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kakaxi/client.rb', line 78 def load_timelapses(days: 5) uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/timelapse?days=#{days}") timelapses = get(uri)['data'] Struct.new('TimelapseMetaData', :size, :device_id) @timelapses = timelapses.map do |timelapse| Kakaxi::Data::Timelapse.new( id: timelapse['id'], start_datetime: timelapse['beginningTime'], end_datetime: timelapse['endTime'], url: timelapse['videoURL'], thumbnail_name: timelapse['thumbnail']['name'], thumbnail_url: timelapse['thumbnail']['url'] ) end @timelapse_data = Struct::TimelapseMetaData.new(@timelapses.length, @current_device.id) end |
#set_current_device(index: 0, id: nil, name: nil) ⇒ Object
23 24 25 26 27 |
# File 'lib/kakaxi/client.rb', line 23 def set_current_device(index: 0, id: nil, name: nil) @current_device = @devices[index] if id.nil? && name.nil? @current_device = @devices.find { |device| device.id == id } unless id.nil? @current_device = @devices.find { |device| device.name == name } unless name.nil? end |