fitbyte 0.1.0 → 0.2.0

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: 3868d5e912a5ea9c97364b68b5185a12af3a510a
4
- data.tar.gz: 10077bcf8477ba026a4af634a448502f734602a5
3
+ metadata.gz: 5d0655317d528786ceaf0f17b6c784cc8b5efeb1
4
+ data.tar.gz: 619f36cf862d95e9ddde9e8d1f77e6bd9cbfb7e3
5
5
  SHA512:
6
- metadata.gz: f5c5289e461ebf1349d26035ae81615754a9354b08b17a1f137fd90a38e8a29f82bf6d322f4d12a05335aedc09f6c63079fcfec01f3a77aed351118d57a49643
7
- data.tar.gz: dce52e6d3ad1dbecf34ee0376bd95945ad5c3aaa26455628e6bf4b37e59f8881a4b5a257af58388a0f732eb576da9fe9afa0434183768fef80b19f941d4bcf74
6
+ metadata.gz: 09a3f33748d49d7bfcc248fb72a776c80bc4051fb3b894230d67c0f07e01c202729aef6fef5b89fbaa265ca0f81574bcbeaa20312915654b4ed3a3352e35fc1d
7
+ data.tar.gz: 30275329df704750b1b8178bfedd01d022e949c8a382b29a2e21ae42ca7528f8a57c99f42513e8844b66f6219eeb9ecd85e3dd68c86bb83a80e58bba52d5da86
data/README.md CHANGED
@@ -1,51 +1,51 @@
1
1
  # Fitbyte
2
2
 
