fitgem_oauth2 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4668e05ec00b938a76c13ca40aa46fe6f685b113
4
- data.tar.gz: 90910ad066bb5eef0d6eb89b082a48da66c14164
3
+ metadata.gz: 54ad6d585fffb7134b719ed81b213171337662f0
4
+ data.tar.gz: 17e07a83a551e7a93ec04b96750f9f4724c88522
5
5
  SHA512:
6
- metadata.gz: 788da8e8b805f8fb654d0c08695c4159cda613256724b183af15388e43cb63c72fcd3d51db9a4e32d42c7f6c4e00f87ea5294b215b6065b01a06d16a03b940b3
7
- data.tar.gz: e331d68cb25eef55e7b8c3ab7f66e2eb0616948a48052f98b6d9a97447025e2e1fffa56dcf3dd53e46b298973af2f743580b8981e30a825cfdcae26ff1a334b2
6
+ metadata.gz: cae5964dedd83d56054f358b28fe7975bb6061a23ee62c25a2f4e31a140fbcc0089a935f0fa951237c7268c6b275ec7cdaefc488da3ced3acb667ffecdc92af8
7
+ data.tar.gz: 25b91d93e6a26b731224a30db09c70a32ce84be1b1e4be2fabeedd04933710a5e848ef71957cb5a0bf9f88d245c8f50caa417544cc52470545555444d8be1067
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'fitgem_oauth2'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = '2016-01-18'
5
5
  s.summary = "This gem allows requesting data from Fitbit API using OAuth2"
6
6
  s.description = "This gem allows requesting data from Fitbit API using OAuth2"
@@ -0,0 +1,9 @@
1
+ module FitgemOauth2
2
+ class Client
3
+
4
+ def activities_on_date(date)
5
+ get_call("1/user/#{user_id}/activities/date/#{format_date(date)}.json")
6
+ end
7
+
8
+ end
9
+ end
@@ -1,12 +1,22 @@
1
+ require 'fitgem_oauth2/activity.rb'
2
+ require 'fitgem_oauth2/sleep.rb'
3
+ require 'fitgem_oauth2/steps.rb'
4
+
5
+ require 'fitgem_oauth2/utils.rb'
6
+
1
7
  require 'base64'
2
8
  require 'faraday'
3
9
 
4
10
  module FitgemOauth2
5
11
  class Client
6
12
 
7
- attr_accessor :token
13
+ attr_reader :token
14
+
15
+ attr_reader :user_id
8
16
 
9
- attr_accessor :user_id
17
+ attr_reader :client_id
18
+
19
+ attr_reader :client_secret
10
20
 
11
21
  def initialize(opts)
12
22
  @client_id = opts[:client_id]
@@ -32,14 +42,25 @@ module FitgemOauth2
32
42
  @connection = Faraday.new("https://api.fitbit.com")
33
43
  end
34
44
 
35
-
36
- def activities_on_date(date)
37
- connection.get "1/user/#{user_id}/activities/date/#{format_date(date)}.json" do |request|
45
+ def get_call(url)
46
+ response = connection.get(url) do |request|
38
47
  request.headers['Authorization'] = "Bearer #{token}"
39
48
  request.headers['Content-Type'] = "application/x-www-form-urlencoded"
40
49
  end
50
+
51
+ headers_to_keep = ["fitbit-rate-limit-limit","fitbit-rate-limit-remaining","fitbit-rate-limit-reset"]
52
+
53
+ case response.status
54
+ when 200; return JSON.parse(response.body).merge!(response.headers.slice(*headers_to_keep))
55
+ when 400; raise FitgemOauth2::BadRequestError
56
+ when 401; raise FitgemOauth2::UnauthorizedError
57
+ when 403; raise FitgemOauth2::ForbiddenError
58
+ when 404; raise FitgemOauth2::NotFoundError
59
+ when 500..599; raise FitgemOauth2::ServerError
60
+ end
41
61
  end
42
62
 
63
+
43
64
  def refresh_access_token(refresh_token)
44
65
  response = connection.post('/oauth2/token') do |request|
45
66
  encoded = Base64.strict_encode64("#{@client_id}:#{@client_secret}")
@@ -52,19 +73,7 @@ module FitgemOauth2
52
73
  end
