fitbit_client 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d472e8616f43677a3a28ac7137c7d60a8280721
4
- data.tar.gz: 382b88f98061745a339f780cf99d4deb7b33bd41
3
+ metadata.gz: 268141f31ad3bf289dec2404577a09de5606e1ae
4
+ data.tar.gz: 35f15997f295c7394537131aa99e30f378193b46
5
5
  SHA512:
6
- metadata.gz: 726befa5d90d4248eaedd89ca4136cc8b3a6a598d1471b04b36ae653546ddfbc44ef5a6b1beac7f8a5fd6393c17fc4a4ec3f55412a8a591a2692c7f0ad7796e0
7
- data.tar.gz: 7df1adce571fbef32179594713fa3f2f5ad4819a43b82caf0519dac9a81540c1bc5c482cc4d10da74e298fa34483f94d576b71aeb4f6ce7c0bea6afb9aa253af
6
+ metadata.gz: af6103913ffde626389c6106072583da0ff4e0c8a1134e1d09620d6075367cfa1d7415881d17b51ebf52a6e9e0049784b7eba99eb90f929db77b569a99b6aa90
7
+ data.tar.gz: 1abc601fc74062c263a419ab9adf43ee18f872a96601fe9233bb04e9473ecc24c5c9badf650a1a71ca00149c4d1900c463f4cb09372ee96fdac0beddd840349c
data/.rubocop.yml CHANGED
@@ -1,6 +1,8 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
  AllCops:
3
3
  TargetRubyVersion: 2.3
4
+ Exclude:
5
+ - 'test/**/*'
4
6
 
5
7
  Documentation:
6
8
  Enabled: false
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FitbitClient
2
2
 
