tanita-api-client 0.1.0 → 0.1.1

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: 7276f5bfe20571b5e467cb6723f40df02c52d7287de11dca0eb40009487c3e21
4
- data.tar.gz: 44d0295e8ed1d822e5cc2dc60e0b2222fa046970cde0973b1bdc3372d930f0d4
3
+ metadata.gz: 0325b0624c8b82bbec750574aaf9330352520598be1a35d7992389807477af0d
4
+ data.tar.gz: d976f86f33c4e72c89009dd5e474d62834c570316ebb4ce712c379aa04b1792c
5
5
  SHA512:
6
- metadata.gz: b306fa0d89dcfe58209130baf00f0f051cb34c8c6a79ffdee50740f70e29dacf52b8b4d51e01aaa7a3cf0ab03e303c750a8a600a9efa9aa683a3bc32c506d20b
7
- data.tar.gz: 43b9326709bf1f594bea7270155730139cd1d7e6d0bec99b3eb01f258e5be97987ca72ade4214f63fb0d9fc622be9cfa063c65f5a99ca3b8aba76ea14a959969
6
+ metadata.gz: c7a6f768eedd5365e0381162ebe5f40f3dda5ca70b5c7623dd7c5b1d3a99990181ffa27f75a73d41895cbb470b9d40db125885bc86463ff3585e4d845e85745c
7
+ data.tar.gz: acd0893e68b74525c66b8834df29d00f94c006b0845ec607f773015776612f5f2245432069087306bc0305a333e814bf6896dce44e8f1a9f8814e86c9fb0c34a
@@ -1,3 +1,9 @@
1
+
2
+ # 0.1.1
3
+
4
+ * support Tanita::Api::Client.configure
5
+ * wrote spec
6
+
1
7
  # 0.1.0
2
8
 
3
9
  * Initial release
data/Gemfile CHANGED
@@ -3,6 +3,10 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in tanita-api-client.gemspec
4
4
  gemspec
5
5
 
6
- group :development, :test do
6
+ group :development do
7
7
  gem 'rubocop', require: false
8
8
  end