53
74
 
54
75
  private
55
-
56
- attr_accessor :connection
57
-
58
- def format_date(date)
59
- date
60
- end
61
-
62
- def get(url)
63
-
64
- end
65
-
66
- def raw_get(url)
67
- end
76
+ attr_reader :connection
68
77
 
69
78
  end
70
79
  end
@@ -0,0 +1,20 @@
1
+ module FitgemOauth2
2
+ class InvalidDateArgument < ArgumentError
3
+ end
4
+
5
+ # HTTP errors
6
+ class BadRequestError < StandardError
7
+ end
8
+
9
+ class UnauthorizedError < StandardError
10
+ end
11
+
12
+ class ForbiddenError < StandardError
13
+ end
14
+
15
+ class NotFoundError < StandardError
16
+ end
17
+
18
+ class ServerError < StandardError
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module FitgemOauth2
2
+
3
+ class Client
4
+
5
+ def sleep_on_date(date)
6
+ get_call("1/user/#{user_id}/sleep/date/#{format_date(date)}.json")
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,11 @@
1
+ module FitgemOauth2
2
+
3
+ class Client
4
+
5
+ def steps_on_date(date)
6
+ get_call("1/user/#{user_id}/activities/steps/date/#{format_date(date)}/1d.json")
7
+ end
8
+
9
+ end
10
+
11
+ end
@@ -0,0 +1,38 @@
1
+ module FitgemOauth2
2
+ class Client
3
+ # This function was copied from the Fitgem project (https://github.com/whazzmaster/fitgem/blob/master/lib/fitgem/helpers.rb)
4
+ # Format any of a variety of date types into the formatted string
5
+ # required when using the fitbit API.
6
+ #
7
+ # The date parameter can take several different kind of objects: a
8
+ # DateTime object, a Date object, a Time object or a String object. Furthermore,
9
+ # the string object may be either the date in a preformatted string
10
+ # ("yyyy-MM-dd"), or it may be the string values "today" or
11
+ # "tomorrow".
12
+ #
13
+ # @param [DateTime, Date, Time, String] date The object to format into a
14
+ # date string
15
+ # @raise [Fitgem::InvalidDateArgument] Raised when the object is
16
+ # not a DateTime, Date, Time or a valid (yyyy-MM-dd) String object
17
+ # @return [String] Date in "yyyy-MM-dd" string format
18
+ def format_date(date)
19
+ if date.is_a? String
20
+ case date
21
+ when 'today'
22
+ return Date.today.strftime("%Y-%m-%d")
23
+ when 'yesterday'
24
+ return (Date.today-1).strftime("%Y-%m-%d")
25
+ else
26
+ unless date =~ /\d{4}\-\d{2}\-\d{2}/
27
+ raise FitgemOauth2::InvalidDateArgument, "Invalid date (#{date}), must be in yyyy-MM-dd format"
28
+ end
29
+ return date
30
+ end
31
+ elsif Date === date || Time === date || DateTime === date
32
+ return date.strftime("%Y-%m-%d")
33
+ else
34
+ raise FitgemOauth2::InvalidDateArgument, "Date used must be a date/time object or a string in the format YYYY-MM-DD; supplied argument is a #{date.class}"
35
+ end
36
+ end
37
+ end
38
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fitgem_oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ankit Gupta
@@ -32,7 +32,12 @@ extra_rdoc_files: []
32
32
  files:
33
33
  - fitgem_oauth2.gemspec
34
34
  - lib/fitgem_oauth2.rb
35
+ - lib/fitgem_oauth2/activity.rb
35
36
  - lib/fitgem_oauth2/client.rb
37
+ - lib/fitgem_oauth2/errors.rb
38
+ - lib/fitgem_oauth2/sleep.rb
39
+ - lib/fitgem_oauth2/steps.rb
40
+ - lib/fitgem_oauth2/utils.rb
36
41
  homepage: http://rubygems.org/gems/fitgem_oauth2
37
42
  licenses:
38
43
  - MIT
@@ -58,3 +63,4 @@ signing_key:
58
63
  specification_version: 4
59
64
  summary: This gem allows requesting data from Fitbit API using OAuth2
60
65
  test_files: []
66
+ has_rdoc: