ondotori-ruby-client 0.0.1 → 1.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 335ed96b6ab74f8124f60984363e39c1bf535f48ef37f18406cf7e3f43d58f86
4
- data.tar.gz: 4cbfa18a83fd71bd8d29f5a8ca39f63251263c6849878d76304162fa0f4f653e
3
+ metadata.gz: 442594d52153a81560cbbf8a79fcda7cf1ab6034c54367257e6d79249904e84b
4
+ data.tar.gz: b5178b2d79592f93caa2263957977ac7b0962ba158901306768417b5882c97a4
5
5
  SHA512:
6
- metadata.gz: b1a6b1a13d2e82386c97d7b2fd6dc2702dbfe87c3172b25f1dfcb69209842c56dd3a2eabe98e42a8cdf0f6bee97da6480982bcf0a6319367d323bb029ddc3647
7
- data.tar.gz: 58cf6d9f26bb298d4d5869b681a171e9076909815a5dc033fcea148f5f3e5c6f24f3f5d3c74416113f8c783f05466fa170f25159423752946ba4f71c169b84fc
6
+ metadata.gz: 2f5925a8a7843296922668e9168e799bf1fc6522c6c6104320555219aace442eef1c671d2e55e80e38c9f6d15d60f2444ba0b630c2793f9487f7350cae57562e
7
+ data.tar.gz: bf580712e184dc1e9f7942dac11a4b745c488afa43d38ec6df1d2c3901380a32732724098eb05e10b644090875c9eec1eaa505f20926be760f7c691719a23eed
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ Gemfile.lock
data/.rubocop.yml CHANGED
@@ -36,7 +36,7 @@ Style/GuardClause:
36
36
 
37
37
  Naming/FileName:
38
38
  Exclude:
39
- - lib/ondotori-web-client.rb
39
+ - lib/ondotori-ruby-client.rb
40
40
 
41
41
 
42
42
  # Offense count: 14
@@ -44,6 +44,9 @@ Naming/FileName:
44
44
  Metrics/MethodLength:
45
45
  Max: 32
46
46
 
47
+ Metrics/ParameterLists:
48
+ Max: 6
49
+
47
50
  # Offence count: 10
48
51
  Metrics/AbcSize:
49
52
  Max: 39
data/CHANGELOG.md CHANGED
@@ -1,4 +1,12 @@
1
- ## [Unreleased]
1
+ ## [Released]
2
+
3
+ ## [1.1.0] - 2021-04-17
4
+
5
+ - Support for getting RateLimit when an error occurs.
6
+
7
+ ## [1.0.0] - 2021-03-04
8
+
9
+ - Support v1 all API
2
10
 
3
11
  ## [0.1.0] - 2021-02-18