3
- [![Build Status](https://travis-ci.org/sounxrt/fitbit_client.svg?branch=master)](https://travis-ci.org/sounxrt/fitbit_client) [![Code Climate](https://codeclimate.com/github/sounxrt/fitbit_client/badges/gpa.svg)](https://codeclimate.com/github/sounxrt/fitbit_client) [![Test Coverage](https://codeclimate.com/github/sounxrt/fitbit_client/badges/coverage.svg)](https://codeclimate.com/github/sounxrt/fitbit_client/coverage)
3
+ [![Build Status](https://travis-ci.org/sounxrt/fitbit_client.svg?branch=master)](https://travis-ci.org/sounxrt/fitbit_client) [![Code Climate](https://codeclimate.com/github/sounxrt/fitbit_client/badges/gpa.svg)](https://codeclimate.com/github/sounxrt/fitbit_client) [![Test Coverage](https://codeclimate.com/github/sounxrt/fitbit_client/badges/coverage.svg)](https://codeclimate.com/github/sounxrt/fitbit_client/coverage) [![Gem Version](https://badge.fury.io/rb/fitbit_client.svg)](https://badge.fury.io/rb/fitbit_client)
4
4
 
5
5
  Provides access to the Fitbit API using OAuth2
6
6
 
data/lib/fitbit_client.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'fitbit_client/version'
4
4
  require 'fitbit_client/error'
5
+ require 'fitbit_client/token_error'
5
6
  require 'fitbit_client/util'
6
7
  require 'fitbit_client/network/request'
7
8
 
@@ -34,6 +35,10 @@ module FitbitClient
34
35
  class << self
35
36
  attr_accessor :client_id, :client_secret
36
37
  attr_accessor :default_language, :default_locale
38
+
39
+ def debug_mode!(enable)
40
+ ENV['OAUTH_DEBUG'] = enable ? 'true' : 'false'
41
+ end
37
42
  end
38
43
 
39
44
  def self.configure
@@ -17,15 +17,6 @@ module FitbitClient
17
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
18
  end
19
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
20
  # Force a refresh token
30
21
  def refresh_token!
31
22
  oauth2_refresh_token!
@@ -44,10 +44,12 @@ module FitbitClient
44
44
  oauth2_access_token.request(method, path, opts)
45
45
  rescue OAuth2::Error => e # Handle refresh token issue automagically
46
46
  attempt += 1
47
- if expired_token_error?(e.response)
47
+ parsed_response = parse_response(e.response)
48
+ if expired_token_error?(parsed_response)
48
49
  oauth2_refresh_token!
49
50
  retry if attempt < 2
50
51
  end
52
+ check_unrecoverable_token(parsed_response, e)
51
53
  raise FitbitClient::Error.new('Error during OAuth2 request', e.response)
52
54
  end
53
55
  end
@@ -63,9 +65,22 @@ module FitbitClient
63
65
  JSON.parse response.body
64
66
  end
65
67
 
66
- def expired_token_error?(response)
67
- json_response = parse_response(response)
68
- json_response.dig('errors', 0, 'errorType') == 'expired_token'
68
+ def check_unrecoverable_token(parsed_response, error)
69
+ if invalid_token_error?(parsed_response, 'invalid_grant', 'Refresh token invalid') ||
70
+ invalid_token_error?(parsed_response, 'invalid_token', 'Access token invalid')
71
+ raise FitbitClient::TokenError.new('Invalid token', error.response)
72
+ end
73
+ end
74
+
75
+ def expired_token_error?(parsed_response)
76
+ parsed_response.dig('errors', 0, 'errorType') == 'expired_token'
77
+ end
78
+
79
+ def invalid_token_error?(parsed_response, error_type, error_message)
80
+ message = parsed_response.dig('errors', 0, 'message')
81
+ return unless message
82
+ parsed_response.dig('errors', 0, error_type) == 'invalid_token' &&
83
+ message.start_with?(error_message)
69
84
  end
70
85
 
71
86
  def oauth2_refresh_token!
@@ -3,9 +3,18 @@
3
3
  module FitbitClient
4
4
  module Resources
5
5
  module Common
6
+ def path_user_version(path, options = {})
7
+ version = options.fetch(:version, '1')
8
+ return "/#{version}#{path}.json" if options[:skip_user]
9
+
10
+ # Add user id
11
+ user_id = options.fetch(:user_id, '-')
12
+ "/#{version}/user/#{user_id}#{path}.json"
13
+ end
14
+
6
15
  def skip_user_options!(options)
7
16
  options[:skip_user] = true
8
- options.delete![:user_id] if options.key?(:user_id)
17
+ options.delete(:user_id) if options.key?(:user_id)
9
18
  end
10
19
 
11
20
  def time_series_path(type, resource, date, end_limit)
@@ -138,11 +138,12 @@ module FitbitClient
138
138
 
139
139
  private
140
140
 
141
+ # rubocop:disable Metrics/LineLength
141
142
  def sleep_default_version!(options)
142
143
  if options[:version] == '1'
143
144
  warn '[DEPRECATION] These endpoints are deprecated and support for them may end unexpectedly. If your application does not depend on the sleep as calculated by these endpoints, please use the new v1.2 sleep endpoints.'
144
145
  else
145
- options[:version] = '1.2'
146
+ options[:version] ||= '1.2'
146
147
  end
147
148
  end
148
149
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FitbitClient
4
+ # This class represent a unrecoverable token error
5
+ class TokenError < Error
6
+ end
7
+ end
@@ -6,15 +6,6 @@ module FitbitClient
6
6
  obj.nil? || obj.empty?
7
7
  end
8
8
 
9
- def path_user_version(path, options = {})
10
- version = options.fetch(:version, '1')
11
- return "/#{version}#{path}.json" if options[:skip_user]
12
-
13
- # Add user id
14
- user_id = options.fetch(:user_id, '-')
15
- "/#{version}/user/#{user_id}#{path}.json"
16
- end
17
-
18
9
  # Convert a Date object to a iso8601 format String (yyyy-MM-dd)
19
10
  def iso_date(date)
20
11
  date.iso8601
@@ -28,6 +19,6 @@ module FitbitClient
28
19
  time.strftime('%H:%M:%S')
29
20
  end
30
21
 
31
- module_function :empty_str?, :iso_date, :iso_time, :iso_time_with_seconds, :path_user_version
22
+ module_function :empty_str?, :iso_date, :iso_time, :iso_time_with_seconds
32
23
  end
33
24
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FitbitClient
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fitbit_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Ocon
@@ -179,6 +179,7 @@ files:
179
179
  - lib/fitbit_client/resources/devices.rb
180
180
  - lib/fitbit_client/resources/sleep.rb
181
181
  - lib/fitbit_client/resources/subscription.rb
182
+ - lib/fitbit_client/token_error.rb
182
183
  - lib/fitbit_client/util.rb
183
184
  - lib/fitbit_client/version.rb
184
185
  homepage: https://github.com/sounxrt/fitbit_client