fitbit_client 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.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rubocop.yml +24 -0
- data/.rubocop_todo.yml +13 -0
- data/.travis.yml +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +62 -0
- data/Rakefile +12 -0
- data/bin/console +10 -0
- data/bin/setup +8 -0
- data/fitbit_client.gemspec +36 -0
- data/lib/fitbit_client.rb +42 -0
- data/lib/fitbit_client/authorization_grant_flow.rb +31 -0
- data/lib/fitbit_client/client.rb +42 -0
- data/lib/fitbit_client/error.rb +24 -0
- data/lib/fitbit_client/network/request.rb +87 -0
- data/lib/fitbit_client/resources.rb +15 -0
- data/lib/fitbit_client/resources/activity.rb +93 -0
- data/lib/fitbit_client/resources/body_and_weight.rb +145 -0
- data/lib/fitbit_client/resources/common.rb +20 -0
- data/lib/fitbit_client/resources/devices.rb +22 -0
- data/lib/fitbit_client/resources/sleep.rb +150 -0
- data/lib/fitbit_client/resources/subscription.rb +59 -0
- data/lib/fitbit_client/util.rb +33 -0
- data/lib/fitbit_client/version.rb +5 -0
- metadata +208 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d472e8616f43677a3a28ac7137c7d60a8280721
|
4
|
+
data.tar.gz: 382b88f98061745a339f780cf99d4deb7b33bd41
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 726befa5d90d4248eaedd89ca4136cc8b3a6a598d1471b04b36ae653546ddfbc44ef5a6b1beac7f8a5fd6393c17fc4a4ec3f55412a8a591a2692c7f0ad7796e0
|
7
|
+
data.tar.gz: 7df1adce571fbef32179594713fa3f2f5ad4819a43b82caf0519dac9a81540c1bc5c482cc4d10da74e298fa34483f94d576b71aeb4f6ce7c0bea6afb9aa253af
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
AllCops:
|
3
|
+
TargetRubyVersion: 2.3
|
4
|
+
|
5
|
+
Documentation:
|
6
|
+
Enabled: false
|
7
|
+
|
8
|
+
Metrics/MethodLength:
|
9
|
+
Max: 15
|
10
|
+
|
11
|
+
Style/PercentLiteralDelimiters:
|
12
|
+
Description: Use `%`-literal delimiters consistently
|
13
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
|
14
|
+
Enabled: true
|
15
|
+
PreferredDelimiters:
|
16
|
+
"%": "()"
|
17
|
+
"%i": "()"
|
18
|
+
"%q": "()"
|
19
|
+
"%Q": "()"
|
20
|
+
"%r": "{}"
|
21
|
+
"%s": "()"
|
22
|
+
"%w": "()"
|
23
|
+
"%W": "()"
|
24
|
+
"%x": "()"
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-06-29 16:54:15 -0500 using RuboCop version 0.49.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 21
|
10
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
11
|
+
# URISchemes: http, https
|
12
|
+
Metrics/LineLength:
|
13
|
+
Max: 165
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at michel.ocon@unosquare.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Michel Ocon
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# FitbitClient
|
2
|
+
|
3
|
+
[](https://travis-ci.org/sounxrt/fitbit_client) [](https://codeclimate.com/github/sounxrt/fitbit_client) [](https://codeclimate.com/github/sounxrt/fitbit_client/coverage)
|
4
|
+
|
5
|
+
Provides access to the Fitbit API using OAuth2
|
6
|
+
|
7
|
+
- The version of the API for most endpoints is the 1, and 1.2 for the ones were was available.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'fitbit_client'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install fitbit_client
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
To create an instance of the Client
|
28
|
+
```
|
29
|
+
client = FitbitClient::Client.new(access_token, refresh_token, { client_id: 'fitbit_app_client_id', client_secret: 'fitbit_app_client_secret' })
|
30
|
+
```
|
31
|
+
|
32
|
+
**Note:** `access_token` and `refresh_token` must come from a valid OAuth2 session.
|
33
|
+
|
34
|
+
### Rails
|
35
|
+
|
36
|
+
You can add an initializer to set the client_id and client_secret configuration values:
|
37
|
+
|
38
|
+
```
|
39
|
+
FitbitClient.configure do |config|
|
40
|
+
config.client_id = ENV['FITBIT_CLIENT_ID']
|
41
|
+
config.client_secret = ENV['FITBIT_CLIENT_SECRET']
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
## Development
|
47
|
+
|
48
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
49
|
+
|
50
|
+
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).
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/sounxrt/fitbit_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
59
|
+
|
60
|
+
## Code of Conduct
|
61
|
+
|
62
|
+
Everyone interacting in the FitbitClient project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/fitbit_client/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'fitbit_client'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
require 'pry'
|
10
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
5
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
6
|
+
require 'fitbit_client/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |spec|
|
9
|
+
spec.name = 'fitbit_client'
|
10
|
+
spec.version = FitbitClient::VERSION
|
11
|
+
spec.authors = ['Michel Ocon']
|
12
|
+
spec.email = ['soun.xrt@gmail.com']
|
13
|
+
spec.summary = 'Connects to Fibit API using OAuth2'
|
14
|
+
spec.description = 'Client library which connects to the Fibit API using OAuth2 protocol.'
|
15
|
+
spec.homepage = 'https://github.com/sounxrt/fitbit_client'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
spec.required_ruby_version = '~> 2.3'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'pry', '~> 0'
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
28
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0', '>= 1.0.0'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
31
|
+
spec.add_development_dependency 'simplecov', '~> 0.13.0'
|
32
|
+
spec.add_development_dependency 'vcr', '~> 3.0'
|
33
|
+
spec.add_development_dependency 'webmock', '~> 2.3'
|
34
|
+
|
35
|
+
spec.add_dependency('oauth2', '~> 1.4', '>= 1.4.0')
|
36
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'fitbit_client/version'
|
4
|
+
require 'fitbit_client/error'
|
5
|
+
require 'fitbit_client/util'
|
6
|
+
require 'fitbit_client/network/request'
|
7
|
+
|
8
|
+
# Resources
|
9
|
+
require 'fitbit_client/resources/common'
|
10
|
+
require 'fitbit_client/resources/activity'
|
11
|
+
require 'fitbit_client/resources/body_and_weight'
|
12
|
+
require 'fitbit_client/resources/devices'
|
13
|
+
require 'fitbit_client/resources/sleep'
|
14
|
+
require 'fitbit_client/resources/subscription'
|
15
|
+
require 'fitbit_client/resources'
|
16
|
+
|
17
|
+
# The main client
|
18
|
+
require 'fitbit_client/client'
|
19
|
+
require 'fitbit_client/authorization_grant_flow'
|
20
|
+
|
21
|
+
# The authorization grant flow
|
22
|
+
module FitbitClient
|
23
|
+
OAUTH2_CLIENT_OPTIONS = { site: 'https://api.fitbit.com',
|
24
|
+
token_url: 'https://api.fitbit.com/oauth2/token',
|
25
|
+
authorize_url: 'https://www.fitbit.com/oauth2/authorize',
|
26
|
+
auth_scheme: :basic_auth }.freeze
|
27
|
+
|
28
|
+
VALID_SCOPES = %w(activity heartrate location nutrition profile settings sleep social weight).freeze
|
29
|
+
VALID_SUBSCRIPTIONS = %i(activities body foods sleep).freeze
|
30
|
+
|
31
|
+
@default_locale = 'en_US'
|
32
|
+
@default_language = 'en_US'
|
33
|
+
|
34
|
+
class << self
|
35
|
+
attr_accessor :client_id, :client_secret
|
36
|
+
attr_accessor :default_language, :default_locale
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.configure
|
40
|
+
yield self
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FitbitClient
|
4
|
+
class AuthorizationGrantFlow
|
5
|
+
attr_reader :client_id, :client_secret
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
@client_id = options.fetch(:client_id, FitbitClient.client_id)
|
9
|
+
@client_secret = options.fetch(:client_secret, FitbitClient.client_secret)
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_token(code, redirect_url)
|
13
|
+
oauth2_client.get_token(grant_type: 'authorization_code', code: code, redirect_uri: redirect_url)
|
14
|
+
end
|
15
|
+
|
16
|
+
def oauth2_client
|
17
|
+
@oauth2_client ||= OAuth2::Client.new(client_id, client_secret, OAUTH2_CLIENT_OPTIONS)
|
18
|
+
end
|
19
|
+
|
20
|
+
def client_identification_uri(redirect_url, scopes)
|
21
|
+
params = URI.encode("?response_type=code&client_id=#{FitbitClient.client_id}&scope=#{scopes}")
|
22
|
+
"#{FitbitClient::OAUTH2_CLIENT_OPTIONS[:authorize_url]}/#{params}&redirect_uri=#{redirect_uri_param(redirect_url)}"
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def redirect_uri_param(redirect_url)
|
28
|
+
URI.encode_www_form_component(redirect_url).to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'oauth2'
|
4
|
+
|
5
|
+
module FitbitClient
|
6
|
+
class Client
|
7
|
+
include FitbitClient::Resources
|
8
|
+
|
9
|
+
attr_writer :refresh_token_callback
|
10
|
+
attr_reader :client_id, :client_secret, :access_token, :refresh_token
|
11
|
+
|
12
|
+
def initialize(access_token, refresh_token, options = {})
|
13
|
+
@access_token = access_token
|
14
|
+
@refresh_token = refresh_token
|
15
|
+
@client_id = options.fetch(:client_id, FitbitClient.client_id)
|
16
|
+
@client_secret = options.fetch(:client_secret, FitbitClient.client_secret)
|
17
|
+
raise ArgumentError, 'FitbitClient::Client cannot operate without a client_id and client_secret values' if empty_str?(@client_id) || empty_str?(@client_secret)
|
18
|
+
end
|
19
|
+
|
20
|
+
def debug_mode!(enable)
|
21
|
+
if enable
|
22
|
+
@original_oauth_debug = ENV['OAUTH_DEBUG']
|
23
|
+
ENV['OAUTH_DEBUG'] = 'true'
|
24
|
+
elsif @original_oauth_debug
|
25
|
+
ENV['OAUTH_DEBUG'] = @original_oauth_debug
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Force a refresh token
|
30
|
+
def refresh_token!
|
31
|
+
oauth2_refresh_token!
|
32
|
+
end
|
33
|
+
|
34
|
+
# When a new refresh token is received this method will be called with the
|
35
|
+
# OAuth2::AccessToken instance as argument
|
36
|
+
def refresh_token_callback!(oauth2_token)
|
37
|
+
@access_token = oauth2_token.token
|
38
|
+
@refresh_token = oauth2_token.refresh_token
|
39
|
+
@refresh_token_callback&.call(oauth2_token)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|