4
12
 
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Ruby](https://github.com/k28/ondotori-ruby-client/actions/workflows/main.yml/badge.svg)](https://github.com/k28/ondotori-ruby-client/actions/workflows/main.yml)
2
+
1
3
  ## Installation
2
4
 
3
5
  Add this line to your application's Gemfile:
@@ -27,7 +29,7 @@ The first step is to create an API key.
27
29
 
28
30
  #### Web Client Examples
29
31
 
30
- ```
32
+ ```ruby
31
33
  params = { "api-key" => "API Key you create", "login-id" => "tbxxxx", "login-pass" => "password"}
32
34
  client = Ondotori::WebAPI::Client.new(params)
33
35
  ```
@@ -43,27 +45,73 @@ client = Ondotori::WebAPI::Client.new(params)
43
45
 
44
46
  To get current readings, do the following.
45
47
 
46
- ```
47
- params = { "api-key" => "API Key you create", "login-id" => "tbxxxx", "login-pass" => "password"}
48
- client = Ondotori::WebAPI::Client.new(params)
48
+ ```ruby
49
49
  response = client.current()
50
50
  ```
51
51
 
52
+ #### Get Latest Data
53
+
54
+ To get latest data, do the following.
55
+
56
+ ```ruby
57
+ response = client.latest_data("SERIAL")
58
+ ```
59
+
60
+ #### Get Latest Data RTR500
61
+
62
+ To get latest data (RTR500), do the following.
63
+
64
+ ```ruby
65
+ response = client.latest_data_rtr500(base: "BaseUnit Serial", remote: "RemoteUnit Serial")
66
+ ```
67
+
68
+ #### Get Data (TR-7wb/nw/wf, TR4)
69
+
70
+ To get data (TR-7wb/nw/wf, TR4), do the following.
71
+
72
+ ```ruby
73
+ from = Time.now - (3600 * 24)
74
+ to = Time.now
75
+ limit = 16000
76
+ data_range = Ondotori::WebAPI::Api::DataRange.new(from: from, to: to, limit: 10)
77
+ response = client.data("Device Serial", data_range: data_range)
78
+ ```
79
+
80
+ data_range parameter is optional.
81
+
82
+ #### Get Data (RTR500 Series)
83
+
84
+ To get data (RTR500 Series), do the following.
85
+
86
+ ```ruby
87
+ from = Time.now - (3600 * 24)
88
+ to = Time.now
89
+ limit = 16000
90
+ data_range = Ondotori::WebAPI::Api::DataRange.new(from: from, to: to, limit: 10)
91
+ response = client.data_rtr500(base: "BaseUnit Serial", remote: "Device Serial", data_range: data_range)
92
+ ```
93
+
94
+ data_range parameter is optional.
95
+
52
96
  #### Error Handling
53
97
 
54
98
  Ondotori Errors
55
99
 
56
100
  In case of parameter abnormality or error returned from the web server, the error will be raised.
57
101
  For example, to receive an authentication error from the server, use the following.
58
- ```
102
+
103
+ ```ruby
59
104
  rescue Ondotori::WebAPI::Api::Errors::ResponseError => e
60
- # puts "Response error #{e.messaeg}"
105
+ puts "Response error #{e.message}"
61
106
  end
62
107
  ```
108
+
109
+ If you reach the limit, you can check it with ResponseError.ratelimit.
63
110
  All of these errors inherit from `Ondotori::WebAPI::Api::Errors::Error`, so you can handle or silence all errors if necessary:
64
- ```
111
+
112
+ ```ruby
65
113
  rescue Ondotori::WebAPI::Api::Errors::Error => e
66
- # puts "Response error #{e.messaeg}"
114
+ puts "An error has occurred #{e.message}"
67
115
  end
68
116
  ```
69
117
 
@@ -75,7 +123,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
75
123
 
76
124
  ## Contributing
77
125
 
78
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ondotori-ruby-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ondotori-ruby-client/blob/master/CODE_OF_CONDUCT.md).
126
+ Bug reports and pull requests are welcome on GitHub at https://github.com/k28/ondotori-ruby-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ondotori-ruby-client/blob/master/CODE_OF_CONDUCT.md).
79
127
 
80
128
  ## License
