withings-sdk 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bffdaac5113e9b8548d0e9540fed0d105f4fac7f
4
+ data.tar.gz: 66b5665d6663a342e9b6de3edc8618500e3a8154
5
+ SHA512:
6
+ metadata.gz: dc2889228ece6b6c4a63c3a90510946558637a54cdce47300d3bd9c0c82bd2cf146e36c0ce0ad0665756890a73165856148e85f76cb80f86ed47211b1d8de52c
7
+ data.tar.gz: a34bd32aa020683d997ce7d4b19da2da4ff2a40cf853231a1dcd903941546262cc1907616973f10bd1eb52f683e1ef105e6bc23d4dd3f7eb4a417dca7f7f550c
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /.ruby-version
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.3
4
+ addons:
5
+ code_climate:
6
+ repo_token:
7
+ secure: "nNf8S+GUM7H0AXyv2dQ2GiDi4qhfLlP7JMXRSL4Bm6MUNPJ5mxRd5cNOdhQaqwcnZXSXkcqr6AEc79XBQBTYWB/o/VAq0yApKw1uvV1g9EQvpGcsSZf6OfcLExCNgZT4chJccfQ+Qd+ToJS6cUF+RY/1RXWfhmaj54DBH2E1lz0="
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :test do
4
+ gem 'codeclimate-test-reporter', require: nil
5
+ gem 'webmock', '~> 1.2'
6
+ end
7
+
8
+ # Specify your gem's dependencies in activite.gemspec
9
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Paul Osman
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,142 @@
1
+ # Withings API Ruby Gem
2
+
3
+ [![Build Status](http://img.shields.io/travis/paulosman/withings-sdk.svg)][travis]
4
+ [![Code Climate](https://codeclimate.com/github/paulosman/withings-sdk/badges/gpa.svg)][codeclimate]
5
+ [![Test Coverage](https://codeclimate.com/github/paulosman/withings-sdk/badges/coverage.svg)][coverage]
6
+ [![Dependency Status](https://gemnasium.com/paulosman/withings-sdk.svg)][gemnasium]
7
+ [![Gem Version](https://badge.fury.io/rb/withings-sdk.svg)][gemversion]
8
+
9
+ This gem provides access to data collected by [Withings](http://withings.com/) devices through
10
+ their [HTTP API](https://oauth.withings.com/api/doc).
11
+
12
+ [travis]: https://travis-ci.org/paulosman/withings-sdk
13
+ [codeclimate]: https://codeclimate.com/github/paulosman/withings-sdk
14
+ [coverage]: https://codeclimate.com/github/paulosman/withings-sdk
15
+ [gemnasium]: https://gemnasium.com/paulosman/withings-sdk
16
+ [gemversion]: https://badge.fury.io/rb/withings-sdk
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ ```ruby
23
+ gem 'withings-sdk'
24
+ ```
25
+
26
+ And then execute:
27
+
28
+ $ bundle
29
+
30
+ Or install it yourself as:
31
+
32
+ $ gem install withings-sdk
33
+
34
+ ## Usage
35
+
36
+ ### Authorization
37
+
38
+ Withings uses OAuth 1.0a for API authorization. If you're unfamiliar with OAuth, you can
39
+ [read about it here][bible] or you can [read the full spec][spec] if you're feeling brave.
40
+
41
+ Before you can make requests to the Withings API, you must first obtain user authorization and
42
+ generate an Access Token. Before you can write an application that uses the Withings API you
43
+ must [register and obtain consumer credentials][register].
44
+
45
+ The following examples assume you are using a web framework such as [Sinatra][sinatra].
46
+
47
+ [register]: https://oauth.withings.com/partner/add "Withings Application Registration"
48
+ [bible]: http://oauthbible.com/ "OAuth Bible"
49
+ [spec]: http://oauth.net/core/1.0a/ "OAuth 1.0a Core Spec"
50
+ [sinatra]: http://www.sinatrarb.com/ "Sinatra"
51
+
52
+ ```ruby
53
+ client = WithingsSDK::Client.new({
54
+ consumer_key: 'YOUR_CONSUMER_KEY',
55
+ consumer_secret: 'YOUR_CONSUMER_SECRET'
56
+ })
57
+
58
+ request_token = client.request_token({
59
+ oauth_callback: 'YOUR_OAUTH_CALLBACK'
60
+ })
61
+
62
+ authorize_url = client.authorize_url(request_token.token, request_token.secret)
63
+ redirect authorize_url
64
+ ```
65
+
66
+ When the user has finished authorizing your application, they will be redirected
67
+ to the callback URL you specified with the following parameters in the query string:
68
+ ```userid```, ```oauth_token``` and ```oauth_verifier```. Store these parameters as
69
+ you'll need them later.
70
+
71
+ ```ruby
72
+ client = WithingsSDK::Client.new({
73
+ consumer_key: 'YOUR_CONSUMER_KEY',
74
+ consumer_secret: 'YOUR_CONSUMER_SECRET'
75
+ })
76
+
77
+ client.access_token(request_token.token, request_token.secret, {
78
+ oauth_verifier: 'OAUTH_VERIFIER'
79
+ })
80
+ ```
81
+
82
+ ## Making Requests
83
+
84
+ Now that you have an authorized access token, you can create a ```WithingsSDK::Client``` instance:
85
+
86
+ ```ruby
87
+ client = WithingsSDK::Client.new do |config|
88
+ config.consumer_key = 'YOUR_CONSUMER_KEY'
89
+ config.consumer_secret = 'YOUR_CONSUMER_SECRET'
90
+ config.token = 'YOUR_ACCESS_TOKEN'
91
+ config.secret = 'YOUR_ACCESS_TOKEN_SECRET'
92
+ end
93
+ ```
94
+
95
+ Now you can make authenticated requests on behalf of a user. For example, to get a list of
96
+ activity measures, you can use the following example:
97
+
98
+ ```ruby
99
+ activities = client.activities(user_id, { startdateymd: '2015-01-01', enddateymd: '2015-02-28' })
100
+ activities.each do |activity|
101
+ if activity.is_a?(WithingsSDK::Activity)
102
+ puts "Date: #{activity.date}, Steps: #{activity.steps}"
103
+ end
104
+ end
105
+ ```
106
+ ## Dates
107
+
108
+ Many of the Withings API endpoints accept a date parameter in either YYYY-MM-DD format or as a
109
+ unix epoch. This gem tries to simplify date handling by allowing you to specify either, or a
110
+ ```Date``` or ```DateTime``` instance. For example:
111
+
112
+ ```ruby
113
+ client = WithingsSDK::Client.new { options }
114
+ sleep_details = client.sleep_series(user_id, {
115
+ startdate: DateTime.new(2015, 2, 20, 12, 0, 0),
116
+ enddate: DateTime.new(2015, 2, 21, 12, 20, 0)
117
+ })
118
+ ```
119
+
120
+ Or equivalently,
121
+
122
+ ```ruby
123
+ client = WithingsSDK::Client.new { options }
124
+ sleep_details = client.sleep_series(user_id, {
125
+ startdate: '2015-02-20',
126
+ enddate: '2015-02-21'
127
+ })
128
+ ```
129
+
130
+ ## Development
131
+
132
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
133
+
134
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
135
+
136
+ ## Contributing
137
+
138
+ 1. Fork it ( https://github.com/paulosman/withings-sdk/fork )
139
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
140
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
141
+ 4. Push to the branch (`git push origin my-new-feature`)
142
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ # Default directory to look in is `/specs`
5
+ # Run with `rake spec`
6
+ RSpec::Core::RakeTask.new(:spec) do |task|
7
+ task.rspec_opts = ['--color', '--format', 'documentation']
8
+ end
9
+
10
+ task :default => :spec
11
+
12
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "withings-sdk"
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,7 @@
1
+ require 'withings-sdk/version'
2
+ require 'withings-sdk/http/oauth'
3
+ require 'withings-sdk/utils'
4
+ require 'withings-sdk/client'
5
+
6
+ module WithingsSDK
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'withings-sdk/base'
2
+
3
+ module WithingsSDK
4
+ class Activity < Base
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ module WithingsSDK
2
+ class Base
3
+ # @return [Hash]
4
+ attr_reader :attrs
5
+
6
+ # Initializes a new object with attributes for the values passed to the constructor.
7
+ #
8
+ # @param attrs [Hash]
9
+ # @return [WithingsSDK::Base]
10
+ def initialize(attrs = {})
11
+ @attrs = attrs || {}
12
+ @attrs.each do |key, value|
13
+ self.class.class_eval { attr_reader key }
14
+ instance_variable_set("@#{key}", value)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,222 @@
1
+ require 'withings-sdk/error'
2
+ require 'withings-sdk/http/request'
3
+ require 'withings-sdk/activity'
4
+ require 'withings-sdk/measurement_group'
5
+ require 'withings-sdk/notification'
6
+ require 'withings-sdk/sleep_series'
7
+ require 'withings-sdk/sleep_summary'
8
+ require 'withings-sdk/response'
9
+
10
+ module WithingsSDK
11
+ class Client
12
+ include WithingsSDK::HTTP::OAuthClient
13
+
14
+ attr_writer :user_agent
15
+
16
+ # Initializes a new Client object used to communicate with the Withings API.
17
+ #
18
+ # An authenticated Client can be created with an access token and access token
19
+ # secret if the user has previously authorized access to their Withings account
20
+ # and you've stored their access credentials. An unauthenticated Client can be
21
+ # created that will allow you to initiate the OAuth authorization flow, directing
22
+ # the user to Withings to authorize access to their account.
23
+ #
24
+ # @param options [Hash]
25
+ # @option options [String] :consumer_key The consumer key (required)
26
+ # @option options [String] :consumer_secret The consumer secret (required)
27
+ # @option options [String] :token The access token (if you've stored it)
28
+ # @option options [String] :secret The access token secret (if you've stored it)
29
+ #
30
+ # @example User has not yet authorized access to their Withings account
31
+ # client = WithingsSDK::Client.new({ consumer_key: your_key, consumer_secret: your_secret })
32
+ #
33
+ # @example User has authorized access to their Withings account
34
+ # client = WithingsSDK::Client.new({
35
+ # consumer_key: your_key,
36
+ # consumer_secret: your_secret,
37
+ # token: your_access_token,
38
+ # secret: your_access_token_secret
39
+ # })
40
+ #
41
+ # @example You can also pass parameters as a block
42
+ # client = WithingsSDK::Client.new do |config|
43
+ # config.consumer_key = your_key
44
+ # config.consumer_secret = your_secret
45
+ # config.token = token
46
+ # config.secret = secret
47
+ # end
48
+ #
49
+ # @return [WithingsSDK::Client]
50
+ def initialize(options = {})
51
+ options.each do |key, value|
52
+ instance_variable_set("@#{key}", value)
53
+ end
54
+
55
+ yield(self) if block_given?
56
+
57
+ unless @token.nil? || @secret.nil?
58
+ @access_token = existing_access_token(@token, @secret)
59
+ end
60
+ end
61
+
62
+ # Return the User-Agent string
63
+ #
64
+ # @return [String]
65
+ def user_agent
66
+ @user_agent ||= "WithingsRubyGem/#{WithingsSDK::VERSION}"
67
+ end
68
+
69
+ # Get a list of activity measures for the specified user
70
+ #
71
+ # @param user_id [Integer]
72
+ # @param options [Hash]
73
+ #
74
+ # @return [Array<WithingsSDK::Activity>]
75
+ def activities(user_id, options = {})
76
+ perform_request(:get, '/v2/measure', WithingsSDK::Activity, 'activities', {
77
+ action: 'getactivity',
78
+ userid: user_id
79
+ }.merge(options))
80
+ end
81
+
82
+ # Get a list of body measurements taken by Withings devices
83
+ #
84
+ # @param user_id [Integer]
85
+ # @param options [Hash]
86
+ #
87
+ # @return [Array<WithingsSDK::MeasurementGroup>]
88
+ def body_measurements(user_id, options = {})
89
+ perform_request(:get, '/measure', WithingsSDK::MeasurementGroup, 'measuregrps', {
90
+ action: 'getmeas',
91
+ userid: user_id
92
+ }.merge(options))
93
+ end
94
+
95
+ # Return a list of weight body measurements
96
+ #
97
+ # @param user_id [Integer]
98
+ # @param options [Hash]
99
+ #
100
+ # @return [Array<WithingsSDK::Measure::Weight>]
101
+ def weight(user_id, options = {})
102
+ groups = body_measurements(user_id, options)
103
+ weights = []
104
+ groups.each do |group|
105
+ group.measures.each do |measure|
106
+ next if !measure.is_a? WithingsSDK::Measure::Weight
107
+ weights << WithingsSDK::Measure::Weight.new(measure.attrs.merge('weighed_at' => group.date))
108
+ end
109
+ end
110
+ weights
111
+ end
112
+
113
+ # Get details about a user's sleep
114
+ #
115
+ # @param user_id [Integer]
116
+ # @param options [Hash]
117
+ #
118
+ # @return [Array<WithingsSDK::Sleep>]
119
+ def sleep_series(user_id, options = {})
120
+ perform_request(:get, '/v2/sleep', WithingsSDK::SleepSeries, 'series', {
121
+ action: 'get',
122
+ userid: user_id
123
+ }.merge(options))
124
+ end
125
+
126
+ # Get a summary of a user's night. Includes the total time they slept,
127
+ # how long it took them to fall asleep, how long it took them to fall
128
+ # asleep, etc.
129
+ #
130
+ # NOTE: user_id isn't actually used in this API call (so I assume it is
131
+ # derived from the OAuth credentials) but I was uncomfortable introducing
132
+ # this inconsistency into this gem.
133
+ #
134
+ # @param user_id [Intger]
135
+ # @param options [Hash]
136
+ #
137
+ # @return [Array<WithingsSDK::SleepSummary>]
138
+ def sleep_summary(user_id, options = {})
139
+ perform_request(:get, '/v2/sleep', WithingsSDK::SleepSummary, 'series', {
140
+ action: 'getsummary'
141
+ }.merge(options))
142
+ end
143
+
144
+ # Register a webhook / notification with the Withings API. This allows
145
+ # you to be notified when new data is available for a user.
146
+ #
147
+ # @param user_id [Integer]
148
+ # @param options [Hash]
149
+ #
150
+ # @return [WithingsSDK::Response]
151
+ def create_notification(user_id, options = {})
152
+ perform_request(:post, '/notify', WithingsSDK::Response, nil, {
153
+ action: 'subscribe'
154
+ }.merge(options))
155
+ end
156
+
157
+ # Get information about a specific webhook / notification.
158
+ #
159
+ # @param user_id [Integer]
160
+ # @param options [Hash]
161
+ #
162
+ # @return [WithingsSDK::Notification]
163
+ def get_notification(user_id, options = {})
164
+ perform_request(:get, '/notify', WithingsSDK::Notification, nil, {
165
+ action: 'get'
166
+ }.merge(options))
167
+ end
168
+
169
+ # Return a list of registered webhooks / notifications.
170
+ #
171
+ # @param user_id [Integer]
172
+ # @param options [Hash]
173
+ #
174
+ # @return [Array<WithingsSDK::Notification>]
175
+ def list_notifications(user_id, options = {})
176
+ perform_request(:get, '/notify', WithingsSDK::Notification, 'profiles', {
177
+ action: 'list'
178
+ }.merge(options))
179
+ end
180
+
181
+ # Revoke previously subscribed webhook / notification.
182
+ #
183
+ # @param user_id [Integer]
184
+ # @param options [Hash]
185
+ #
186
+ # @return [WithingsSDK::Response]
187
+ def revoke_notification(user_id, options = {})
188
+ perform_request(:get, '/notify', WithingsSDK::Response, nil, {
189
+ action: 'revoke'
190
+ }.merge(options))
191
+ end
192
+
193
+ private
194
+
195
+ # Helper function that handles all API requests
196
+ #
197
+ # @param http_method [Symbol]
198
+ # @param path [String]
199
+ # @param klass [Class]
200
+ # @param key [String]
201
+ # @param options [Hash]
202
+ #
203
+ # @return [Array<Object>]
204
+ def perform_request(http_method, path, klass, key, options = {})
205
+ if @consumer_key.nil? || @consumer_secret.nil?
206
+ raise WithingsSDK::Error::ClientConfigurationError, "Missing consumer_key or consumer_secret"
207
+ end
208
+ options = WithingsSDK::Utils.normalize_date_params(options)
209
+ request = WithingsSDK::HTTP::Request.new(@access_token, { 'User-Agent' => user_agent })
210
+ response = request.send(http_method, path, options)
211
+ if key.nil?
212
+ klass.new(response)
213
+ elsif response.has_key? key
214
+ response[key].collect do |element|
215
+ klass.new(element)
216
+ end
217
+ else
218
+ [klass.new(response)]
219
+ end
220
+ end
221
+ end
222
+ end
@@ -0,0 +1,18 @@
1
+ module WithingsSDK
2
+ # Custom errors for rescuing from Withings API errors
3
+ class Error < StandardError
4
+ # @return [Integer]
5
+ attr_reader :code
6
+
7
+ # Raised when client is misconfigured
8
+ ClientConfigurationError = Class.new(self)
9
+
10
+ # Withings returns 200 for everything, making it difficult to figure out
11
+ # exactly what went wrong. They also appear to send back fairly arbitrary
12
+ # codes, for example a response with an HTTP Status Code 200 can contain
13
+ # a body {"status":503,"error":"Invalid Params"} if OAuth credentials are
14
+ # incorrect (503 normally indicates that a downstream service is unavailable).
15
+ # Because of this we just wrap most errors in this class.
16
+ InvalidResponseError = Class.new(self)
17
+ end
18
+ end
@@ -0,0 +1,50 @@
1
+ require 'oauth'
2
+
3
+ module WithingsSDK
4
+ module HTTP
5
+ module OAuthClient
6
+ attr_accessor :consumer_key, :consumer_secret, :token, :secret
7
+ attr_writer :consumer
8
+
9
+ DEFAULT_OPTIONS = {
10
+ site: 'https://oauth.withings.com',
11
+ proxy: nil,
12
+ request_token_path: '/account/request_token',
13
+ authorize_path: '/account/authorize',
14
+ access_token_path: '/account/access_token',
15
+ scheme: :query_string
16
+ }
17
+
18
+ def request_token(options = {})
19
+ consumer.get_request_token(options)
20
+ end
21
+
22
+ def authorize_url(token, secret, options = {})
23
+ request_token = OAuth::RequestToken.new(consumer, token, secret)
24
+ request_token.authorize_url(options)
25
+ end
26
+
27
+ def access_token(token, secret, options = {})
28
+ request_token = OAuth::RequestToken.new(consumer, token, secret)
29
+ @access_token = request_token.get_access_token(options)
30
+ @token = @access_token.token
31
+ @secret = @access_token.secret
32
+ @access_token
33
+ end
34
+
35
+ def existing_access_token(token, secret)
36
+ OAuth::AccessToken.new(consumer, token, secret)
37
+ end
38
+
39
+ def connected?
40
+ !@access_token.nil?
41
+ end
42
+
43
+ private
44
+
45
+ def consumer
46
+ @consumer ||= OAuth::Consumer.new(@consumer_key, @consumer_secret, DEFAULT_OPTIONS)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,61 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ module WithingsSDK
6
+ module HTTP
7
+ BASE_URI = 'https://wbsapi.withings.net'
8
+
9
+ class Request
10
+ def initialize(access_token, headers)
11
+ @access_token = access_token
12
+ @headers = headers
13
+ end
14
+
15
+ def get(path, options = {})
16
+ request(:get, path, options)
17
+ end
18
+
19
+ def post(path, options = {})
20
+ request(:post, path, options)
21
+ end
22
+
23
+ protected
24
+
25
+ def hash_to_query(hash)
26
+ return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
27
+ end
28
+
29
+ def request_with_body(method, path, options = {})
30
+ body = hash_to_query(options)
31
+ uri = "#{BASE_URI}#{path}"
32
+ @access_token.send(method, uri, body, @headers)
33
+ end
34
+
35
+ def request_with_query_string(method, path, options = {})
36
+ uri = "#{BASE_URI}#{path}?#{hash_to_query(options)}"
37
+ @access_token.send(method, uri, @headers)
38
+ end
39
+
40
+ def request(method, path, options = {})
41
+ if [:post, :put].include? method
42
+ response = request_with_body(method, path, options)
43
+ else
44
+ response = request_with_query_string(method, path, options)
45
+ end
46
+
47
+ if response.code.to_i < 200 or response.code.to_i >= 400
48
+ raise WithingsSDK::Error::ClientConfigurationError, response.body
49
+ end
50
+
51
+ body = JSON.parse(response.body)
52
+ if body['status'].to_i != 0
53
+ raise WithingsSDK::Error::InvalidResponseError, "#{body['status']} - #{body['error']}"
54
+ end
55
+
56
+ body['body'] ||= body
57
+ body['body']
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,31 @@
1
+ require 'withings-sdk/measures'
2
+
3
+ module WithingsSDK
4
+ class MeasurementGroup < Base
5
+ # Types of body measurements collected by Withings devices and supported
6
+ # by this gem. See http://oauth.withings.com/api/doc#api-Measure-get_measure
7
+ # for details.
8
+ TYPES = {
9
+ 1 => WithingsSDK::Measure::Weight,
10
+ 4 => WithingsSDK::Measure::Height,
11
+ 5 => WithingsSDK::Measure::FatFreeMass,
12
+ 6 => WithingsSDK::Measure::FatRatio,
13
+ 8 => WithingsSDK::Measure::FatMassWeight,
14
+ 11 => WithingsSDK::Measure::Pulse
15
+ }
16
+
17
+ # Create a new instance with a collection of measurements of the appropriate
18
+ # WithingsSDK::Measure type.
19
+ #
20
+ # @param attrs [Hash]
21
+ # @return [WithingsSDK::MeasurementGroup]
22
+ def initialize(attrs = {})
23
+ super(attrs)
24
+ return if attrs['measures'].nil?
25
+ @measures = attrs['measures'].collect do |measurement|
26
+ klass = TYPES[measurement['type']]
27
+ klass.new(measurement) unless klass.nil?
28
+ end.reject { |obj| obj.nil? }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,44 @@
1
+ module WithingsSDK
2
+ class Measure < Base
3
+
4
+ # Create a new instance.
5
+ #
6
+ # The Withings API returns all values as integers with a unit which represents
7
+ # the power of 10 the value should be multiplied by to get the real value. For
8
+ # example, value=20 and unit=-1 should be 2.0.
9
+ #
10
+ # @param attrs [Hash]
11
+ # @return [WithingsSDK::Measure]
12
+ def initialize(attrs = {})
13
+ super(attrs)
14
+ @value = value / (10 ** unit.abs).to_f
15
+ end
16
+
17
+ #
18
+ # Different measurement types
19
+ #
20
+
21
+ Weight = Class.new(self) do |cls|
22
+ # Return weight measurement in kilograms (default unit)
23
+ #
24
+ # @return [Float]
25
+ def in_kg
26
+ @value
27
+ end
28
+
29
+ # Return weight measurement in pounds
30
+ #
31
+ # @return [Float]
32
+ def in_lb
33
+ (@value * 2.20462).round(3)
34
+ end
35
+ end
36
+
37
+ Height = Class.new(self)
38
+ Pulse = Class.new(self)
39
+
40
+ FatFreeMass = Class.new(self)
41
+ FatMassWeight = Class.new(self)
42
+ FatRatio = Class.new(self)
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ module WithingsSDK
2
+ class Notification < Base
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module WithingsSDK
2
+
3
+ # A Response is used to represent an empty API response with a status
4
+ # code and no additional information.
5
+ class Response
6
+ # @return [Integer]
7
+ attr_reader :status
8
+
9
+ def initialize(attrs = {})
10
+ @status = attrs['status'].to_i if attrs.has_key? 'status'
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ require 'withings-sdk/base'
2
+
3
+ module WithingsSDK
4
+ class SleepSeries < Base
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'withings-sdk/base'
2
+
3
+ module WithingsSDK
4
+ class SleepSummary < Base
5
+ end
6
+ end
@@ -0,0 +1,72 @@
1
+ require 'date'
2
+
3
+ module WithingsSDK
4
+ module Utils
5
+
6
+ def self.normalize_date_params(options)
7
+ opts = hash_with_string_date_keys(options)
8
+
9
+ convert_epoch_date_params!(opts)
10
+ convert_ymd_date_params!(opts)
11
+
12
+ if opts.has_key? 'startdateymd' and !opts.has_key? 'startdate'
13
+ opts['startdate'] = to_epoch(opts['startdateymd'])
14
+ end
15
+ if opts.has_key? 'enddateymd' and !opts.has_key? 'enddate'
16
+ opts['enddate'] = to_epoch(opts['enddateymd'])
17
+ end
18
+
19
+ opts['startdateymd'] = to_ymd(opts['startdate']) if opts.has_key? 'startdate'
20
+ opts['enddateymd'] = to_ymd(opts['enddate']) if opts.has_key? 'enddate'
21
+ opts
22
+ end
23
+
24
+ private
25
+
26
+ def self.hash_with_string_date_keys(params)
27
+ p = params.dup
28
+ date_fields = [:startdateymd, :enddateymd, :startdate, :enddate, :lastupdate]
29
+ date_fields.each { |key| p[key.to_s] = p.delete(key) if p.has_key? key }
30
+ p
31
+ end
32
+
33
+ def self.convert_ymd_date_params!(params)
34
+ ymd_fields = ['startdateymd', 'enddateymd']
35
+ ymd_fields.each do |key|
36
+ params[key] = to_ymd(params[key]) if params.has_key? key
37
+ end
38
+ end
39
+
40
+ def self.convert_epoch_date_params!(params)
41
+ epoch_fields = ['startdate', 'enddate', 'lastdate', 'date']
42
+ epoch_fields.each do |key|
43
+ params[key] = to_epoch(params[key]) if params.has_key? key
44
+ end
45
+ end
46
+
47
+ def self.to_epoch(d)
48
+ if d.is_a? Date or d.is_a? DateTime
49
+ d.strftime('%s').to_i
50
+ elsif d =~ /[0-9]{4}-[0-9]{2}-[0-9]{2}/
51
+ DateTime.strptime(d, '%Y-%m-%d').strftime('%s').to_i
52
+ else
53
+ d
54
+ end
55
+ end
56
+
57
+ def self.to_ymd(d)
58
+ if d.is_a? Date or d.is_a? DateTime
59
+ d.strftime('%Y-%m-%d')
60
+ elsif (d =~ /[0-9]+/) != 0
61
+ DateTime.strptime(d.to_s, '%s').strftime('%Y-%m-%d')
62
+ else
63
+ d
64
+ end
65
+ end
66
+
67
+ def self.to_ymdhms(d)
68
+ DateTime.strptime(d.to_s, '%s').strftime('%Y-%m-%d %H:%M:%S')
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module WithingsSDK
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'withings-sdk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "withings-sdk"
8
+ spec.version = WithingsSDK::VERSION
9
+ spec.authors = ["Paul Osman"]
10
+ spec.email = ["paul@eval.ca"]
11
+
12
+ spec.summary = 'A Ruby interface to the Withings API.'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/paulosman/activite'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "oauth", "~> 0.4.7"
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: withings-sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Osman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.4.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.4.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: A Ruby interface to the Withings API.
70
+ email:
71
+ - paul@eval.ca
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/withings-sdk.rb
87
+ - lib/withings-sdk/activity.rb
88
+ - lib/withings-sdk/base.rb
89
+ - lib/withings-sdk/client.rb
90
+ - lib/withings-sdk/error.rb
91
+ - lib/withings-sdk/http/oauth.rb
92
+ - lib/withings-sdk/http/request.rb
93
+ - lib/withings-sdk/measurement_group.rb
94
+ - lib/withings-sdk/measures.rb
95
+ - lib/withings-sdk/notification.rb
96
+ - lib/withings-sdk/response.rb
97
+ - lib/withings-sdk/sleep_series.rb
98
+ - lib/withings-sdk/sleep_summary.rb
99
+ - lib/withings-sdk/utils.rb
100
+ - lib/withings-sdk/version.rb
101
+ - withings-sdk.gemspec
102
+ homepage: https://github.com/paulosman/activite
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.4.5.1
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A Ruby interface to the Withings API.
126
+ test_files: []