3
- Fitbyte is a gem that allows interaction with Fitbit's REST API, using the OAuth 2.0 protocol for user authorization.
3
+ Fitbyte is a gem that allows interaction with [Fitbit's REST API](https://dev.fitbit.com/docs/basics/), using the OAuth 2.0 protocol for user authorization.
4
4
 
5
- **Please Note:** Fitbit's API is currently in beta, and is in active/rapid development.
5
+ **Note:** Fitbit's API is currently in beta, and is in active/rapid development. Breaking changes to certain endpoints may be introduced during early development of this gem, until Fitbit's API solidifies.
6
6
 
7
7
  ## Installation
8
8
 
9
- Add the following line to your application's Gemfile:
9
+ To install the latest release:
10
+
11
+ $ gem install fitbyte
12
+
13
+ To include in a Rails project, add it to the Gemfile:
10
14
 
11
15
  ```ruby
12
16
  gem "fitbyte"
13
17
  ```
14
18
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install fitbyte
22
-
23
19
  ## Usage
24
20
 
25
- Create a client instance:
21
+ To use the Fitbit API, you must register your application at [dev.fitbit.com](https://dev.fitbit.com/apps). After registering, you should have access to **CLIENT ID**, **CLIENT SECRET**, and **REDIRECT URI** values for use in instantiating a *Fitbyte::Client* object.
22
+
23
+ - Create a client instance:
26
24
 
27
25
  ```ruby
28
- client = Fitbyte::Client.new(client_id: my_client_id, client_secret: my_client_secret, redirect_uri: "http://example.com")
26
+ client = Fitbyte::Client.new(client_id: my_client_id,
27
+ client_secret: my_client_secret,
28
+ redirect_uri: "http://example.com")
29
29
  ```
30
30
 
31
- Generate a link for the Fitbit authorization page:
31
+ - Generate a link for the Fitbit authorization page:
32
32
 
33
33
  ```ruby
34
34
  client.auth_page_link
35
35
  # => https://fitbit.com/oauth2/authorize?client_id=123XYZ&redirect_uri=...
36
36
  ```
37
37
 
38
- Follow generated link to Fitbit's authorization page. Once the given permissions prompt has been approved, you're sent to the `redirect_uri`, along with an authorization `code` query param. This authorization code can be exchanged for an access token:
38
+ - Follow generated link to Fitbit's authorization page. After approving your app, you're sent to the `redirect_uri`, with an appended authorization `code` param, which you'll exchange for an access token:
39
39
 
40
40
  ```ruby
41
41
  client.get_token(auth_code)
42
42
  ```
43
43
 
44
- You are now authenticated and can make calls to Fitbit's API:
44
+ You're now authenticated and can make calls to Fitbit's API:
45
45
 
46
46
  ```ruby
47
47
  client.food_logs
48
- # => returns JSON of today's food logs
48
+ # => returns JSON of current day's food logs
49
49
  ```
50
50
 
51
51
  ## License
data/fitbyte.gemspec CHANGED
@@ -6,7 +6,7 @@ require 'fitbyte/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "fitbyte"
8
8
  spec.version = Fitbyte::VERSION
9
- spec.authors = ["Zoran"]
9
+ spec.authors = ["Zoran Pesic"]
10
10
  spec.email = ["zoran1991@gmail.com"]
11
11
 
12
12
  spec.summary = %q{A gem that allows interaction with Fitbit's REST API, using OAuth2 for user authorization.}
@@ -1,31 +1,31 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def daily_activity_summary(date=Date.today)
4
- get("1/user/-/activities/date/#{format_date(date)}.json")
4
+ get("user/-/activities/date/#{format_date(date)}.json")
5
5
  end
6
6
 
7
7
  def frequent_activities
8
- get("1/user/-/activities/frequent.json")
8
+ get("user/-/activities/frequent.json")
9
9
  end
10
10
 
11
11
  def favorite_activities
12
- get("1/user/-/activities/favorite.json")
12
+ get("user/-/activities/favorite.json")
13
13
  end
14
14
 
15
15
  def all_activities
16
- get("1/activities.json")
16
+ get("activities.json")
17
17
  end
18
18
 
19
19
  def lifetime_stats
20
- get("1/user/-/activities.json")
20
+ get("user/-/activities.json")
21
21
  end
22
22
 
23
23
  def daily_goals
24
- get("1/user/-/activities/goals/daily.json")
24
+ get("user/-/activities/goals/daily.json")
25
25
  end
26
26
 
27
27
  def weekly_goals
28
- get("1/user/-/activities/goals/weekly.json")
28
+ get("user/-/activities/goals/weekly.json")
29
29
  end
30
30
  end
31
31
  end
@@ -1,7 +1,7 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def alarms(tracker_id)
4
- get("1/user/-/devices/tracker/#{tracker_id}/alarms.json")
4
+ get("user/-/devices/tracker/#{tracker_id}/alarms.json")
5
5
  end
6
6
  end
7
7
  end
data/lib/fitbyte/body.rb CHANGED
@@ -1,19 +1,19 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def weight_logs(date=Date.today)
4
- get("1/user/-/body/log/weight/date/#{format_date(date)}.json")
4
+ get("user/-/body/log/weight/date/#{format_date(date)}.json")
5
5
  end
6
6
 
7
7
  def body_fat_logs(date=Date.today)
8
- get("1/user/-/body/log/fat/date/#{format_date(date)}.json")
8
+ get("user/-/body/log/fat/date/#{format_date(date)}.json")
9
9
  end
10
10
 
11
11
  def weight_goals
12
- get("1/user/-/body/log/weight/goal.json")
12
+ get("user/-/body/log/weight/goal.json")
13
13
  end
14
14
 
15
15
  def body_fat_goals
16
- get("1/user/-/body/log/fat/goal.json")
16
+ get("user/-/body/log/fat/goal.json")
17
17
  end
18
18
  end
19
19
  end
@@ -11,22 +11,25 @@ require "fitbyte/water"
11
11
 
12
12
  module Fitbyte
13
13
  class Client
14
+ attr_accessor :api_version, :unit_system, :locale, :scope
15
+
14
16
  def initialize(options)
15
- missing_args = [:client_id, :client_secret] - options.keys
16
- if missing_args.size > 0
17
- raise ArgumentError, "Missing required options: #{missing.join(', ')}"
18
- end
17
+ missing_args = [:client_id, :client_secret, :redirect_uri] - options.keys
18
+
19
+ raise ArgumentError, "Required arguments: #{missing.join(', ')}" if missing_args.size > 0
20
+
19
21
  @client_id = options[:client_id]
20
22
  @client_secret = options[:client_secret]
21
23
 
22
24
  @redirect_uri = options[:redirect_uri]
23
- @site_url = options[:site_url] || "https://api.fitbit.com"
24
- @authorize_url = options[:authorize_url] || "https://www.fitbit.com/oauth2/authorize"
25
- @token_url = options[:token_url] || "https://api.fitbit.com/oauth2/token"
25
+ @site_url = options[:site_url] || defaults[:site_url]
26
+ @authorize_url = options[:authorize_url] || defaults[:authorize_url]
27
+ @token_url = options[:token_url] || defaults[:token_url]
26
28
 
27
- @scope = options[:scope].join(" ") || "activity nutrition profile settings sleep social weight"
28
- @unit_system = options[:unit_system] || "en_US"
29
- @locale = options[:locale] || "en_US"
29
+ @scope = format_scope(options[:scope]) || defaults[:scope]
30
+ @unit_system = options[:unit_system] || defaults[:unit_system]
31
+ @locale = options[:locale] || defaults[:locale]
32
+ @api_version = "1"
30
33
 
31
34
  @client = OAuth2::Client.new(@client_id, @client_secret, site: @site_url,
32
35
  authorize_url: @authorize_url, token_url: @token_url)
@@ -54,14 +57,25 @@ module Fitbyte
54
57
 
55
58
  def request_headers
56
59
  {
57
- "User-Agent" => "fitbyte-#{Fitbyte::VERSION} gem(#{Fitbyte::REPO_URL})",
60
+ "User-Agent" => "fitbyte-#{Fitbyte::VERSION} gem (#{Fitbyte::REPO_URL})",
58
61
  "Accept-Language" => @unit_system,
59
62
  "Accept-Locale" => @locale
60
63
  }
61
64
  end
62
65
 
66
+ def defaults
67
+ {
68
+ site_url: "https://api.fitbit.com",
69
+ authorize_url: "https://www.fitbit.com/oauth2/authorize",
70
+ token_url: "https://api.fitbit.com/oauth2/token",
71
+ scope: "activity nutrition profile settings sleep social weight",
72
+ unit_system: "en_US",
73
+ locale: "en_US"
74
+ }
75
+ end
76
+
63
77
  def get(path)
64
- JSON.parse(token.get(path, headers: request_headers).response.body)
78
+ JSON.parse(token.get(("#{@api_version}/" + path), headers: request_headers).response.body)
65
79
  end
66
80
  end
67
81
  end
@@ -1,7 +1,7 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def devices
4
- get("1/user/-/devices.json")
4
+ get("user/-/devices.json")
5
5
  end
6
6
  end
7
7
  end
data/lib/fitbyte/food.rb CHANGED
@@ -1,23 +1,23 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def food_logs(date=Date.today)
4
- get("1/user/-/foods/log/date/#{format_date(date)}.json")
4
+ get("user/-/foods/log/date/#{format_date(date)}.json")
5
5
  end
6
6
 
7
7
  def recent_foods
8
- get("1/user/-/foods/recent.json")
8
+ get("user/-/foods/recent.json")
9
9
  end
10
10
 
11
11
  def frequent_foods
12
- get("1/user/-/foods/log/frequent.json")
12
+ get("user/-/foods/log/frequent.json")
13
13
  end
14
14
 
15
15
  def favorite_foods
16
- get("1/user/-/foods/log/favorite.json")
16
+ get("user/-/foods/log/favorite.json")
17
17
  end
18
18
 
19
19
  def food_goals
20
- get("1/user/-/foods/log/goal.json")
20
+ get("user/-/foods/log/goal.json")
21
21
  end
22
22
  end
23
23
  end
@@ -1,11 +1,11 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def friends
4
- get("1/user/-/friends.json")
4
+ get("user/-/friends.json")
5
5
  end
6
6
 
7
7
  def friends_leaderboard
8
- get("1/user/-/friends/leaderboard.json")
8
+ get("user/-/friends/leaderboard.json")
9
9
  end
10
10
  end
11
11
  end
@@ -13,5 +13,9 @@ module Fitbyte
13
13
  raise ArgumentError, "Invalid type [#{date.class}] - provide a Date/Time/DateTime or a String(yyyy-MM-dd format)."
14
14
  end
15
15
  end
16
+
17
+ def format_scope(scope)
18
+ scope.is_a?(Array) ? scope.join(" ") : scope
19
+ end
16
20
  end
17
21
  end
data/lib/fitbyte/sleep.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def sleep_logs(date=Date.today)
4
- get("1/user/-/sleep/date/#{format_date(date)}.json")
4
+ get("user/-/sleep/date/#{format_date(date)}.json")
5
5
  end
6
6
  end
7
7
  end
data/lib/fitbyte/user.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def profile
4
- get("1/user/-/profile.json")
4
+ get("user/-/profile.json")
5
5
  end
6
6
 
7
7
  def badges
8
- get("1/user/-/badges.json")
8
+ get("user/-/badges.json")
9
9
  end
10
10
  end
11
11
  end
@@ -1,4 +1,4 @@
1
1
  module Fitbyte
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  REPO_URL = "https://github.com/zokioki/fitbyte"
4
4
  end
data/lib/fitbyte/water.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Fitbyte
2
2
  class Client
3
3
  def water_logs(date=Date.today)
4
- get("1/user/-/foods/log/water/date/#{format_date(date)}.json")
4
+ get("user/-/foods/log/water/date/#{format_date(date)}.json")
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fitbyte
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
- - Zoran
7
+ - Zoran Pesic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []