Class: Kakaxi::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ Client

Returns a new instance of Client.



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

def initialize(email, password)
  @email = email
  @password = password
end

Instance Attribute Details

#current_deviceObject (readonly)

Returns the value of attribute current_device.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def current_device
  @current_device
end

#devicesObject (readonly)

Returns the value of attribute devices.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def devices
  @devices
end

#farmObject (readonly)

Returns the value of attribute farm.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def farm
  @farm
end

#humiditiesObject (readonly)

Returns the value of attribute humidities.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def humidities
  @humidities
end

#interval_photo_dataObject (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_photosObject (readonly)

Returns the value of attribute interval_photos.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def interval_photos
  @interval_photos
end

#rainfallsObject (readonly)

Returns the value of attribute rainfalls.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def rainfalls
  @rainfalls
end

#solar_radiationsObject (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_dataObject (readonly)

Returns the value of attribute temp_data.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def temp_data
  @temp_data
end

#tempsObject (readonly)

Returns the value of attribute temps.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def temps
  @temps
end

#timelapsesObject (readonly)

Returns the value of attribute timelapses.



10
11
12
# File 'lib/kakaxi/client.rb', line 10

def timelapses
  @timelapses
end

#tokenObject (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_devicesObject



33
34
35
36
# File 'lib/kakaxi/client.rb', line 33

def load_devices
  @devices = get_devices
  @current_device = @devices[0]
end

#load_farmObject



29
30
31
# File 'lib/kakaxi/client.rb', line 29

def load_farm
  @farm = get_farm
end

#load_humidities(days: 5) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kakaxi/client.rb', line 48

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::.new(@humidities.length, @current_device.id)
end

#load_interval_photos(pagination: false, start_date: Date.today, end_date: Date.today + 7) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/kakaxi/client.rb', line 104

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::.new(@interval_photos.size, pagination, start_date, end_date)
end

#load_rainfalls(days: 5) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/kakaxi/client.rb', line 77

def load_rainfalls(days: 5)
  uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/rainfall?days=#{days}&unit=inch")
  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::.new(@rainfalls.length, 'inch', @current_device.id)
end

#load_solar_radiations(days: 5) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kakaxi/client.rb', line 63

def load_solar_radiations(days: 5)
  uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/solar_radiation?days=#{days}&unit=watt")
  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::.new(@solar_radiations.length, 'watt',  @current_device.id) 
end

#load_temps(days: 5, unit: 'celsius') ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/kakaxi/client.rb', line 38

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::.new(@temps.length, unit, @current_device.id)
end

#load_timelapses(days: 5) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kakaxi/client.rb', line 87

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::.new(@timelapses.length, @current_device.id)
end

#load_tokenObject



25
26
27
# File 'lib/kakaxi/client.rb', line 25

def load_token
  @token = get_token
end

#set_current_device(index: 0, id: nil, name: nil) ⇒ Object



19
20
21
22
23
# File 'lib/kakaxi/client.rb', line 19

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