81
129
 
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+ require "ondotori-ruby-client"
3
+
4
+ WEB_STORAGE_ACCESS_INFO_PATH = "/var/tmp/webstorage.json"
5
+ # The webstorage.json looks like the following.
6
+ # {
7
+ # "api-key":"T&D WebStorage API Key",
8
+ # "login-id" : "rbacxxxx",
9
+ # "login-pass" : "password"
10
+ # }
11
+
12
+ # Reads the information for API access from the file.
13
+ def load_params
14
+ File.open(WEB_STORAGE_ACCESS_INFO_PATH) do |file|
15
+ storage_info = file.read
16
+ load_info = JSON.parse(storage_info)
17
+
18
+ # This is not necessary, but is left for the explanation of the parameter settings.
19
+ wss_access_info = {}
20
+ wss_access_info["api-key"] = load_info["api-key"]
21
+ wss_access_info["login-id"] = load_info["login-id"]
22
+ wss_access_info["login-pass"] = load_info["login-pass"]
23
+ return wss_access_info
24
+ end
25
+ rescue SystemCallError => e
26
+ puts %(class=[#{e.class}] message=[#{e.message}])
27
+ rescue IOError => e
28
+ puts %(class=[#{e.class}] message=[#{e.message}])
29
+ end
30
+
31
+ def main
32
+ params = load_params
33
+ if params.nil?
34
+ puts "Load parameter error..."
35
+ return
36
+ end
37
+
38
+ %w[api-key login-id login-pass].each do |k|
39
+ if params[k].nil?
40
+ puts "parameter #{k} is nil. Please check #{WEB_STORAGE_ACCESS_INFO_PATH}"
41
+ exit
42
+ end
43
+ end
44
+
45
+ begin
46
+ client = Ondotori::WebAPI::Client.new(params)
47
+ response = client.current
48
+ puts "#{response}"
49
+ rescue Ondotori::WebAPI::Api::Errors::Error => e
50
+ puts "Some error happend #{e.message} #{e.code}"
51
+ end
52
+ end
53
+
54
+ main
@@ -7,5 +7,7 @@ require_relative "ondotori/webapi/httpwebaccess"
7
7
  require_relative "ondotori/webapi/api/errors"
8
8
  require_relative "ondotori/webapi/api/param"
9
9
  require_relative "ondotori/webapi/api/params"
10
+ require_relative "ondotori/webapi/api/rate_limit"
10
11
  require_relative "ondotori/webapi/api/response"
11
12
  require_relative "ondotori/webapi/api/uri"
13
+ require_relative "ondotori/webapi/api/data_range"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ondotori
4
- VERSION = "0.0.1"
4
+ VERSION = "1.1.0"
5
5
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ondotori
4
+ module WebAPI
5
+ module Api
6
+ class DataRange
7
+ attr_reader :from, :to, :limit
8
+
9
+ def initialize(from: nil, to: nil, limit: nil)
10
+ validate(from, to, limit)
11
+
12
+ @from = from
13
+ @to = to
14
+ @limit = limit.nil? ? 0 : [0, limit].max
15
+ end
16
+
17
+ def validate(from, to, _limit)
18
+ [from, to].each do |param|
19
+ next if param.nil? || param.instance_of?(Time)
20
+
21
+ raise Ondotori::WebAPI::Api::Errors::InvaildParameter.new(
22
+ "from and to parameter must be nil or Time.", 9992
23
+ )
24
+ end
25
+ end
26
+
27
+ def add_data_range(params)
28
+ params["unixtime-from"] = @from.to_i unless @from.nil?
29
+ params["unixtime-to"] = @to.to_i unless @to.nil?
30
+ params["number"] = @limit if @limit != 0
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -13,7 +13,14 @@ module Ondotori
13
13
  end
14
14
  end
15
15
 
16
- class ResponseError < Error; end
16
+ class ResponseError < Error
17
+ attr_reader :ratelimit
18
+
19
+ def initialize(message, code, ratelimit)
20
+ super message, code
21
+ @ratelimit = ratelimit
22
+ end
23
+ end
17
24
 
18
25
  class InitializeParameterNotFound < Error; end
19
26
 
@@ -3,9 +3,24 @@
3
3
  module Ondotori
4
4
  module WebAPI
5
5
  module Api
6
- class CurrentParams
7
- def initialize(param, remote: [], base: [])
6
+ class ParamsBase
7
+ def initialize(param)
8
8
  @param = param
9
+ end
10
+
11
+ def to_ondotori_param
12
+ params = {}
13
+ params[Api::Param::API_KEY] = @param.api_key
14
+ params[Api::Param::LOGIN_ID] = @param.login_id
15
+ params[Api::Param::LOGIN_PASS] = @param.login_pass
16
+
17
+ params
18
+ end
19
+ end
20
+
21
+ class CurrentParams < ParamsBase
22
+ def initialize(param, remote: [], base: [])
23
+ super(param)
9
24
 
10
25
  if remote.length.positive? && base.length.positive?
11
26
  raise Ondotori::WebAPI::Api::Errors::InvaildParameter.new(
@@ -17,10 +32,7 @@ module Ondotori
17
32
  end
18
33
 
19
34
  def to_ondotori_param
20
- params = {}
21
- params[Api::Param::API_KEY] = @param.api_key
22
- params[Api::Param::LOGIN_ID] = @param.login_id
23
- params[Api::Param::LOGIN_PASS] = @param.login_pass
35
+ params = super
24
36
  params["remote-serial"] = @remote_serial_list if @remote_serial_list.length.positive?
25
37
  params["base-serial"] = @base_serial_list if @base_serial_list.length.positive?
26
38
 
@@ -28,26 +40,93 @@ module Ondotori
28
40
  end
29
41
  end
30
42
 
31
- class LatestDataParams
32
- def initialize(_param, serial)
43
+ class LatestDataParams < ParamsBase
44
+ def initialize(param, serial: "")
45
+ super(param)
33
46
  if serial.empty?
34
47
  raise Ondotori::WebAPI::Api::Errors::InvaildParameter.new(
35
- "latest-data need remote-serial", 9998
48
+ "latest-data need remote-serial", 9994
36
49
  )
37
50
  end
38
51
  @remote_serial = serial
39
52
  end
40
53
 
41
54
  def to_ondotori_param
42
- params = {}
43
- params[Api::Param::API_KEY] = @param.api_key
44
- params[Api::Param::LOGIN_ID] = @param.login_id
45
- params[Api::Param::LOGIN_PASS] = @param.login_pass
55
+ params = super
56
+ params["remote-serial"] = @remote_serial
57
+
58
+ params
59
+ end
60
+ end
61
+
62
+ class LatestDataRTR500Params < ParamsBase
63
+ def initialize(param, base: "", remote: "")
64
+ super(param)
65
+ if base.empty? || remote.empty?
66
+ raise Ondotori::WebAPI::Api::Errors::InvaildParameter.new(
67
+ "latest-data-rtr500 need both the baseunit serial and remote unit serial.", 9993
68
+ )
69
+ end
70
+ @base_serial = base
71
+ @remote_serial = remote
72
+ end
73
+
74
+ def to_ondotori_param
75
+ params = super
76
+ params["base-serial"] = @base_serial
46
77
  params["remote-serial"] = @remote_serial
47
78
 
48
79
  params
49
80
  end
50
81
  end
82
+
83
+ class DataParams < ParamsBase
84
+ def initialize(param, serial, data_range: nil)
85
+ super(param)
86
+ validate(serial)
87
+ @data_range = data_range
88
+ @serial = serial
89
+ end
90
+
91
+ def validate(serial)
92
+ unless serial.instance_of?(String)
93
+ raise Ondotori::WebAPI::Api::Errors::InvaildParameter.new(
94
+ "serial must be String.", 9991
95
+ )
96
+ end
97
+ end
98
+
99
+ def to_ondotori_param
100
+ params = super
101
+ params["remote-serial"] = @serial
102
+ @data_range&.add_data_range(params)
103
+
104
+ params
105
+ end
106
+ end
107
+
108
+ class DataRTR500Params < DataParams
109
+ def initialize(param, serial, base, data_range: nil)
110
+ super(param, serial, data_range: data_range)
111
+ validate_base(base)
112
+ @base = base
113
+ end
114
+
115
+ def validate_base(base)
116
+ unless base.instance_of?(String)
117
+ raise Ondotori::WebAPI::Api::Errors::InvaildParameter.new(
118
+ "base unit serial must be String.", 9991
119
+ )
120
+ end
121
+ end
122
+
123
+ def to_ondotori_param
124
+ params = super
125
+ params["base-serial"] = @base
126
+
127
+ params
128
+ end
129
+ end
51
130
  end
52
131
  end
53
132
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ondotori
4
+ module WebAPI
5
+ module Api
6
+ class RateLimit
7
+ attr_reader :limit, :reset, :remaining
8
+
9
+ def initialize(response)
10
+ @limit = response.get_fields("X-RateLimit-Limit")
11
+ @reset = response.get_fields("X-RateLimit-Reset")
12
+ @remaining = response.get_fields("X-RateLimit-Remaining")
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -18,7 +18,8 @@ module Ondotori
18
18
  if result.key?("error")
19
19
  code = result["error"]["code"]
20
20
  message = result["error"]["message"]
21
- raise Ondotori::WebAPI::Api::Errors::ResponseError.new(message, code)
21
+ ratelimit = Ondotori::WebAPI::Api::RateLimit.new(@response)
22
+ raise Ondotori::WebAPI::Api::Errors::ResponseError.new(message, code, ratelimit)
22
23
  end
23
24
 
24
25
  # unknown error...
@@ -10,13 +10,37 @@ module Ondotori
10
10
  @uri = uri
11
11
  end
12
12
 
13
- def current(remote_serial_list: [], base_serial_list: [])
14
- current_param = Api::CurrentParams.new(@param, remote: remote_serial_list, base: base_serial_list)
15
- response = @web_access.access("#{base_uri}current", current_param.to_ondotori_param)
13
+ def access_server(param, uri)
14
+ response = @web_access.access(uri, param.to_ondotori_param)
16
15
  ondotori_response = Ondotori::WebAPI::Api::Response.new(response)
17
16
  ondotori_response.result
18
17
  end
19
18
 
19
+ def current(remote_serial_list: [], base_serial_list: [])
20
+ param = Api::CurrentParams.new(@param, remote: remote_serial_list, base: base_serial_list)
21
+ access_server(param, "#{base_uri}current")
22
+ end
23
+
24
+ def latest_data(serial)
25
+ param = Api::LatestDataParams.new(@param, serial: serial)
26
+ access_server(param, "#{base_uri}latest-data")
27
+ end
28
+
29
+ def latest_data_rtr500(base: "", remote: "")
30
+ param = Api::LatestDataRTR500Params.new(@param, base: base, remote: remote)
31
+ access_server(param, "#{base_uri}latest-data-rtr500")
32
+ end
33
+
34
+ def data(serial, data_range: nil)
35
+ param = Api::DataParams.new(@param, serial, data_range: data_range)
36
+ access_server(param, "#{base_uri}data")
37
+ end
38
+
39
+ def data_rtr500(base: "", remote: "", data_range: nil)
40
+ param = Api::DataRTR500Params.new(@param, remote, base, data_range: data_range)
41
+ access_server(param, "#{base_uri}data-rtr500")
42
+ end
43
+
20
44
  def base_uri
21
45
  return @uri unless @uri.empty?
22
46
 
@@ -23,7 +23,7 @@ module Ondotori
23
23
  when Net::HTTPClientError, Net::HTTPServerError
24
24
  response
25
25
  else
26
- # response.value raises Eception...
26
+ # response.value raises Exception...
27
27
  raise Ondotori::WebAPI::Api::Errors::HttpAccessError.new("#{response.message}", "#{response.code}", 9995)
28
28
  end
29
29
  end
@@ -13,14 +13,15 @@ module Ondotori
13
13
  end
14
14
 
15
15
  class StbWebAccess < WebAccess
16
- attr_reader :params
16
+ attr_reader :params, :uri
17
17
 
18
18
  def initialize(timeout, on_access)
19
19
  super timeout
20
20
  @on_access = on_access
21
21
  end
22
22
 
23
- def access(_uri, params)
23
+ def access(uri, params)
24
+ @uri = uri
24
25
  @params = params
25
26
  @on_access.call(self)
26
27
  end
@@ -9,9 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["k28@me.com"]
10
10
 
11
11
  spec.summary = "T&D WebStorage API Client"
12
- spec.description = <<-TXT.gsub(/^ +/, '')
13
- You can use this gem to get the recorded data from WebStorage.
14
- TXT
12
+ spec.description = "You can use this gem to get the recorded data from T&D WebStorage Service API."
15
13
  spec.homepage = "https://github.com/k28/ondotori-ruby-client"
16
14
  spec.license = "MIT"
17
15
  spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
metadata CHANGED
@@ -1,18 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ondotori-ruby-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuya Hatano
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-22 00:00:00.000000000 Z
11
+ date: 2021-04-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: 'You can use this gem to get the recorded data from WebStorage.
14
-
15
- '
13
+ description: You can use this gem to get the recorded data from T&D WebStorage Service
14
+ API.
16
15
  email:
17
16
  - k28@me.com
18
17
  executables: []
@@ -30,11 +29,14 @@ files:
30
29
  - Rakefile
31
30
  - bin/console
32
31
  - bin/setup
33
- - lib/ondotori-web-client.rb
32
+ - example/get_current_temp.rb
33
+ - lib/ondotori-ruby-client.rb
34
34
  - lib/ondotori/version.rb
35
+ - lib/ondotori/webapi/api/data_range.rb
35
36
  - lib/ondotori/webapi/api/errors.rb
36
37
  - lib/ondotori/webapi/api/param.rb
37
38
  - lib/ondotori/webapi/api/params.rb
39
+ - lib/ondotori/webapi/api/rate_limit.rb
38
40
  - lib/ondotori/webapi/api/response.rb
39
41
  - lib/ondotori/webapi/api/uri.rb
40
42
  - lib/ondotori/webapi/client.rb