fitbit_api 0.8.3 → 0.9.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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +20 -4
- data/fitbit_api.gemspec +1 -0
- data/lib/fitbit_api/client.rb +72 -24
- data/lib/fitbit_api/helpers/utils.rb +1 -1
- data/lib/fitbit_api/version.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6ad0b176146542cb288467849f95e477cd0345b3eb500a83ca13a47423d8ac2
|
4
|
+
data.tar.gz: dd8e47b54fc7210cc30ad347664c8e67ed555e1a64597e5cd012d896e31769d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c313975d4d22141af31553b129dfcbcf901847bc92191e2e5d559c633f245e5522ae72de893bf67fe0b6ebf3179ae0b3edabdf416da937a31d4025112f89e9c
|
7
|
+
data.tar.gz: 5d4c95b142fbe3ba5f7561c1c08e821a64c0dbd7fdafe3d5070b876363a0b2af1f5df1c1359bb54262ac5850ffc8104ca0c63e040e87dee9c45edbeade64d123
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
0.9.0
|
2
|
+
-----
|
3
|
+
- Rework client to accept existing access tokens.
|
4
|
+
- Fix argument parsing in `FitbitAPI::Client` to respect fallback values passed to `#configure`.
|
5
|
+
- Add `byebug` as development dependency.
|
6
|
+
|
1
7
|
0.8.3
|
2
8
|
-----
|
3
9
|
- Fix bug regarding optional `params` parsing for GET requests.
|
data/README.md
CHANGED
@@ -27,12 +27,15 @@ You can reference the [fitbit_api_rails](https://github.com/zokioki/fitbit_api_r
|
|
27
27
|
|
28
28
|
### Quickstart
|
29
29
|
|
30
|
-
If you already have
|
30
|
+
If you already have a user's token data and Fitbit user_id:
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
client = FitbitAPI::Client.new(client_id: 'XXXXXX',
|
34
34
|
client_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
35
|
-
|
35
|
+
access_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
36
|
+
refresh_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
37
|
+
expires_at: 1234567890,
|
38
|
+
user_id: 'XXXXXX')
|
36
39
|
```
|
37
40
|
|
38
41
|
### OAuth 2.0 Authorization Flow
|
@@ -62,14 +65,14 @@ You're now authorized and can make calls to Fitbit's API.
|
|
62
65
|
|
63
66
|
### Interacting with the API
|
64
67
|
|
65
|
-
Once a valid token has been generated, you're able to make API calls
|
68
|
+
Once a valid token has been generated, you're able to make API calls via the client object:
|
66
69
|
|
67
70
|
```ruby
|
68
71
|
client.food_logs Date.today
|
69
72
|
# => { "foods" => [{ "isFavorite" => true, "logDate" => "2015-06-26", "logId" => 1820, "loggedFood" => { "accessLevel" => "PUBLIC", "amount" => 132.57, "brand" => "", "calories" => 752, ...}] }
|
70
73
|
```
|
71
74
|
|
72
|
-
To make responses more easily suited for attribute-assignment, they can be parsed to return a hash whose keys are in snake_case format. This can be done by setting the client's `snake_case_keys` option to `true
|
75
|
+
To make responses more easily suited for attribute-assignment, they can be parsed to return a hash whose keys are in snake_case format. This can be done by setting the client's `snake_case_keys` option to `true`:
|
73
76
|
|
74
77
|
```ruby
|
75
78
|
client.snake_case_keys = true
|
@@ -104,6 +107,19 @@ When initializing a `FitbitAPI::Client` instance, you're given access to a handf
|
|
104
107
|
|
105
108
|
- `:symbolize_keys` - Transform returned object's keys to symbols (default: false)
|
106
109
|
|
110
|
+
If using this library in Rails, you can configure your options using an initializer:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
# config/initializers/fitbit_api.rb
|
114
|
+
|
115
|
+
FitbitAPI.configure do |config|
|
116
|
+
config.client_id = 'XXXX'
|
117
|
+
config.client_secret = 'xxxx'
|
118
|
+
config.snake_case_keys = true
|
119
|
+
config.symbolize_keys = true
|
120
|
+
end
|
121
|
+
```
|
122
|
+
|
107
123
|
## License
|
108
124
|
|
109
125
|
This gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/fitbit_api.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_runtime_dependency 'oauth2', '~> 1.0'
|
23
23
|
|
24
|
+
spec.add_development_dependency 'byebug'
|
24
25
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
25
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
26
27
|
spec.add_development_dependency 'rspec'
|
data/lib/fitbit_api/client.rb
CHANGED
@@ -13,22 +13,15 @@ require 'fitbit_api/water'
|
|
13
13
|
|
14
14
|
module FitbitAPI
|
15
15
|
class Client
|
16
|
-
attr_accessor :api_version, :unit_system, :locale, :scope,
|
16
|
+
attr_accessor :api_version, :unit_system, :locale, :scope,
|
17
|
+
:snake_case_keys, :symbolize_keys
|
17
18
|
attr_reader :user_id
|
18
19
|
|
19
|
-
def initialize(opts)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
unit_system locale scope api_version snake_case_keys symbolize_keys).each do |attr|
|
25
|
-
instance_variable_set("@#{attr}", (opts[attr.to_sym] || FitbitAPI.send(attr)))
|
26
|
-
end
|
27
|
-
|
28
|
-
@client = OAuth2::Client.new(@client_id, @client_secret, site: @site_url,
|
29
|
-
authorize_url: @authorize_url, token_url: @token_url)
|
30
|
-
|
31
|
-
restore_token(opts[:refresh_token]) if opts[:refresh_token]
|
20
|
+
def initialize(opts={})
|
21
|
+
validate_args(opts)
|
22
|
+
assign_attrs(opts)
|
23
|
+
set_client
|
24
|
+
establish_token(opts)
|
32
25
|
end
|
33
26
|
|
34
27
|
def auth_url
|
@@ -36,23 +29,23 @@ module FitbitAPI
|
|
36
29
|
end
|
37
30
|
|
38
31
|
def get_token(auth_code)
|
39
|
-
@token = @client.auth_code.get_token(
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
def restore_token(refresh_token)
|
45
|
-
@token = OAuth2::AccessToken.from_hash(@client, refresh_token: refresh_token).refresh!(headers: auth_header)
|
32
|
+
@token = @client.auth_code.get_token(
|
33
|
+
auth_code,
|
34
|
+
redirect_uri: @redirect_uri,
|
35
|
+
headers: auth_header
|
36
|
+
)
|
46
37
|
@user_id = @token.params['user_id']
|
47
|
-
|
38
|
+
@token
|
48
39
|
end
|
49
40
|
|
50
41
|
def token
|
51
|
-
@token.expired? ? refresh_token : @token
|
42
|
+
@token.expired? ? refresh_token! : @token
|
52
43
|
end
|
53
44
|
|
54
|
-
def refresh_token
|
45
|
+
def refresh_token!
|
55
46
|
@token = @token.refresh!(headers: auth_header)
|
47
|
+
@user_id ||= @token.params['user_id']
|
48
|
+
@token
|
56
49
|
end
|
57
50
|
|
58
51
|
def auth_header
|
@@ -91,5 +84,60 @@ module FitbitAPI
|
|
91
84
|
deep_symbolize_keys!(object) if (opts[:symbolize_keys] || symbolize_keys)
|
92
85
|
return object
|
93
86
|
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def validate_args(opts)
|
91
|
+
required_args = %i[client_id client_secret].freeze
|
92
|
+
missing_args = []
|
93
|
+
|
94
|
+
required_args.each do |arg|
|
95
|
+
missing_args << arg if (opts[arg] || FitbitAPI.send(arg)).nil?
|
96
|
+
end
|
97
|
+
|
98
|
+
return if missing_args.empty?
|
99
|
+
raise FitbitAPI::InvalidArgumentError,
|
100
|
+
"Required arguments: #{missing_args.join(', ')}"
|
101
|
+
end
|
102
|
+
|
103
|
+
def assign_attrs(opts)
|
104
|
+
attrs = %i[client_id client_secret redirect_uri site_url
|
105
|
+
authorize_url token_url unit_system locale scope
|
106
|
+
api_version snake_case_keys symbolize_keys].freeze
|
107
|
+
|
108
|
+
attrs.each do |attr|
|
109
|
+
instance_variable_set("@#{attr}", (opts[attr] || FitbitAPI.send(attr)))
|
110
|
+
end
|
111
|
+
|
112
|
+
@user_id = opts[:user_id]
|
113
|
+
end
|
114
|
+
|
115
|
+
def set_client
|
116
|
+
@client = OAuth2::Client.new(
|
117
|
+
@client_id,
|
118
|
+
@client_secret,
|
119
|
+
site: @site_url,
|
120
|
+
authorize_url: @authorize_url,
|
121
|
+
token_url: @token_url
|
122
|
+
)
|
123
|
+
end
|
124
|
+
|
125
|
+
def establish_token(opts)
|
126
|
+
return unless opts[:access_token] || opts[:refresh_token]
|
127
|
+
|
128
|
+
if opts[:access_token] && !opts[:user_id]
|
129
|
+
raise FitbitAPI::InvalidArgumentError,
|
130
|
+
'user_id is required if using existing access token'
|
131
|
+
end
|
132
|
+
|
133
|
+
@token = OAuth2::AccessToken.new(
|
134
|
+
@client,
|
135
|
+
opts[:access_token],
|
136
|
+
refresh_token: opts[:refresh_token],
|
137
|
+
expires_at: opts[:expires_at]
|
138
|
+
)
|
139
|
+
|
140
|
+
refresh_token! if @token.token.empty?
|
141
|
+
end
|
94
142
|
end
|
95
143
|
end
|
data/lib/fitbit_api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fitbit_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zoran
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|