tanita-api-client 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0325b0624c8b82bbec750574aaf9330352520598be1a35d7992389807477af0d
4
- data.tar.gz: d976f86f33c4e72c89009dd5e474d62834c570316ebb4ce712c379aa04b1792c
3
+ metadata.gz: fb992058a0dd7fdb3add0db60205e370a8d167416324ed7339b32ef0c588f98e
4
+ data.tar.gz: 2e1ce4e7562d1eeec91516edb07df7447fa730c980c61891cbe1d5f122c39abb
5
5
  SHA512:
6
- metadata.gz: c7a6f768eedd5365e0381162ebe5f40f3dda5ca70b5c7623dd7c5b1d3a99990181ffa27f75a73d41895cbb470b9d40db125885bc86463ff3585e4d845e85745c
7
- data.tar.gz: acd0893e68b74525c66b8834df29d00f94c006b0845ec607f773015776612f5f2245432069087306bc0305a333e814bf6896dce44e8f1a9f8814e86c9fb0c34a
6
+ metadata.gz: 3c5215df1edcc0cea3ddc324b1ed99ba0791a3b3fd5b8ac1fdf8433402248fd820ebf38427f3cf8f24a269a63f38f7351c03902b625dad0432e50c02736a2898
7
+ data.tar.gz: 770dc23e29bfa3f054957c7de11f3778d1211616608bba427808c7acf95d3f117ef935db67adbf39181db6e317ccb60aa05526be72ea16bb994c600e708a769f
@@ -1,4 +1,14 @@
1
1
 
2
+ # 0.2.0
3
+
4
+ * set required ruby version to greater than or equal to v2.4
5
+ * added `data_type` argument in BaseApiClient#initialize
6
+ * set a proper data type in Result class attributes
7
+ * rename some attributes in Result class
8
+ * e.g.
9
+ * `@data` to `@items`
10
+ * `@data[0][:date]` to `@items[0][:measured_at]` or `@items[0][:registered_at]`
11
+
2
12
  # 0.1.1
3
13
 
4
14
  * support Tanita::Api::Client.configure
@@ -6,4 +16,4 @@
6
16
 
7
17
  # 0.1.0
8
18
 
9
- * Initial release
19
+ * Initial release
data/Gemfile CHANGED
@@ -2,11 +2,3 @@ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in tanita-api-client.gemspec
4
4
  gemspec
5
-
6
- group :development do
7
- gem 'rubocop', require: false
8
- end
9
-
10
- group :test do
11
- gem 'webmock', require: false
12
- end
@@ -1,25 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tanita-api-client (0.1.1)
4
+ tanita-api-client (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  addressable (2.7.0)
10
10
  public_suffix (>= 2.0.2, < 5.0)
11
- ast (2.4.0)
12
11
  crack (0.4.3)
13
12
  safe_yaml (~> 1.0.0)
14
13
  diff-lcs (1.3)
15
14
  hashdiff (1.0.0)
16
- jaro_winkler (1.5.4)
17
- parallel (1.19.1)
18
- parser (2.7.0.1)
19
- ast (~> 2.4.0)
20
15
  public_suffix (4.0.3)
21
- rainbow (3.0.0)
22
- rake (10.5.0)
16
+ rake (12.3.3)
23
17
  rspec (3.9.0)
24
18
  rspec-core (~> 3.9.0)
25
19
  rspec-expectations (~> 3.9.0)
@@ -33,16 +27,7 @@ GEM
33
27
  diff-lcs (>= 1.2.0, < 2.0)
34
28
  rspec-support (~> 3.9.0)
35
29
  rspec-support (3.9.2)
36
- rubocop (0.78.0)
37
- jaro_winkler (~> 1.5.1)
38
- parallel (~> 1.10)
39
- parser (>= 2.6)
40
- rainbow (>= 2.2.2, < 4.0)
41
- ruby-progressbar (~> 1.7)
42
- unicode-display_width (>= 1.4.0, < 1.7)
43
- ruby-progressbar (1.10.1)
44
30
  safe_yaml (1.0.5)
45
- unicode-display_width (1.6.0)
46
31
  webmock (3.7.6)
47
32
  addressable (>= 2.3.6)
48
33
  crack (>= 0.3.2)
@@ -53,11 +38,10 @@ PLATFORMS
53
38
 
54
39
  DEPENDENCIES
55
40
  bundler (~> 2.0)
56
- rake (~> 10.0)
41
+ rake (~> 12.0)
57
42
  rspec (~> 3.0)
58
- rubocop
59
43
  tanita-api-client!
60
- webmock
44
+ webmock (~> 3.7.6)
61
45
 
62
46
  BUNDLED WITH
63
47
  2.1.3
data/README.md CHANGED
@@ -60,7 +60,7 @@ auth_helper = Tanita::Api::Client::Auth.new(
60
60
  Below is the sample get an authentication url and exchange access token from an authentication code.
61
61
 
62
62
  ```ruby
63
- # get Tanita authentication url
63
+ # get Health Planet authentication url
64
64
  auth_helper.auth_uri
65
65
  => "https://www.healthplanet.jp/oauth/auth?client_id=YOUR_ID&redirect_uri=http%3A%2F%2F127.0.0.1%2Fcallback&scope=innerscan&response_type=code"
66
66
 
@@ -91,21 +91,42 @@ result = api.status
91
91
  result = api.status(from: Date.current.ago(1.month), to: Date.current)
92
92
 
93
93
  # list the body-weight data
94
- result.data.each{|item| puts "#{item[:date]} #{item[:weight]}" }
95
- >> result.data.each{|item| puts "#{item[:date]} #{item[:weight]}" }
96
- 201910070753 66.70
97
- 201910070917 66.00
98
- 201910080739 66.40
99
- 201910090806 66.20
100
- 201910090852 65.50
101
- 201910100809 66.70
102
- 201910110902 66.50
103
- 201910130822 66.70
94
+ result.items.each{|item| puts "#{Time.at(item[:measured_at]).strftime('%F %R')} => #{item[:weight]}" }
95
+ 2019-10-10 08:09 => 66.7
96
+ 2019-10-11 09:02 => 66.5
97
+ 2019-10-13 08:22 => 66.7
98
+ 2019-10-15 08:49 => 66.4
99
+ 2019-10-17 07:52 => 67.0
100
+
101
+ # Result of Innerscan Api
102
+ result = Tanita::Api::Client::Innerscan.new.status
103
+ result.items[0].keys
104
+ => [:measured_at, :model, :weight, :body_fat, :muscle_mass, :physique_rating, :visceral_fat_rating, :basal_metabolic_rate, :metabolic_age, :bone_mass]
105
+
106
+ # Result of Sphygmomanometer Api
107
+ result = Tanita::Api::Client::Sphygmomanometer.new.status
108
+ result.items[0].keys
109
+ => [:measured_at, :model, :maximal_pressure, :minimal_pressure, :pulse]
110
+
111
+ # Result of Pedometer Api
112
+ result = Tanita::Api::Client::Pedometer.new.status
113
+ result.items[0].keys
114
+ => [:measured_at, :model, :steps, :calories]
115
+
116
+ # Result of Smug Api
117
+ result = Tanita::Api::Client::Smug.new.status
118
+ result.items[0].keys
119
+ => [:measured_at, :model, :urinary_sugar]
120
+
121
+ # common attributes of Result class
122
+ result.birth_date # [Date]
123
+ result.height # [Float] (centimeter)
124
+ result.sex # [String] 'male' or 'female'
104
125
  ```
105
126
 
106
127
  ## Contributing
107
128
 
108
- Bug reports and pull requests are welcome on GitHub at https://github.com/koshilife/tanita-api-ruby-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
129
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/koshilife/tanita-api-ruby-client). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
109
130
 
110
131
  ## License
111
132
 
@@ -73,15 +73,14 @@ module Tanita
73
73
 
74
74
  def measurement_tags
75
75
  {
76
- :weight => '6021',
77
- :body_fat => '6022',
78
- :muscle_mass => '6023',
79
- :physique_rating => '6024',
80
- :visceral_fat_rating2 => '6025',
81
- :visceral_fat_rating1 => '6026',
82
- :basal_metabolic_rate => '6027',
83
- :metabolic_age => '6028',
84
- :bone_mass => '6029'
76
+ :weight => {:code => '6021', :type => Float},
77
+ :body_fat => {:code => '6022', :type => Float},
78
+ :muscle_mass => {:code => '6023', :type => Float},
79
+ :physique_rating => {:code => '6024', :type => Integer},
80
+ :visceral_fat_rating => {:code => '6025', :type => Float},
81
+ :basal_metabolic_rate => {:code => '6027', :type => Integer},
82
+ :metabolic_age => {:code => '6028', :type => Integer},
83
+ :bone_mass => {:code => '6029', :type => Float}
85
84
  }
86
85
  end
87
86
  end
@@ -93,9 +92,9 @@ module Tanita
93
92
 
94
93
  def measurement_tags
95
94
  {
96
- :maximal_pressure => '622E',
97
- :minimal_pressure => '622F',
98
- :pulse => '6230'
95
+ :maximal_pressure => {:code => '622E', :type => Integer},
96
+ :minimal_pressure => {:code => '622F', :type => Integer},
97
+ :pulse => {:code => '6230', :type => Integer}
99
98
  }
100
99
  end
101
100
  end
@@ -107,9 +106,9 @@ module Tanita
107
106
 
108
107
  def measurement_tags
109
108
  {
110
- :steps => '6331',
111
- :exercise => '6335',
112
- :calories => '6336'
109
+ :steps => {:code => '6331', :type => Integer},
110
+ :exercise => {:code => '6335', :type => Integer},
111
+ :calories => {:code => '6336', :type => Integer}
113
112
  }
114
113
  end
115
114
  end
@@ -121,7 +120,7 @@ module Tanita
121
120
 
122
121
  def measurement_tags
123
122
  {
124
- :urinary_sugar => '6240'
123
+ :urinary_sugar => {:code => '6240', :type => Integer}
125
124
  }
126
125
  end
127
126
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'time'
3
4
  require 'tanita/api/client/helpers'
4
5
 
5
6
  module Tanita
@@ -18,33 +19,35 @@ module Tanita
18
19
  class Error < StandardError
19
20
  end
20
21
 
22
+ DATE_TYPE_REGISTERD_AT = 0
23
+ DATE_TYPE_MEASURED_AT = 1
24
+
21
25
  class BaseApiClient
22
26
  include HttpHelper
23
27
 
24
- DATE_REGISTERD_AT = 0
25
- DATE_MEASURED_AT = 1
26
-
27
- def initialize(access_token: nil)
28
+ def initialize(access_token: nil, date_type: DATE_TYPE_MEASURED_AT)
28
29
  config = Tanita::Api::Client.configuration
29
30
  @access_token = access_token || config.access_token
30
31
  raise Error.new("param:'access_token' is required.'") if @access_token.nil?
32
+
33
+ @date_type = date_type
34
+ raise Error.new("param:'date_type' is invalid.'") unless [DATE_TYPE_REGISTERD_AT, DATE_TYPE_MEASURED_AT].include? date_type
31
35
  end
32
36
 
33
37
  def status(
34
- date_type: DATE_MEASURED_AT,
35
38
  from: nil,
36
39
  to: nil
37
40
  )
38
- tags = measurement_tags.values.join(',')
41
+ tags = measurement_tags.values.map { |i| i[:code] }.join(',')
39
42
  params = {
40
43
  :access_token => @access_token,
41
- :date => date_type,
44
+ :date => @date_type,
42
45
  :tag => tags
43
46
  }
44
47
  params[:from] = time_format(from) unless from.nil?
45
48
  params[:to] = time_format(to) unless to.nil?
46
49
  res = request(endpoint, params)
47
- Result.new(:response => res, :client => self)
50
+ Result.new(:client => self, :response => res)
48
51
  end
49
52
 
50
53
  def endpoint
@@ -55,6 +58,25 @@ module Tanita
55
58
  raise NotImplementedError
56
59
  end
57
60
 
61
+ def find_measurement_tag(code:)
62
+ return @inverted_measurement[code] unless @inverted_measurement.nil?
63
+
64
+ @inverted_measurement = {}
65
+ measurement_tags.each do |m_name, m_info|
66
+ @inverted_measurement[m_info[:code]] = {:name => m_name, :type => m_info[:type]}
67
+ end
68
+ @inverted_measurement[code]
69
+ end
70
+
71
+ def date_key
72
+ case @date_type
73
+ when DATE_TYPE_REGISTERD_AT
74
+ :registered_at
75
+ when DATE_TYPE_MEASURED_AT
76
+ :measured_at
77
+ end
78
+ end
79
+
58
80
  private
59
81
 
60
82
  def time_format(time)
@@ -64,39 +86,53 @@ module Tanita
64
86
 
65
87
  class Result
66
88
  include HttpHelper
89
+
90
+ # [Date]
67
91
  attr_reader :birth_date
92
+
93
+ # [Float] (centimeter)
68
94
  attr_reader :height
95
+
96
+ # [String] 'male' or 'female'
69
97
  attr_reader :sex
70
- attr_reader :data
71
98
 
72
- def initialize(response:, client:)
99
+ # [Array<Hash>]
100
+ attr_reader :items
101
+
102
+ def initialize(client:, response:)
103
+ @client = client
73
104
  result = parse_json(response.body)
74
- @birth_date = result[:birth_date]
75
- @height = result[:height]
105
+ @birth_date = Date.parse(result[:birth_date])
106
+ @height = result[:height].to_f
76
107
  @sex = result[:sex]
77
-
78
- mapper = client.measurement_tags.invert
79
- set_data(result[:data], mapper)
108
+ @items = build_items(result[:data])
80
109
  end
81
110
 
82
111
  private
83
112
 
84
- def set_data(items, mapper)
85
- data_dic = {}
86
- items.each do |item|
113
+ def build_items(raw_items)
114
+ item_dic = {}
115
+ raw_items.each do |item|
87
116
  date = item[:date]
88
117
  model = item[:model]
89
118
  key = "#{date}_#{model}"
90
- measurement = mapper[item[:tag]]
91
- value = item[:keydata]
92
-
93
- data_dic[key] ||= {}
94
- data_dic[key][:date] = date unless data_dic[key].key? :date
95
- data_dic[key][:model] = model unless data_dic[key].key? :model
96
- data_dic[key][measurement] = value
119
+ measurement = @client.find_measurement_tag(:code => item[:tag])
120
+ value = cast(:value => item[:keydata], :type => measurement[:type])
121
+ item_dic[key] ||= {}
122
+ item_dic[key][@client.date_key] = Time.parse("#{date} +09:00").to_i unless item_dic[key].key? :date
123
+ item_dic[key][:model] = model unless item_dic[key].key? :model
124
+ item_dic[key][measurement[:name]] = value
97
125
  end
98
126
  # sort by date in ascending order
99
- @data = data_dic.values.sort_by { |dic| dic[:date] }
127
+ item_dic.values.sort_by { |dic| dic[@client.date_key] }
128
+ end
129
+
130
+ def cast(value:, type:)
131
+ return value if value.nil?
132
+ return value.to_i if type == Integer
133
+ return value.to_f if type == Float
134
+
135
+ value
100
136
  end
101
137
  end
102
138
  end
@@ -3,7 +3,7 @@
3
3
  module Tanita
4
4
  module Api
5
5
  module Client
6
- VERSION = '0.1.1'
6
+ VERSION = '0.2.0'
7
7
  end
8
8
  end
9
9
  end
@@ -1,32 +1,36 @@
1
- lib = File.expand_path("lib", __dir__)
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
2
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "tanita/api/client/version"
5
+ require 'tanita/api/client/version'
4
6
 
5
7
  Gem::Specification.new do |spec|
6
- spec.name = "tanita-api-client"
8
+ spec.name = 'tanita-api-client'
7
9
  spec.version = Tanita::Api::Client::VERSION
8
- spec.authors = ["Kenji Koshikawa"]
9
- spec.email = ["koshikawa2009@gmail.com"]
10
+ spec.authors = ['Kenji Koshikawa']
11
+ spec.email = ['koshikawa2009@gmail.com']
10
12
 
11
13
  spec.description = 'Client for accessing Tanita Health Planet APIs'
12
14
  spec.summary = spec.description
13
15
  spec.homepage = 'https://github.com/koshilife/tanita-api-ruby-client'
14
- spec.license = "MIT"
16
+ spec.license = 'MIT'
17
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
15
18
 
16
- spec.metadata["homepage_uri"] = spec.homepage
17
- spec.metadata["source_code_uri"] = "https://github.com/koshilife/tanita-api-ruby-client"
18
- spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/CHANGELOG.md"
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/koshilife/tanita-api-ruby-client'
21
+ spec.metadata['changelog_uri'] = "#{spec.metadata['source_code_uri']}/blob/master/CHANGELOG.md"
19
22
 
20
23
  # Specify which files should be added to the gem when it is released.
21
24
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
27
  end
25
- spec.bindir = "exe"
28
+ spec.bindir = 'exe'
26
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
- spec.require_paths = ["lib"]
30
+ spec.require_paths = ['lib']
28
31
 
29
- spec.add_development_dependency "bundler", "~> 2.0"
30
- spec.add_development_dependency "rake", "~> 10.0"
31
- spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency 'bundler', '~> 2.0'
33
+ spec.add_development_dependency 'rake', '~> 12.0'
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
+ spec.add_development_dependency 'webmock', '~> 3.7.6'
32
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tanita-api-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenji Koshikawa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-06 00:00:00.000000000 Z
11
+ date: 2020-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '12.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '10.0'
40
+ version: '12.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.7.6
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.7.6
55
69
  description: Client for accessing Tanita Health Planet APIs
56
70
  email:
57
71
  - koshikawa2009@gmail.com
@@ -62,9 +76,7 @@ files:
62
76
  - ".gitignore"
63
77
  - ".rspec"
64
78
  - ".rubocop.yml"
65
- - ".ruby-version"
66
79
  - ".travis.yml"
67
- - ".vscode/settings.json"
68
80
  - CHANGELOG.md
69
81
  - CODE_OF_CONDUCT.md
70
82
  - Gemfile
@@ -95,14 +107,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
107
  requirements:
96
108
  - - ">="
97
109
  - !ruby/object:Gem::Version
98
- version: '0'
110
+ version: 2.4.0
99
111
  required_rubygems_version: !ruby/object:Gem::Requirement
100
112
  requirements:
101
113
  - - ">="
102
114
  - !ruby/object:Gem::Version
103
115
  version: '0'
104
116
  requirements: []
105
- rubygems_version: 3.0.3
117
+ rubygems_version: 3.1.2
106
118
  signing_key:
107
119
  specification_version: 4
108
120
  summary: Client for accessing Tanita Health Planet APIs
@@ -1 +0,0 @@
1
- 2.6.3
@@ -1,14 +0,0 @@
1
- {
2
- "ruby.format": "rubocop",
3
- "ruby.lint": {
4
- "rubocop": true
5
- },
6
- "[ruby]": {
7
- "editor.formatOnSave": true,
8
- "editor.formatOnSaveTimeout": 10000,
9
- },
10
- "[javascript]": {
11
- "editor.formatOnSave": true,
12
- "editor.formatOnSaveTimeout": 10000,
13
- }
14
- }