xhp 0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/xhp.rb +122 -0
  3. metadata +46 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 15ef03165aa91e4de72f2e056c3d96f3e3073800
4
+ data.tar.gz: 352e92bb9f86bf121eaddf5c64328c54849d73cb
5
+ SHA512:
6
+ metadata.gz: ff79613b892f11866722d8f6f0578927a1495c2e1667a74162a522ff4c0b91c2474e9106d43d4f983cd42ed07b8eeb3ce7a62438c0b548720c2e84445ce1240c
7
+ data.tar.gz: d08fd4134e6dc170532ff878ca816b2961a90ebb4dc322c8a8c884209d68e353a119b47cbf8b84213be1d6832e4cffa8d8705394bb94fa18918ffee679b55860
@@ -0,0 +1,122 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ module XHP
5
+ CLIENT_ID = '346.iMG9jv2hBk.apps.healthplanet.jp'
6
+ CLIENT_SECRET = '1460033240967-qQ94jRSpvPlnlwNEGTVQ61hDwvmAdSvL0JVoJ40W'
7
+
8
+ module HTTP
9
+ def post(url)
10
+ uri = URI.parse(url)
11
+ request = Net::HTTP::Post.new(uri.request_uri)
12
+
13
+ http = Net::HTTP.new(uri.host, uri.port)
14
+ http.use_ssl = true
15
+
16
+ http.set_debug_output $stderr if $xhp_debug
17
+
18
+ http.start do |h|
19
+ response = h.request(request)
20
+ end
21
+ end
22
+
23
+ def get(url)
24
+ uri = URI.parse(url)
25
+ request = Net::HTTP::Get.new(uri.request_uri)
26
+
27
+ http = Net::HTTP.new(uri.host, uri.port)
28
+ http.use_ssl = true
29
+
30
+ http.set_debug_output $stderr if $xhp_debug
31
+
32
+ http.start do |h|
33
+ response = h.request(request)
34
+ end
35
+ end
36
+ end
37
+
38
+ module HealthPlanet
39
+ module OAuth
40
+ include XHP::HTTP
41
+
42
+ class Scope
43
+ def initialize(innerscan = true, sphygmomanometer = true, pedometer = true, smug = true)
44
+ @innerscan, @sphygmomanometer, @pedometer, @smug = innerscan, sphygmomanometer, pedometer, smug
45
+ end
46
+
47
+ def to_s
48
+ a = []
49
+ a << 'innerscan' if @innerscan
50
+ a << 'sphygmomanometer' if @sphygmomanometer
51
+ a << 'pedometer' if @pedometer
52
+ a << 'smug' if @smug
53
+ a.join(',')
54
+ end
55
+ end
56
+
57
+ class RequestToken
58
+ attr_reader :access_token, :expires_in, :refresh_token
59
+
60
+ def initialize(access_token, expires_in, refresh_token)
61
+ @access_token, @expires_in, @refresh_token = access_token, expires_in, refresh_token
62
+ end
63
+ end
64
+
65
+ def get_auth_url(redirect_uri = 'https://www.healthplanet.jp/success.html',
66
+ scope = Scope.new,
67
+ response_type = 'code')
68
+ "https://www.healthplanet.jp/oauth/auth?client_id=#{CLIENT_ID}&redirect_uri=#{redirect_uri}&scope=#{scope.to_s}&response_type=#{response_type}"
69
+ end
70
+
71
+ def token(code,
72
+ redirect_uri = 'https://www.healthplanet.jp/success.html',
73
+ grant_type = 'authorization_code')
74
+ url = "https://www.healthplanet.jp/oauth/token.?client_id=#{CLIENT_ID}&client_secret=#{CLIENT_SECRET}&redirect_uri=#{redirect_uri}&code=#{code}&grant_type=#{grant_type}"
75
+ res = post(url)
76
+ raise "OAuth Token API return response code #{res.code}" unless res.code == '200'
77
+ json = JSON.parse(res.body)
78
+ RequestToken.new(json['access_token'], json['expires_in'], json['refresh_token'])
79
+ end
80
+ end
81
+
82
+ module Status
83
+ class Tag
84
+ def to_s
85
+ '6021,6022,6023,6024,6025,6026,6027,6028,6029'
86
+ end
87
+ end
88
+
89
+ def innerscan(token, tag = nil, date = 1, from = nil, to = nil)
90
+ url_part = ["https://www.healthplanet.jp/status/innerscan.json?access_token=#{token.access_token}&date=#{date}"]
91
+ url_part << "tag=#{tag.to_s}" if tag
92
+ url_part << "from=#{format_date(from)}" if from
93
+ url_part << "to=#{format_date(to)}" if to
94
+
95
+ url = url_part.join('&')
96
+ res = get(url)
97
+ raise "Status API return response code #{res.code}" unless res.code == '200'
98
+ json = JSON.parse(res.body)
99
+ json.extend(ResponseHolder)
100
+ json.http_response = res
101
+ json
102
+ end
103
+
104
+ def format_date(date)
105
+ if date.kind_of?(Date)
106
+ date.strftime("%Y%m%d%H%M%s")
107
+ elsif date.kind_of?(String)
108
+ date
109
+ end
110
+ end
111
+ end
112
+ end
113
+
114
+ module ResponseHolder
115
+ attr_accessor :http_response
116
+ end
117
+
118
+ class Client
119
+ include HealthPlanet::OAuth
120
+ include HealthPlanet::Status
121
+ end
122
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xhp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - xmisao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |
14
+ This library make it easy to access Health Planet API by your ruby script.
15
+ Health Planet API is health monitoring web api that is provided by TANITA Corporation.
16
+ email: mail@xmisao.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/xhp.rb
22
+ homepage: https://github.com/xmisao/xhp
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: A ruby interface for the Health Planet API
46
+ test_files: []