9
+
10
+ group :test do
11
+ gem 'webmock', require: false
12
+ end
@@ -1,17 +1,23 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tanita-api-client (0.1.0)
4
+ tanita-api-client (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ addressable (2.7.0)
10
+ public_suffix (>= 2.0.2, < 5.0)
9
11
  ast (2.4.0)
12
+ crack (0.4.3)
13
+ safe_yaml (~> 1.0.0)
10
14
  diff-lcs (1.3)
15
+ hashdiff (1.0.0)
11
16
  jaro_winkler (1.5.4)
12
17
  parallel (1.19.1)
13
18
  parser (2.7.0.1)
14
19
  ast (~> 2.4.0)
20
+ public_suffix (4.0.3)
15
21
  rainbow (3.0.0)
16
22
  rake (10.5.0)
17
23
  rspec (3.9.0)
@@ -35,7 +41,12 @@ GEM
35
41
  ruby-progressbar (~> 1.7)
36
42
  unicode-display_width (>= 1.4.0, < 1.7)
37
43
  ruby-progressbar (1.10.1)
44
+ safe_yaml (1.0.5)
38
45
  unicode-display_width (1.6.0)
46
+ webmock (3.7.6)
47
+ addressable (>= 2.3.6)
48
+ crack (>= 0.3.2)
49
+ hashdiff (>= 0.4.0, < 2.0.0)
39
50
 
40
51
  PLATFORMS
41
52
  ruby
@@ -46,6 +57,7 @@ DEPENDENCIES
46
57
  rspec (~> 3.0)
47
58
  rubocop
48
59
  tanita-api-client!
60
+ webmock
49
61
 
50
62
  BUNDLED WITH
51
63
  2.1.3
data/README.md CHANGED
@@ -1,7 +1,18 @@
1
1
  # Tanita::Api::Client
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tanita/api/client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
3
 
4
+ ## About
5
+
6
+ These client libraries are created for [Tanita Health Planet](https://www.healthplanet.jp/) APIs.
7
+
8
+ refs: [Health Planet API Doc](https://www.healthplanet.jp/apis/api.html) (only Japanese)
9
+
10
+ ### Setup
11
+
12
+ There are a few setup steps you need to complete before you can use this library:
13
+
14
+ 1. If you don't already have a Health Planet account, [sign up](https://www.healthplanet.jp/entry_agreement.do).
15
+ 2. If you have never created a developer application, read the [API Settings page](https://www.healthplanet.jp/apis_account.do) and be enable API.
5
16
 
6
17
  ## Installation
7
18
 
@@ -21,13 +32,76 @@ Or install it yourself as:
21
32
 
22
33
  ## Usage
23
34
 
24
- TODO: Write usage instructions here
35
+ ### Usage OAuth Helper
25
36
 
26
- ## Development
37
+ The library needs to be configured with your account's OAuth settings which is available in your Health Planet Api Settings page.
38
+ Set `client_id` `client_secret` `redirect_uri` `scopes` to its value:
39
+
40
+ ```ruby
41
+
42
+ # OAuth configuration using configure method
43
+ Tanita::Api::Client.configure do |config|
44
+ config.client_id = '<YOUR_CLIENT_ID>'
45
+ config.client_secret = '<YOUR_CLIENT_SECRET>'
46
+ config.redirect_uri = 'http://your-redirect-uri'
47
+ config.scopes = [Tanita::Api::Client::Scope::INNERSCAN]
48
+ end
49
+ auth_helper = Tanita::Api::Client::Auth.new
50
+
51
+ # OAuth configuration using initializer
52
+ auth_helper = Tanita::Api::Client::Auth.new(
53
+ client_id: '<YOUR_CLIENT_ID>',
54
+ client_secret: '<YOUR_CLIENT_SECRET>',
55
+ redirect_uri: 'http://your-redirect-uri',
56
+ scopes: Tanita::Api::Client::Scope.all
57
+ )
58
+ ```
27
59
 
28
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+ Below is the sample get an authentication url and exchange access token from an authentication code.
29
61
 
30
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+ ```ruby
63
+ # get Tanita authentication url
64
+ auth_helper.auth_uri
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
+
67
+ # get access token
68
+ token = auth_helper.exchange_token(auth_code: '<AUTHENTICATION_CODE>')
69
+ => {:access_token=>"hoge_access_token", :expires_in=>12345678, :refresh_token=>"hoge_refresh_token"}
70
+ ```
71
+
72
+ ### Usage Apis Client
73
+
74
+ The Api client needs access token.
75
+ Set `access_token` to the value you got by above:
76
+
77
+ ```ruby
78
+ # using configure
79
+ Tanita::Api::Client.configure do |config|
80
+ config.access_token = '<YOUR_ACCESS_TOKEN>'
81
+ end
82
+ api = Tanita::Api::Client::Innerscan.new
83
+
84
+ # using initializer
85
+ api = Tanita::Api::Client::Innerscan.new(access_token: '<YOUR_ACCESS_TOKEN>')
86
+
87
+ # fetch innerscan data
88
+ result = api.status
89
+
90
+ # you can specify a period using params(from:, to:)
91
+ result = api.status(from: Date.current.ago(1.month), to: Date.current)
92
+
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
104
+ ```
31
105
 
32
106
  ## Contributing
33
107
 
@@ -2,19 +2,32 @@
2
2
 
3
3
  require 'tanita/api/client/base'
4
4
  require 'tanita/api/client/helpers'
5
+ require 'tanita/api/client/configuration'
5
6
  require 'tanita/api/client/version'
6
7
 
7
8
  module Tanita
8
9
  module Api
9
10
  module Client
11
+ class << self
12
+ def configure
13
+ yield configuration
14
+ end
15
+
16
+ def configuration
17
+ @configuration ||= Tanita::Api::Client::Configuration.new
18
+ end
19
+ end
20
+
10
21
  class Auth
11
22
  include HttpHelper
12
23
 
13
- def initialize(client_id:, client_secret:, redirect_uri:, scopes: [Scope::INNERSCAN])
14
- @client_id = client_id
15
- @client_secret = client_secret
16
- @redirect_uri = redirect_uri
17
- @scopes = scopes
24
+ def initialize(client_id: nil, client_secret: nil, redirect_uri: nil, scopes: nil)
25
+ config = Tanita::Api::Client.configuration
26
+ @client_id = client_id || config.client_id
27
+ @client_secret = client_secret || config.client_secret
28
+ @redirect_uri = redirect_uri || config.redirect_uri
29
+ @scopes = scopes || config.scopes
30
+ check_required_arguments
18
31
  end
19
32
 
20
33
  def auth_uri
@@ -42,6 +55,15 @@ module Tanita
42
55
 
43
56
  token
44
57
  end
58
+
59
+ private
60
+
61
+ def check_required_arguments
62
+ raise Error.new("param:'client_id' is required.'") if @client_id.nil?
63
+ raise Error.new("param:'client_secret' is required.'") if @client_secret.nil?
64
+ raise Error.new("param:'redirect_uri' is required.'") if @redirect_uri.nil?
65
+ raise Error.new("param:'scopes' is required.'") if @scopes.nil?
66
+ end
45
67
  end
46
68
 
47
69
  class Innerscan < BaseApiClient
@@ -10,6 +10,9 @@ module Tanita
10
10
  SPHYGMOMANOMETER = 'sphygmomanometer'
11
11
  PEDOMETER = 'pedometer'
12
12
  SMUG = 'smug'
13
+ def self.all
14
+ constants.map { |name| const_get(name) }
15
+ end
13
16
  end
14
17
 
15
18
  class Error < StandardError
@@ -21,16 +24,10 @@ module Tanita
21
24
  DATE_REGISTERD_AT = 0
22
25
  DATE_MEASURED_AT = 1
23
26
 
24
- def initialize(access_token:)
25
- @access_token = access_token
26
- end
27
-
28
- def endpoint
29
- raise NotImplementedError
30
- end
31
-
32
- def measurement_tags
33
- raise NotImplementedError
27
+ def initialize(access_token: nil)
28
+ config = Tanita::Api::Client.configuration
29
+ @access_token = access_token || config.access_token
30
+ raise Error.new("param:'access_token' is required.'") if @access_token.nil?
34
31
  end
35
32
 
36
33
  def status(
@@ -46,11 +43,18 @@ module Tanita
46
43
  }
47
44
  params[:from] = time_format(from) unless from.nil?
48
45
  params[:to] = time_format(to) unless to.nil?
49
-
50
46
  res = request(endpoint, params)
51
47
  Result.new(:response => res, :client => self)
52
48
  end
53
49
 
50
+ def endpoint
51
+ raise NotImplementedError
52
+ end
53
+
54
+ def measurement_tags
55
+ raise NotImplementedError
56
+ end
57
+
54
58
  private
55
59
 
56
60
  def time_format(time)
@@ -92,7 +96,7 @@ module Tanita
92
96
  data_dic[key][measurement] = value
93
97
  end
94
98
  # sort by date in ascending order
95
- @data = data_dic.values { |dic| -dic[:date] }
99
+ @data = data_dic.values.sort_by { |dic| dic[:date] }
96
100
  end
97
101
  end
98
102
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tanita
4
+ module Api
5
+ module Client
6
+ class Configuration
7
+ # [String]
8
+ attr_accessor :client_id
9
+
10
+ # [String]
11
+ attr_accessor :client_secret
12
+
13
+ # [String]
14
+ attr_accessor :redirect_uri
15
+
16
+ # [String]
17
+ attr_accessor :access_token
18
+
19
+ # [Array<Tanita::Api::Client::Scope>]
20
+ attr_accessor :scopes
21
+ end
22
+ end
23
+ end
24
+ end
@@ -3,7 +3,7 @@
3
3
  module Tanita
4
4
  module Api
5
5
  module Client
6
- VERSION = '0.1.0'
6
+ VERSION = '0.1.1'
7
7
  end
8
8
  end
9
9
  end
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
15
15
 
16
16
  spec.metadata["homepage_uri"] = spec.homepage
17
17
  spec.metadata["source_code_uri"] = "https://github.com/koshilife/tanita-api-ruby-client"
18
- spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/CHANGELOG.md"
18
+ spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/CHANGELOG.md"
19
19
 
20
20
  # Specify which files should be added to the gem when it is released.
21
21
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
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.0
4
+ version: 0.1.1
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-05 00:00:00.000000000 Z
11
+ date: 2020-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,6 +76,7 @@ files:
76
76
  - bin/setup
77
77
  - lib/tanita/api/client.rb
78
78
  - lib/tanita/api/client/base.rb
79
+ - lib/tanita/api/client/configuration.rb
79
80
  - lib/tanita/api/client/helpers.rb
80
81
  - lib/tanita/api/client/version.rb
81
82
  - tanita-api-client.gemspec
@@ -85,7 +86,7 @@ licenses:
85
86
  metadata:
86
87
  homepage_uri: https://github.com/koshilife/tanita-api-ruby-client
87
88
  source_code_uri: https://github.com/koshilife/tanita-api-ruby-client
88
- changelog_uri: https://github.com/koshilife/tanita-api-ruby-client/CHANGELOG.md
89
+ changelog_uri: https://github.com/koshilife/tanita-api-ruby-client/blob/master/CHANGELOG.md
89
90
  post_install_message:
90
91
  rdoc_options: []
91
92
  require_paths: