MShealth 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d19a145943a23c74d08ac17534027279d74cf0e
4
+ data.tar.gz: 92184e6a9223cf664c609120e3f4564d4ff749db
5
+ SHA512:
6
+ metadata.gz: 93e4568e8c4d16097ee593d6b4d338679129ece7773cf4a700f0c563cba52920519f41f95cb72a67a8cae8778a5cbb5aff4bcb6059640709a8980ad993714948
7
+ data.tar.gz: 52bb607990a149d4c1a9d989d670a820276a78e601b460bafec5438ea64a050e265e28683795f91d536f4cec9634de100ad4b26604f9ffe34737d41eb903a372
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in MShealth.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'MShealth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "MShealth"
8
+ spec.version = MShealth::VERSION
9
+ spec.authors = ["WH Lu"]
10
+ spec.email = ["luwh364@gmail.com"]
11
+
12
+ spec.summary = "Ruby API wrapper for the Microsoft Health Cloud"
13
+ spec.description = "Ruby API wrapper for the Microsoft Health Cloud"
14
+ spec.homepage = "https://github.com/yielder/mshealth"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "httparty"
24
+ spec.add_dependency "hashie"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.10"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest"
29
+ spec.add_development_dependency "vcr"
30
+ spec.add_development_dependency "webmock"
31
+ end
@@ -0,0 +1,98 @@
1
+ # MShealth
2
+
3
+ Ruby API wrapper for the [Microsoft Health Cloud](http://developer.microsoftband.com/cloudAPI).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'MShealth'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install MShealth
20
+
21
+ ## Usage
22
+ Check [get started](http://developer.microsoftband.com/Content/docs/MS%20Health%20API%20Getting%20Started.pdf) on how to register application and the application authentication scheme.
23
+
24
+ Set the token:
25
+
26
+ ```ruby
27
+ MShealth.configure do |c|
28
+ c.token = "your token"
29
+ end
30
+ ```
31
+
32
+ Then get client:
33
+
34
+ ```ruby
35
+ client = MShealth.client
36
+ ```
37
+
38
+ ### Profile
39
+
40
+ ```ruby
41
+ client.profile #Get the details about this user from their Microsoft Health profile.
42
+ ```
43
+
44
+ ### Devices
45
+
46
+ ```ruby
47
+ client.device(id:'FFFF1700-FFFF-FFFF-8529-454E11000210') #Get the single device info
48
+ client.devices #Return a list devices
49
+ ```
50
+
51
+ ### Activities
52
+
53
+ ```ruby
54
+ # Get the single activity. "details,minute_summaries,map_points" are optional.
55
+ activity = client.activity(id:"2519647921241112976",details:1,minute_summaries:1,map_points:1)
56
+ # Get all activities during the period in an array. "details,minute_summaries,map_points,activity_types" are optional.
57
+ activities = client.activities(start_time:Time.new(2015,5,27),
58
+ end_time:Time.new(2015,7,29),
59
+ activity_types:"Run,Sleep",
60
+ details:1,
61
+ minute_summaries:1,
62
+ map_points:1)
63
+ ```
64
+
65
+ ### Summary
66
+
67
+ ```ruby
68
+ # Get all summaries during the period in an array. "end_time,device_id" are optional.
69
+ summary = client.summary(period:'hourly',
70
+ start_time:Time.new(2015,6,27),
71
+ end_time:Time.new(2015,7,20),
72
+ device_id:"FFFF1700-FFFF-FFFF-8529-454E11000210")
73
+ ```
74
+
75
+ ### Return object
76
+
77
+ All returns are stored in a hash-like Mash object. All naming of parameters are ruby-ish.(eg:firstName -> first_name)
78
+
79
+ ```ruby
80
+ profile = client.profile
81
+ profile.first_name
82
+ # You can check the whole structure using to_yaml:
83
+ puts profile.to_yaml
84
+ ```
85
+
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yielder/MShealth.
90
+
91
+ ## Todo
92
+
93
+ - Add documentation
94
+ - Add oauth2 support
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "MShealth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ require "MShealth/version"
2
+ require "MShealth/client"
3
+ require "MShealth/configuration"
4
+ require "MShealth/mash"
5
+ require "MShealth/errors"
6
+
7
+
8
+ require 'time'
9
+
10
+ module MShealth
11
+ class << self
12
+ def client
13
+ @client ||= Client.new
14
+ end
15
+
16
+ def configure(&block)
17
+ client.configure(&block)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,169 @@
1
+ require 'httparty'
2
+
3
+ module MShealth
4
+
5
+ class Client
6
+
7
+
8
+ include HTTParty
9
+ base_uri 'https://api.microsofthealth.net'
10
+
11
+ def configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def configure
16
+ yield configuration
17
+ end
18
+
19
+ def profile
20
+ response = handle_response("/"+configuration.api_version+"/me/Profile",
21
+ :headers => {"Authorization" => "bearer "+configuration.token})
22
+ MShealth::Mash.new(response)
23
+ end
24
+
25
+ def devices
26
+ response = handle_response("/"+configuration.api_version+"/me/Devices",
27
+ :headers => {"Authorization" => "bearer "+configuration.token})
28
+ devices = []
29
+ response['deviceProfiles'].each do |device|
30
+ devices << MShealth::Mash.new(device)
31
+ end
32
+ devices
33
+ end
34
+
35
+ def device(id:)
36
+ response = handle_response("/"+configuration.api_version+"/me/Devices/"+id,
37
+ :headers => {"Authorization" => "bearer "+configuration.token})
38
+ MShealth::Mash.new(response)
39
+ end
40
+
41
+ def summary(period:,start_time:,**options)
42
+ query = {}
43
+ query['startTime'] = start_time.iso8601
44
+ query['endTime'] = options[:end_time].iso8601 if options.key?(:end_time)
45
+ query['deviceIds'] = options[:device_id] if options.key?(:device_id)
46
+
47
+ period_str = nil
48
+ response = nil
49
+
50
+ case period
51
+ when "hourly"
52
+ period_str = 'Hourly'
53
+ when "daily"
54
+ period_str = 'Daily'
55
+ else
56
+ end
57
+
58
+ response = handle_response("/"+configuration.api_version+"/me/Summaries/"+period_str,
59
+ :headers => {"Authorization" => "bearer "+configuration.token},
60
+ :query => query)
61
+
62
+ result = []
63
+
64
+ response['summaries'].each do |summary|
65
+ result << MShealth::Mash.new(summary)
66
+ end
67
+
68
+ while response.key?("nextPage")
69
+ response = handle_response(response['nextPage'],:headers => {"Authorization" => "bearer "+configuration.token})
70
+ response['summaries'].each do |summary|
71
+ result << MShealth::Mash.new(summary)
72
+ end
73
+ end
74
+
75
+ result
76
+ end
77
+
78
+ def activity(id:,**options)
79
+ query = {}
80
+ query = handle_activity_include(query,options)
81
+
82
+ response = handle_response("/"+configuration.api_version+"/me/Activities/"+id,
83
+ :headers => {"Authorization" => "bearer "+configuration.token},
84
+ :query => query)
85
+
86
+ MShealth::Mash.new(response)
87
+
88
+ end
89
+
90
+ def activities(start_time:,end_time:,**options)
91
+ query = {}
92
+
93
+ query['startTime'] = start_time.iso8601
94
+ query['endTime'] = end_time.iso8601
95
+ query['activityTypes'] = options[:activity_types] if options.key?(:activity_types)
96
+ query = handle_activity_include(query,options)
97
+
98
+ response = response = handle_response("/"+configuration.api_version+"/me/Activities",
99
+ :headers => {"Authorization" => "bearer "+configuration.token},
100
+ :query => query)
101
+
102
+ result = []
103
+
104
+ types = ['bike','freePlay','golf','guidedWorkout','run','sleep']
105
+
106
+ result = extract_activities(types,response,result)
107
+
108
+ while response.key?("nextPage")
109
+ response = handle_response(response['nextPage'],:headers => {"Authorization" => "bearer "+configuration.token})
110
+ result = extract_activities(types,response,result)
111
+ end
112
+
113
+ result
114
+ end
115
+
116
+ private
117
+ def handle_response(*args)
118
+ response = self.class.get(*args)
119
+
120
+ case response.code.to_s
121
+ when "401"
122
+ raise MShealth::Errors::UnauthorizedError.new, response
123
+ when "400"
124
+ raise MShealth::Errors::BadRequestError.new, response
125
+ when "403"
126
+ raise MShealth::Errors::ForbiddenError.new, response
127
+ when "404"
128
+ raise MShealth::Errors::NotFoundError.new, response
129
+ when "429"
130
+ raise MShealth::Errors::TooManyRequestsError.new, response
131
+ when "500"
132
+ raise MShealth::Errors::ServerError.new, response
133
+ when "200"
134
+ return response
135
+ else
136
+ puts response.code.to_s
137
+ end
138
+
139
+ end
140
+
141
+ def handle_activity_include(query,options)
142
+ query['activityIncludes'] = ""
143
+ query['activityIncludes'] += "Details," if options.key?(:details)
144
+ query['activityIncludes'] += "MinuteSummaries," if options.key?(:minute_summaries)
145
+ query['activityIncludes'] += "MapPoints," if options.key?(:map_points)
146
+ if query['activityIncludes'] == ""
147
+ query.delete('activityIncludes')
148
+ else
149
+ query['activityIncludes'] = query['activityIncludes'][0...-1]
150
+ end
151
+ query
152
+ end
153
+
154
+ def extract_activities(types,response,result)
155
+ types.each do |type|
156
+ name = type + 'Activities'
157
+ if !response[name].nil?
158
+ response[name].each do |activity|
159
+ result << MShealth::Mash.new(activity)
160
+ end
161
+ end
162
+ end
163
+ result
164
+ end
165
+
166
+
167
+ end
168
+
169
+ end
@@ -0,0 +1,13 @@
1
+ module MShealth
2
+ DEFAULT_VERSION = "V1"
3
+
4
+ class Configuration
5
+
6
+ attr_accessor :token, :api_version
7
+
8
+ def initialize
9
+ @api_version = DEFAULT_VERSION
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,18 @@
1
+ module MShealth
2
+ module Errors
3
+ class MShealthError < StandardError; end
4
+
5
+ class UnauthorizedError < MShealthError; end
6
+
7
+ class BadRequestError < MShealthError; end
8
+
9
+ class ForbiddenError < MShealthError; end
10
+
11
+ class NotFoundError < MShealthError; end
12
+
13
+ class TooManyRequestsError < MShealthError; end
14
+
15
+ class ServerError < MShealthError; end
16
+
17
+ end
18
+ end
@@ -0,0 +1,37 @@
1
+ require 'hashie'
2
+
3
+ module MShealth
4
+ class Mash < Hashie::Mash
5
+ def initialize(hash)
6
+ mash = super(hash)
7
+ convert_time(mash)
8
+ mash
9
+
10
+ end
11
+
12
+ private
13
+ def convert_time(mash)
14
+ dic = ['birthdate','last_successful_sync','day_id']
15
+ mash.each do |k,v|
16
+ if dic.include?(k.to_s) or k.to_s.include?("time")
17
+ mash[k] = Time.iso8601(v)
18
+ end
19
+ end
20
+ end
21
+
22
+ protected
23
+ def convert_key(key)
24
+ underscore(key)
25
+ end
26
+
27
+ def underscore(camel_cased_word)
28
+ word = camel_cased_word.to_s.dup
29
+ word.gsub!(/::/, '/')
30
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
31
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
32
+ word.tr!("-", "_")
33
+ word.downcase!
34
+ word
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module MShealth
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: MShealth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - WH Lu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Ruby API wrapper for the Microsoft Health Cloud
112
+ email:
113
+ - luwh364@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - MShealth.gemspec
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - lib/MShealth.rb
128
+ - lib/MShealth/client.rb
129
+ - lib/MShealth/configuration.rb
130
+ - lib/MShealth/errors.rb
131
+ - lib/MShealth/mash.rb
132
+ - lib/MShealth/version.rb
133
+ homepage: https://github.com/yielder/mshealth
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.4.6
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Ruby API wrapper for the Microsoft Health Cloud
157
+ test_files: []