fitgem_oauth2 0.0.7 → 0.0.8

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: 455f84d71a7d18d7646a40c478fcd69a836bd244
4
- data.tar.gz: 886c0ad18bc91f724879036e9cd50ae1c85d774e
3
+ metadata.gz: 72e05988ee7b5c090becafe9dfd9345e1bee4aaa
4
+ data.tar.gz: 7ee833ab3f11ab43816a506bb722bc19a1d51945
5
5
  SHA512:
6
- metadata.gz: 1028d59b79f3d3cf574cb9c66ac0df50cc3085190daa9ec309e29da7b0ede492bd37ea3545d48185a1dd1fdeca98ee872e4c894ed260142f5b6c028880bde92b
7
- data.tar.gz: d36dbe9a999347f0c9e9bd12e34de67f5efd300bc100b0592c8f77d9ee42c09fe13e926667b416d99d9e2debe45860dc7f35ab23c298dbb8611457316fbcc7f8
6
+ metadata.gz: 749bf116d19bf60c1f06aa7d95b7e4ec3783efa00a0653b2f26e8afac548a90d25bb266b20bebf81b57e135abfdf59993e3449d1dcfe799f763fc293f23971a2
7
+ data.tar.gz: 8aaefdea1dc62867bf2b9e13a1a68ac37cce7ab5eab36349fe0e524c7557318e0cd017c8c7ffcd96d6d1b6eaa15a009cb40ebbb026fa1ac7e2d7f06bc26a50d9
@@ -6,7 +6,6 @@ require 'fitgem_oauth2/version'
6
6
  Gem::Specification.new do |s|
7
7
  s.name = 'fitgem_oauth2'
8
8
  s.version = FitgemOauth2::VERSION
9
- s.date = Date.today.to_s
10
9
  s.summary = "Fitbit API client library"
11
10
  s.description = "This gem allows requesting data from Fitbit API using OAuth2"
12
11
  s.authors = ["Ankit Gupta"]
@@ -16,6 +15,8 @@ Gem::Specification.new do |s|
16
15
  s.license = 'MIT'
17
16
 
18
17
  s.add_runtime_dependency 'faraday', '~> 0.9'
18
+
19
19
  s.add_development_dependency "rspec", '~> 3.4'
20
+ s.add_development_dependency "factory_girl", '~> 4.5'
20
21
  s.add_development_dependency "dotenv", '~> 2.1'
21
22
  end
@@ -7,6 +7,7 @@ require 'fitgem_oauth2/friends.rb'
7
7
  require 'fitgem_oauth2/heartrate.rb'
8
8
  require 'fitgem_oauth2/sleep.rb'
9
9
  require 'fitgem_oauth2/steps.rb'
10
+ require 'fitgem_oauth2/subscriptions.rb'
10
11
  require 'fitgem_oauth2/users.rb'
11
12
  require 'fitgem_oauth2/utils.rb'
12
13
  require 'fitgem_oauth2/version.rb'
@@ -17,57 +18,27 @@ require 'faraday'
17
18
  module FitgemOauth2
18
19
  class Client
19
20
 
20
- attr_reader :token
21
-
22
- attr_reader :user_id
21
+ DEFAULT_USER_ID = '-'
23
22
 
24
23
  attr_reader :client_id
25
-
26
24
  attr_reader :client_secret
25
+ attr_reader :token
26
+ attr_reader :user_id
27
27
 
28
28
  def initialize(opts)
29
- @client_id = opts[:client_id]
30
- if @client_id.nil?
31
- puts "TODO. Raise an exception due to missing client id"
29
+ missing = [:client_id, :client_secret, :token] - opts.keys
30
+ if missing.size > 0
31
+ raise FitgemOauth2::InvalidArgumentError, "Missing required options: #{missing.join(',')}"
32
32
  end
33
33
 
34
+ @client_id = opts[:client_id]
34
35
  @client_secret = opts[:client_secret]
35
- if @client_secret.nil?
36
- puts "TODO. Raise an exception due to missing client secret"
37
- end
38
-
39
36
  @token = opts[:token]
40
- if @token.nil?
41
- puts "TODO. Raise an exception due to missing token"
42
- end
43
-
44
- @user_id = opts[:user_id]
45
- if @user_id.nil?
46
- @user_id = "-"
47
- end
37
+ @user_id = (opts[:user_id] || DEFAULT_USER_ID)
48
38
 
49
39
  @connection = Faraday.new("https://api.fitbit.com")
50
40
  end
51
41
 
52
- def get_call(url)
53
- response = connection.get(url) do |request|
54
- request.headers['Authorization'] = "Bearer #{token}"
55
- request.headers['Content-Type'] = "application/x-www-form-urlencoded"
56
- end
57
-
58
- headers_to_keep = ["fitbit-rate-limit-limit","fitbit-rate-limit-remaining","fitbit-rate-limit-reset"]
59
-
60
- case response.status
61
- when 200; return JSON.parse(response.body).merge!(response.headers.slice(*headers_to_keep))
62
- when 400; raise FitgemOauth2::BadRequestError
63
- when 401; raise FitgemOauth2::UnauthorizedError
64
- when 403; raise FitgemOauth2::ForbiddenError
65
- when 404; raise FitgemOauth2::NotFoundError
66
- when 500..599; raise FitgemOauth2::ServerError
67
- end
68
- end
69
-
70
-
71
42
  def refresh_access_token(refresh_token)
72
43
  response = connection.post('/oauth2/token') do |request|
73
44
  encoded = Base64.strict_encode64("#{@client_id}:#{@client_secret}")
@@ -79,8 +50,41 @@ module FitgemOauth2
79
50
  return JSON.parse(response.body)
80
51
  end
81
52
 
53
+ def get_call(url)
54
+ response = connection.get(url) { |request| set_headers(request) }
55
+ return parse_response(response)
56
+ end
57
+
58
+ def post_call(url, params = {})
59
+ response = connection.post(url, params) { |request| set_headers(request) }
60
+ return parse_response(response)
61
+ end
62
+
63
+ def delete_call(url)
64
+ response = connection.delete(url) { |request| set_headers(request) }
65
+ return parse_response(response)
66
+ end
67
+
82
68
  private
83
69
  attr_reader :connection
84
70
 
71
+ def set_headers(request)
72
+ request.headers['Authorization'] = "Bearer #{token}"
73
+ request.headers['Content-Type'] = "application/x-www-form-urlencoded"
74
+ end
75
+
76
+ def parse_response(response)
77
+ headers_to_keep = ["fitbit-rate-limit-limit","fitbit-rate-limit-remaining","fitbit-rate-limit-reset"]
78
+
79
+ case response.status
80
+ when 200; return JSON.parse(response.body).merge!(response.headers.slice(*headers_to_keep))
81
+ when 400; raise FitgemOauth2::BadRequestError
82
+ when 401; raise FitgemOauth2::UnauthorizedError
83
+ when 403; raise FitgemOauth2::ForbiddenError
84
+ when 404; raise FitgemOauth2::NotFoundError
85
+ when 500..599; raise FitgemOauth2::ServerError
86
+ end
87
+ end
88
+
85
89
  end
86
90
  end
@@ -0,0 +1,32 @@
1
+ module FitgemOauth2
2
+ class Client
3
+
4
+ SUBSCRIBABLE_TYPES = [:sleep, :body, :activities, :foods, :all]
5
+
6
+ def subscriptions(opts)
7
+ get_call(subscription_url(opts))
8
+ end
9
+
10
+ def create_subscription(opts)
11
+ post_call(subscription_url(opts))
12
+ end
13
+
14
+ def remove_subscription(opts)
15
+ delete_call(subscription_url(opts))
16
+ end
17
+
18
+ protected
19
+
20
+ def subscription_url(opts)
21
+ type = opts[ :type ] || :all
22
+ subscription_id = opts[:subscription_id]
23
+
24
+ url = [ '1', 'user', user_id ]
25
+ url << type unless type == :all
26
+ url << 'apiSubscriptions'
27
+ url << subscription_id if subscription_id
28
+
29
+ return url.join('/') + '.json'
30
+ end
31
+ end
32
+ end
@@ -1,3 +1,3 @@
1
1
  module FitgemOauth2
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fitgem_oauth2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ankit Gupta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-17 00:00:00.000000000 Z
11
+ date: 2016-03-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_girl
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.5'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: dotenv
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -70,6 +84,7 @@ files:
70
84
  - lib/fitgem_oauth2/heartrate.rb
71
85
  - lib/fitgem_oauth2/sleep.rb
72
86
  - lib/fitgem_oauth2/steps.rb
87
+ - lib/fitgem_oauth2/subscriptions.rb
73
88
  - lib/fitgem_oauth2/users.rb
74
89
  - lib/fitgem_oauth2/utils.rb
75
90
  - lib/fitgem_oauth2/version.rb
@@ -98,3 +113,4 @@ signing_key:
98
113
  specification_version: 4
99
114
  summary: Fitbit API client library
100
115
  test_files: []
116
+ has_rdoc: