kakaxi 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3949b5b3fd256bbe28e3f5571c07b102ffbc7aa4
4
+ data.tar.gz: fd1b0a05bb45303d5cf7e9b78e591b70ee08d3ee
5
+ SHA512:
6
+ metadata.gz: c3505fb7ebd3e3abb895b747898de81e767c0e46e3e8cbab99f083810ddfadb1d908160371119ab76a792a9085b876fcaf7e0696f189d7601d0b348338b07c4a
7
+ data.tar.gz: 9094d96cbd5a17ace27cc2678f4d7d8d19b32d9431ac072f6c557f8e34685386e8ec1b06aa2205b30cf208b0010c2751447858f6f79e5156d55bae247803ebab
@@ -0,0 +1,6 @@
1
+ class InvalidCredentials < StandardError
2
+ def initialize(email, password, msg='email: %s, or password: %s is invalid')
3
+ super(msg % [email, password])
4
+ end
5
+ end
6
+
@@ -0,0 +1,141 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'net/http'
4
+ require 'net/https'
5
+
6
+ module Kakaxi
7
+ class Client
8
+ COMMON_HEADER = { 'Content-Type' => 'application/json' }
9
+ BASE_URL = 'https://kakaxi-data.me/api/v1/'
10
+ attr_reader :current_device, :temps, :temp_data, :token, :farm, :devices, :humidities, :timelapses, :solar_radiations, :rainfalls
11
+
12
+ def initialize(email, password)
13
+ @email = email
14
+ @password = password
15
+ @token = get_token
16
+ @farm = get_farm
17
+ @devices = get_devices
18
+ @current_device = @devices[0]
19
+ end
20
+
21
+ def set_current_device(index: 0, id: nil, name: nil)
22
+ @current_device = @devices[index] if id.nil? && name.nil?
23
+ @current_device = @devices.find { |device| device.id == id } unless id.nil?
24
+ @current_device = @devices.find { |device| device.name == name } unless name.nil?
25
+ end
26
+
27
+ def load_temps(days: 5, unit: 'celsius')
28
+ uri = URI.parse(BASE_URL + "kakaxi_devices/#{current_device.id}/indicators/temperature?days=#{days}&unit=#{unit}")
29
+ temps = get(uri)['data']
30
+ Struct.new('TempMetaData', :size, :unit, :device_id)
31
+ @temps = temps.map do |temp|
32
+ Kakaxi::Data::Temperature.new(value: temp['temperature'], recorded_at: temp['recorded_at'], interpolation: temp['interpolation'])
33
+ end
34
+ @temp_data = Struct::TempMetaData.new(@temps.length, unit, @current_device.id)
35
+ end
36
+
37
+ def load_humidities(days: 5)
38
+ uri = URI.parse(BASE_URL + "kakaxi_devices/#{current_device.id}/indicators/humidity?days=#{days}")
39
+ humidities = get(uri)['data']
40
+ Struct.new('HumidityMetaData', :size, :device_id)
41
+ @humidities = humidities.map do |humidity|
42
+ Kakaxi::Data::Humidity.new(
43
+ value: humidity['humidity'],
44
+ recorded_at: humidity['recorded_at'],
45
+ interpolation: humidity['interpolation'],
46
+ deficit: humidity['humidity_deficit']
47
+ )
48
+ end
49
+ @humidity_data = Struct::HumidityMetaData.new(@humidities.length, @current_device.id)
50
+ end
51
+
52
+ def load_solar_radiations(days: 5)
53
+ uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/solar_radiation?days=#{days}&unit=watt")
54
+ solar_radiations = get(uri)['data']
55
+ Struct.new('SolarRadiationMetaData', :size, :unit, :device_id)
56
+ @solar_radiations = solar_radiations.map do |solar_radiation|
57
+ Kakaxi::Data::SolarRadiation.new(
58
+ value: solar_radiation['amount'],
59
+ recorded_at: solar_radiation['recorded_at'],
60
+ interpolation: solar_radiation['interpolation']
61
+ )
62
+ end
63
+ @solar_radiation_data = Struct::SolarRadiationMetaData.new(@solar_radiations.length, 'watt', @current_device.id)
64
+ end
65
+
66
+ def load_rainfalls(days: 5)
67
+ uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/rainfall?days=#{days}&unit=inch")
68
+ rainfalls = get(uri)['data']
69
+ Struct.new('RainfallMetaData', :size, :unit, :device_id)
70
+ @rainfalls = rainfalls.map do |rainfall|
71
+ Kakaxi::Data::Rainfall.new(value: rainfall['amount'], recorded_at: rainfall['recorded_at'], interpolation: rainfall['interpolation'])
72
+ end
73
+ @rainfall_data = Struct::RainfallMetaData.new(@rainfalls.length, 'inch', @current_device.id)
74
+ end
75
+
76
+ def load_timelapses(days: 5)
77
+ uri = URI.parse(BASE_URL + "kakaxi_devices/#{@current_device.id}/indicators/timelapse?days=#{days}")
78
+ timelapses = get(uri)['data']
79
+ Struct.new('TimelapseMetaData', :size, :device_id)
80
+ @timelapses = timelapses.map do |timelapse|
81
+ Kakaxi::Data::Timelapse.new(
82
+ id: timelapse['id'],
83
+ start_datetime: timelapse['beginningTime'],
84
+ end_datetime: timelapse['endTime'],
85
+ url: timelapse['videoURL'],
86
+ thumbnail_name: timelapse['thumbnail']['name'],
87
+ thumbnail_url: timelapse['thumbnail']['url']
88
+ )
89
+ end
90
+ @timelapse_data = Struct::TimelapseMetaData.new(@timelapses.length, @current_device.id)
91
+ end
92
+
93
+ private
94
+ def get_token
95
+ uri = URI.parse(BASE_URL + 'oauth/token')
96
+ params = { username: @email, password: @password, grant_type: 'password', scope: 'all' }
97
+ response = post(uri, params)
98
+ raise InvalidCredentials.new(@email, @password) if response.code == '404'
99
+ JSON.parse(response.body)['access_token']
100
+ end
101
+
102
+ def get_farm
103
+ farm = get(URI.parse(BASE_URL + 'users/me'))['farm']
104
+ Kakaxi::Farm.new(id: farm['id'], name: farm['name'])
105
+ end
106
+
107
+ def get_devices
108
+ uri = URI.parse(BASE_URL + "farms/#{@farm.id}/kakaxi_devices?pagination=false")
109
+ devices = get(uri)
110
+ devices.map { |device| Kakaxi::Device.new(id: device['id'], name: device['name']) }
111
+ end
112
+
113
+ def post(uri, params, header=COMMON_HEADER)
114
+ request = Net::HTTP::Post.new(uri.path)
115
+ request.body = params.to_json
116
+ set_header(header, request)
117
+ https(uri).request(request)
118
+ end
119
+
120
+ def get(uri)
121
+ header = COMMON_HEADER.merge('Authorization' => "Bearer #{@token}")
122
+ request = Net::HTTP::Get.new(uri)
123
+ set_header(header, request)
124
+ JSON.parse(https(uri).request(request).body)
125
+ end
126
+
127
+ def set_header(header, request)
128
+ header.each { |key, value| request[key] = value }
129
+ end
130
+
131
+ def https(uri)
132
+ https = Net::HTTP.new(uri.host, uri.port)
133
+ https.use_ssl = true
134
+ https
135
+ end
136
+
137
+ def indicator_uri(device_id, indicator, days, unit)
138
+ URI.parse(BASE_URL + "kakaxi_devices/#{device_id}/indicators/#{indicator}?days=#{days}")
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,14 @@
1
+ module Kakaxi
2
+ module Data
3
+ class BaseData
4
+ attr_reader :value, :recorded_at, :interpolation
5
+
6
+ def initialize(value: nil, recorded_at: nil, interpolation: nil)
7
+ @value = value
8
+ @recorded_at = DateTime.strptime(recorded_at, '%Y-%m-%dT%H:%M:%SZ')
9
+ @interpolation = interpolation
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,8 @@
1
+ require 'date'
2
+ require 'kakaxi/data/base_data'
3
+ require 'kakaxi/data/temperature'
4
+ require 'kakaxi/data/humidity'
5
+ require 'kakaxi/data/solar_radiation'
6
+ require 'kakaxi/data/rainfall'
7
+ require 'kakaxi/data/timelapse'
8
+
@@ -0,0 +1,15 @@
1
+ require 'kakaxi/data/base_data'
2
+
3
+ module Kakaxi
4
+ module Data
5
+ class Humidity < BaseData
6
+ attr_reader :deficit
7
+
8
+ def initialize(value: nil, recorded_at: nil, interpolation: nil, deficit: nil)
9
+ @deficit = deficit
10
+ super(value: value, recorded_at: recorded_at, interpolation: interpolation)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,15 @@
1
+ require 'kakaxi/data/base_data'
2
+
3
+ module Kakaxi
4
+ module Data
5
+ class Rainfall < BaseData
6
+ attr_reader :unit
7
+
8
+ def initialize(value: nil, recorded_at: nil, interpolation: nil, unit: nil)
9
+ @unit = unit
10
+ super(value: value, recorded_at: recorded_at, interpolation: interpolation)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,15 @@
1
+ require 'kakaxi/data/base_data'
2
+
3
+ module Kakaxi
4
+ module Data
5
+ class SolarRadiation < BaseData
6
+ attr_reader :unit
7
+
8
+ def initialize(value: nil, recorded_at: nil, interpolation: nil, unit: nil)
9
+ @unit = unit
10
+ super(value: value, recorded_at: recorded_at, interpolation: interpolation)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,15 @@
1
+ require 'kakaxi/data/base_data'
2
+
3
+ module Kakaxi
4
+ module Data
5
+ class Temperature < BaseData
6
+ attr_reader :unit
7
+
8
+ def initialize(value: nil, recorded_at: nil, interpolation: nil, unit: nil)
9
+ @unit = unit
10
+ super(value: value, recorded_at: recorded_at, interpolation: interpolation)
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,18 @@
1
+ require 'kakaxi/data/base_data'
2
+
3
+ module Kakaxi
4
+ module Data
5
+ class Timelapse
6
+ attr_reader :id, :start_datetime, :end_datetime, :url, :thumbnail
7
+
8
+ def initialize(id: nil, start_datetime: nil, end_datetime: nil, url: nil, thumbnail_name: nil, thumbnail_url: nil)
9
+ @id = id
10
+ @start_datetime = DateTime.strptime(start_datetime, '%Y-%m-%dT%H:%M:%SZ')
11
+ @end_datetime = DateTime.strptime(end_datetime, '%Y-%m-%dT%H:%M:%SZ')
12
+ @url = URI.parse(url)
13
+ @thumbnail = { name: thumbnail_name, url: thumbnail_url }
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,11 @@
1
+ module Kakaxi
2
+ class Device
3
+ attr_accessor :id, :name
4
+
5
+ def initialize(id: nil, name: nil)
6
+ @id = id
7
+ @name = name
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,10 @@
1
+ module Kakaxi
2
+ class Farm
3
+ attr_accessor :id, :name
4
+
5
+ def initialize(id: nil, name: nil)
6
+ @id = id
7
+ @name = name
8
+ end
9
+ end
10
+ end
data/lib/kakaxi.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'exception/invalid_credentials'
2
+ require 'kakaxi/farm'
3
+ require 'kakaxi/device'
4
+ require 'kakaxi/data/data'
5
+ require 'kakaxi/client'
6
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kakaxi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Naggi Goishi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Make it easy to use Kakaxi API by ruby
14
+ email: naggi@kakaxi.jp
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/exception/invalid_credentials.rb
20
+ - lib/kakaxi.rb
21
+ - lib/kakaxi/client.rb
22
+ - lib/kakaxi/data/base_data.rb
23
+ - lib/kakaxi/data/data.rb
24
+ - lib/kakaxi/data/humidity.rb
25
+ - lib/kakaxi/data/rainfall.rb
26
+ - lib/kakaxi/data/solar_radiation.rb
27
+ - lib/kakaxi/data/temperature.rb
28
+ - lib/kakaxi/data/timelapse.rb
29
+ - lib/kakaxi/device.rb
30
+ - lib/kakaxi/farm.rb
31
+ homepage: https://kakaxi-data.me/doc#v1-get
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.6.13
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Library for calling kakaxi api by ruby
55
+ test_files: []