activite 0.1.0

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: c0fc49292ff1cc617edec317ddb65991c408d8b1
4
+ data.tar.gz: 21d949774ed01cea6fc2eb219616926a5b2f4096
5
+ SHA512:
6
+ metadata.gz: 1e92cca90c65cb4c40ee0af4644aefa399d08395eb06675d8a7e9123fd1bd49b36f800c3c29d7056dfdc455856e85e519cff16358bd915661457c767912307cf
7
+ data.tar.gz: 3634d67561efa2d67346fd6b5cb52c3c1cd02e8922b97a2fb050e1def9dd500e96a1979ba903c0c5cb02db8f0b9ed7948a9b12324430823ac4843572e3217343
@@ -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,146 @@
1
+ # Withings API Ruby Gem
2
+
3
+ [![Build Status](http://img.shields.io/travis/paulosman/activite.svg)][travis]
4
+ [![Code Climate](https://codeclimate.com/github/paulosman/activite/badges/gpa.svg)][codeclimate]
5
+ [![Test Coverage](https://codeclimate.com/github/paulosman/activite/badges/coverage.svg)][coverage]
6
+ [![Dependency Status](https://gemnasium.com/paulosman/activite.svg)][gemnasium]
7
+
8
+ This gem provides access to data collected by [Withings](http://withings.com/) devices through
9
+ their [HTTP API](https://oauth.withings.com/api/doc).
10
+
11
+ NOTE: This gem is a work in progress. It is not yet available via RubyGems.
12
+
13
+ [travis]: https://travis-ci.org/paulosman/activite
14
+ [codeclimate]: https://codeclimate.com/github/paulosman/activite
15
+ [coverage]: https://codeclimate.com/github/paulosman/activite
16
+ [gemnasium]: https://gemnasium.com/paulosman/activite
17
+
18
+ ### TODO
19
+
20
+ * Notifications
21
+
22
+ ## Installation
23
+
24
+ Add this line to your application's Gemfile:
25
+
26
+ ```ruby
27
+ gem 'activite'
28
+ ```
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install activite
37
+
38
+ ## Usage
39
+
40
+ ### Authorization
41
+
42
+ Withings uses OAuth 1.0a for API authorization. If you're unfamiliar with OAuth, you can
43
+ [read about it here][bible] or you can [read the full spec][spec] if you're feeling brave.
44
+
45
+ Before you can make requests to the Withings API, you must first obtain user authorization and
46
+ generate an Access Token. Before you can write an application that uses the Withings API you
47
+ must [register and obtain consumer credentials][register].
48
+
49
+ The following examples assume you are using a web framework such as [Sinatra][sinatra].
50
+
51
+ [register]: https://oauth.withings.com/partner/add "Withings Application Registration"
52
+ [bible]: http://oauthbible.com/ "OAuth Bible"
53
+ [spec]: http://oauth.net/core/1.0a/ "OAuth 1.0a Core Spec"
54
+ [sinatra]: http://www.sinatrarb.com/ "Sinatra"
55
+
56
+ ```ruby
57
+ client = Activite::Client.new({
58
+ consumer_key: 'YOUR_CONSUMER_KEY',
59
+ consumer_secret: 'YOUR_CONSUMER_SECRET'
60
+ })
61
+
62
+ request_token = client.request_token({
63
+ oauth_callback: 'YOUR_OAUTH_CALLBACK'
64
+ })
65
+
66
+ authorize_url = client.authorize_url(request_token.token, request_token.secret)
67
+ redirect authorize_url
68
+ ```
69
+
70
+ When the user has finished authorizing your application, they will be redirected
71
+ to the callback URL you specified with the following parameters in the query string:
72
+ ```userid```, ```oauth_token``` and ```oauth_verifier```. Store these parameters as
73
+ you'll need them later.
74
+
75
+ ```ruby
76
+ client = Activite::Client.new({
77
+ consumer_key: 'YOUR_CONSUMER_KEY',
78
+ consumer_secret: 'YOUR_CONSUMER_SECRET'
79
+ })
80
+
81
+ client.access_token(request_token.token, request_token.secret, {
82
+ oauth_verifier: 'OAUTH_VERIFIER'
83
+ })
84
+ ```
85
+
86
+ ## Making Requests
87
+
88
+ Now that you have an authorized access token, you can create a ```Activite::Client``` instance:
89
+
90
+ ```ruby
91
+ client = Activite::Client.new do |config|
92
+ config.consumer_key = 'YOUR_CONSUMER_KEY'
93
+ config.consumer_secret = 'YOUR_CONSUMER_SECRET'
94
+ config.token = 'YOUR_ACCESS_TOKEN'
95
+ config.secret = 'YOUR_ACCESS_TOKEN_SECRET'
96
+ end
97
+ ```
98
+
99
+ Now you can make authenticated requests on behalf of a user. For example, to get a list of
100
+ activity measures, you can use the following example:
101
+
102
+ ```ruby
103
+ activities = client.activities(user_id, { startdateymd: '2015-01-01', enddateymd: '2015-02-28' })
104
+ activities.each do |activity|
105
+ if activity.is_a?(Activite::Activity)
106
+ puts "Date: #{activity.date}, Steps: #{activity.steps}"
107
+ end
108
+ end
109
+ ```
110
+ ## Dates
111
+
112
+ Many of the Withings API endpoints accept a date parameter in either YYYY-MM-DD format or as a
113
+ unix epoch. This gem tries to simplify date handling by allowing you to specify either, or a
114
+ ```Date``` or ```DateTime``` instance. For example:
115
+
116
+ ```ruby
117
+ client = Activite::Client.new { options }
118
+ sleep_details = client.sleep_series(user_id, {
119
+ startdate: DateTime.new(2015, 2, 20, 12, 0, 0),
120
+ enddate: DateTime.new(2015, 2, 21, 12, 20, 0)
121
+ })
122
+ ```
123
+
124
+ Or equivalently,
125
+
126
+ ```ruby
127
+ client = Activite::Client.new { options }
128
+ sleep_details = client.sleep_series(user_id, {
129
+ startdate: '2015-02-20',
130
+ enddate: '2015-02-21'
131
+ })
132
+ ```
133
+
134
+ ## Development
135
+
136
+ 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.
137
+
138
+ 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).
139
+
140
+ ## Contributing
141
+
142
+ 1. Fork it ( https://github.com/paulosman/activite/fork )
143
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
144
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
145
+ 4. Push to the branch (`git push origin my-new-feature`)
146
+ 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,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activite/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activite"
8
+ spec.version = Activite::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"
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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activite"
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 'activite/version'
2
+ require 'activite/http/oauth'
3
+ require 'activite/utils'
4
+ require 'activite/client'
5
+
6
+ module Activite
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'activite/base'
2
+
3
+ module Activite
4
+ class Activity < Base
5
+ end
6
+ end
@@ -0,0 +1,18 @@
1
+ module Activite
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 [Activite::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,151 @@
1
+ require 'activite/error'
2
+ require 'activite/http/request'
3
+ require 'activite/activity'
4
+ require 'activite/measurement_group'
5
+ require 'activite/sleep_series'
6
+ require 'activite/sleep_summary'
7
+
8
+ module Activite
9
+ class Client
10
+ include Activite::HTTP::OAuthClient
11
+
12
+ attr_writer :user_agent
13
+
14
+ # Initializes a new Client object used to communicate with the Withings API.
15
+ #
16
+ # An authenticated Client can be created with an access token and access token
17
+ # secret if the user has previously authorized access to their Withings account
18
+ # and you've stored their access credentials. An unauthenticated Client can be
19
+ # created that will allow you to initiate the OAuth authorization flow, directing
20
+ # the user to Withings to authorize access to their account.
21
+ #
22
+ # @param options [Hash]
23
+ # @option options [String] :consumer_key The consumer key (required)
24
+ # @option options [String] :consumer_secret The consumer secret (required)
25
+ # @option options [String] :token The access token (if you've stored it)
26
+ # @option options [String] :secret The access token secret (if you've stored it)
27
+ #
28
+ # @example User has not yet authorized access to their Withings account
29
+ # client = Activite::Client.new({ consumer_key: your_key, consumer_secret: your_secret })
30
+ #
31
+ # @example User has authorized access to their Withings account
32
+ # client = Activite::Client.new({
33
+ # consumer_key: your_key,
34
+ # consumer_secret: your_secret,
35
+ # token: your_access_token,
36
+ # secret: your_access_token_secret
37
+ # })
38
+ #
39
+ # @example You can also pass parameters as a block
40
+ # client = Activite::Client.new do |config|
41
+ # config.consumer_key = your_key
42
+ # config.consumer_secret = your_secret
43
+ # config.token = token
44
+ # config.secret = secret
45
+ # end
46
+ #
47
+ # @return [Activite::Client]
48
+ def initialize(options = {})
49
+ options.each do |key, value|
50
+ instance_variable_set("@#{key}", value)
51
+ end
52
+
53
+ yield(self) if block_given?
54
+
55
+ unless @token.nil? || @secret.nil?
56
+ @access_token = existing_access_token(@token, @secret)
57
+ end
58
+ end
59
+
60
+ # Return the User-Agent string
61
+ #
62
+ # @return [String]
63
+ def user_agent
64
+ @user_agent ||= "WithingsRubyGem/#{Activite::VERSION}"
65
+ end
66
+
67
+ # Get a list of activity measures for the specified user
68
+ #
69
+ # @param user_id [Integer]
70
+ # @param options [Hash]
71
+ #
72
+ # @return [Array<Activite::Activity>]
73
+ def activities(user_id, options = {})
74
+ perform_request(:get, '/v2/measure', Activite::Activity, 'activities', {
75
+ action: 'getactivity',
76
+ userid: user_id
77
+ }.merge(options))
78
+ end
79
+
80
+ # Get a list of body measurements taken by Withings devices
81
+ #
82
+ # @param user_id [Integer]
83
+ # @param options [Hash]
84
+ #
85
+ # @return [Array<Activite::MeasurementGroup>]
86
+ def body_measurements(user_id, options = {})
87
+ perform_request(:get, '/measure', Activite::MeasurementGroup, 'measuregrps', {
88
+ action: 'getmeas',
89
+ userid: user_id
90
+ }.merge(options))
91
+ end
92
+
93
+ # Get details about a user's sleep
94
+ #
95
+ # @param user_id [Integer]
96
+ # @param options [Hash]
97
+ #
98
+ # @return [Array<Activite::Sleep>]
99
+ def sleep_series(user_id, options = {})
100
+ perform_request(:get, '/v2/sleep', Activite::SleepSeries, 'series', {
101
+ action: 'get',
102
+ userid: user_id
103
+ }.merge(options))
104
+ end
105
+
106
+ # Get a summary of a user's night. Includes the total time they slept,
107
+ # how long it took them to fall asleep, how long it took them to fall
108
+ # asleep, etc.
109
+ #
110
+ # NOTE: user_id isn't actually used in this API call (so I assume it is
111
+ # derived from the OAuth credentials) but I was uncomfortable introducing
112
+ # this inconsitency into this gem.
113
+ #
114
+ # @param user_id [Intger]
115
+ # @param options [Hash]
116
+ #
117
+ # @return [Array<Activite::SleepSummary>]
118
+ def sleep_summary(user_id, options = {})
119
+ perform_request(:get, '/v2/sleep', Activite::SleepSummary, 'series', {
120
+ action: 'getsummary'
121
+ }.merge(options))
122
+ end
123
+
124
+ private
125
+
126
+ # Helper function that handles all API requests
127
+ #
128
+ # @param http_method [Symbol]
129
+ # @param path [String]
130
+ # @param klass [Class]
131
+ # @param key [String]
132
+ # @param options [Hash]
133
+ #
134
+ # @return [Array<Object>]
135
+ def perform_request(http_method, path, klass, key, options = {})
136
+ if @consumer_key.nil? || @consumer_secret.nil?
137
+ raise Activite::Error::ClientConfigurationError, "Missing consumer_key or consumer_secret"
138
+ end
139
+ options = Activite::Utils.normalize_date_params(options)
140
+ request = Activite::HTTP::Request.new(@access_token, { 'User-Agent' => user_agent })
141
+ response = request.send(http_method, path, options)
142
+ if response.has_key? key
143
+ response[key].collect do |element|
144
+ klass.new(element)
145
+ end
146
+ else
147
+ [klass.new(response)]
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,18 @@
1
+ module Activite
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,49 @@
1
+ require 'oauth'
2
+
3
+ module Activite
4
+ module HTTP
5
+ module OAuthClient
6
+ attr_accessor :consumer_key, :consumer_secret, :token, :secret
7
+
8
+ DEFAULT_OPTIONS = {
9
+ site: 'https://oauth.withings.com',
10
+ proxy: nil,
11
+ request_token_path: '/account/request_token',
12
+ authorize_path: '/account/authorize',
13
+ access_token_path: '/account/access_token',
14
+ scheme: :query_string
15
+ }
16
+
17
+ def request_token(options = {})
18
+ consumer.get_request_token(options)
19
+ end
20
+
21
+ def authorize_url(token, secret, options = {})
22
+ request_token = OAuth::RequestToken.new(consumer, token, secret)
23
+ request_token.authorize_url(options)
24
+ end
25
+
26
+ def access_token(token, secret, options = {})
27
+ request_token = OAuth::RequestToken.new(consumer, token, secret)
28
+ @access_token = request_token.get_access_token(options)
29
+ @token = @access_token.token
30
+ @secret = @access_token.secret
31
+ @access_token
32
+ end
33
+
34
+ def existing_access_token(token, secret)
35
+ OAuth::AccessToken.new(consumer, token, secret)
36
+ end
37
+
38
+ def connected?
39
+ !@access_token.nil?
40
+ end
41
+
42
+ private
43
+
44
+ def consumer
45
+ @consumer ||= OAuth::Consumer.new(@consumer_key, @consumer_secret, DEFAULT_OPTIONS)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,35 @@
1
+ require 'uri'
2
+ require 'json'
3
+ require 'net/http'
4
+
5
+ module Activite
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
+ uri = "#{BASE_URI}#{path}?#{hash_to_query(options)}"
17
+ response = @access_token.get(uri, @headers)
18
+ if response.code.to_i < 200 or response.code.to_i >= 400
19
+ raise Activite::Error::ClientConfigurationError, response.body
20
+ end
21
+ body = JSON.parse(response.body)
22
+ if body['status'].to_i != 0
23
+ raise Activite::Error::InvalidResponseError, "#{body['status']} - #{body['error']}"
24
+ end
25
+ body['body']
26
+ end
27
+
28
+ protected
29
+
30
+ def hash_to_query(hash)
31
+ return URI.encode(hash.map{|k,v| "#{k}=#{v}"}.join("&"))
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,31 @@
1
+ require 'activite/measures'
2
+
3
+ module Activite
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 => Activite::Measure::Weight,
10
+ 4 => Activite::Measure::Height,
11
+ 5 => Activite::Measure::FatFreeMass,
12
+ 6 => Activite::Measure::FatRatio,
13
+ 8 => Activite::Measure::FatMassWeight,
14
+ 11 => Activite::Measure::Pulse
15
+ }
16
+
17
+ # Create a new instance with a collection of measurements of the appropriate
18
+ # Activite::Measure type.
19
+ #
20
+ # @param attrs [Hash]
21
+ # @return [Activite::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 Activite
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 [Activite::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(Weight)
41
+ FatMassWeight = Class.new(Weight)
42
+ FatRatio = Class.new(self)
43
+ end
44
+ end
@@ -0,0 +1,6 @@
1
+ require 'activite/base'
2
+
3
+ module Activite
4
+ class SleepSeries < Base
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'activite/base'
2
+
3
+ module Activite
4
+ class SleepSummary < Base
5
+ end
6
+ end
@@ -0,0 +1,67 @@
1
+ require 'date'
2
+
3
+ module Activite
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
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ module Activite
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Osman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-01 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'
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'
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
+ - activite.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/activite.rb
88
+ - lib/activite/activity.rb
89
+ - lib/activite/base.rb
90
+ - lib/activite/client.rb
91
+ - lib/activite/error.rb
92
+ - lib/activite/http/oauth.rb
93
+ - lib/activite/http/request.rb
94
+ - lib/activite/measurement_group.rb
95
+ - lib/activite/measures.rb
96
+ - lib/activite/sleep_series.rb
97
+ - lib/activite/sleep_summary.rb
98
+ - lib/activite/utils.rb
99
+ - lib/activite/version.rb
100
+ homepage: https://github.com/paulosman/activite
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.2.2
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: A Ruby interface to the Withings API.
124
+ test_files: []