fitbyte 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3868d5e912a5ea9c97364b68b5185a12af3a510a
4
+ data.tar.gz: 10077bcf8477ba026a4af634a448502f734602a5
5
+ SHA512:
6
+ metadata.gz: f5c5289e461ebf1349d26035ae81615754a9354b08b17a1f137fd90a38e8a29f82bf6d322f4d12a05335aedc09f6c63079fcfec01f3a77aed351118d57a49643
7
+ data.tar.gz: dce52e6d3ad1dbecf34ee0376bd95945ad5c3aaa26455628e6bf4b37e59f8881a4b5a257af58388a0f732eb576da9fe9afa0434183768fef80b19f941d4bcf74
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fitbyte.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Zoran Pesic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # Fitbyte
2
+
3
+ Fitbyte is a gem that allows interaction with Fitbit's REST API, using the OAuth 2.0 protocol for user authorization.
4
+
5
+ **Please Note:** Fitbit's API is currently in beta, and is in active/rapid development.
6
+
7
+ ## Installation
8
+
9
+ Add the following line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem "fitbyte"
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fitbyte
22
+
23
+ ## Usage
24
+
25
+ Create a client instance:
26
+
27
+ ```ruby
28
+ client = Fitbyte::Client.new(client_id: my_client_id, client_secret: my_client_secret, redirect_uri: "http://example.com")
29
+ ```
30
+
31
+ Generate a link for the Fitbit authorization page:
32
+
33
+ ```ruby
34
+ client.auth_page_link
35
+ # => https://fitbit.com/oauth2/authorize?client_id=123XYZ&redirect_uri=...
36
+ ```
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:
39
+
40
+ ```ruby
41
+ client.get_token(auth_code)
42
+ ```
43
+
44
+ You are now authenticated and can make calls to Fitbit's API:
45
+
46
+ ```ruby
47
+ client.food_logs
48
+ # => returns JSON of today's food logs
49
+ ```
50
+
51
+ ## License
52
+
53
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fitbyte"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fitbyte/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fitbyte"
8
+ spec.version = Fitbyte::VERSION
9
+ spec.authors = ["Zoran"]
10
+ spec.email = ["zoran1991@gmail.com"]
11
+
12
+ spec.summary = %q{A gem that allows interaction with Fitbit's REST API, using OAuth2 for user authorization.}
13
+ spec.homepage = Fitbyte::REPO_URL
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "oauth2", "~> 1.0.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,7 @@
1
+ require "fitbyte/version"
2
+ require "oauth2"
3
+ require "json"
4
+ require "date"
5
+ require "fitbyte/client"
6
+
7
+ module Fitbyte; end
@@ -0,0 +1,31 @@
1
+ module Fitbyte
2
+ class Client
3
+ def daily_activity_summary(date=Date.today)
4
+ get("1/user/-/activities/date/#{format_date(date)}.json")
5
+ end
6
+
7
+ def frequent_activities
8
+ get("1/user/-/activities/frequent.json")
9
+ end
10
+
11
+ def favorite_activities
12
+ get("1/user/-/activities/favorite.json")
13
+ end
14
+
15
+ def all_activities
16
+ get("1/activities.json")
17
+ end
18
+
19
+ def lifetime_stats
20
+ get("1/user/-/activities.json")
21
+ end
22
+
23
+ def daily_goals
24
+ get("1/user/-/activities/goals/daily.json")
25
+ end
26
+
27
+ def weekly_goals
28
+ get("1/user/-/activities/goals/weekly.json")
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ module Fitbyte
2
+ class Client
3
+ def alarms(tracker_id)
4
+ get("1/user/-/devices/tracker/#{tracker_id}/alarms.json")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ module Fitbyte
2
+ class Client
3
+ def weight_logs(date=Date.today)
4
+ get("1/user/-/body/log/weight/date/#{format_date(date)}.json")
5
+ end
6
+
7
+ def body_fat_logs(date=Date.today)
8
+ get("1/user/-/body/log/fat/date/#{format_date(date)}.json")
9
+ end
10
+
11
+ def weight_goals
12
+ get("1/user/-/body/log/weight/goal.json")
13
+ end
14
+
15
+ def body_fat_goals
16
+ get("1/user/-/body/log/fat/goal.json")
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,67 @@
1
+ require "fitbyte/helpers"
2
+ require "fitbyte/activities"
3
+ require "fitbyte/alarms"
4
+ require "fitbyte/body"
5
+ require "fitbyte/devices"
6
+ require "fitbyte/food"
7
+ require "fitbyte/friends"
8
+ require "fitbyte/sleep"
9
+ require "fitbyte/user"
10
+ require "fitbyte/water"
11
+
12
+ module Fitbyte
13
+ class Client
14
+ 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
19
+ @client_id = options[:client_id]
20
+ @client_secret = options[:client_secret]
21
+
22
+ @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"
26
+
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"
30
+
31
+ @client = OAuth2::Client.new(@client_id, @client_secret, site: @site_url,
32
+ authorize_url: @authorize_url, token_url: @token_url)
33
+ end
34
+
35
+ def auth_page_link
36
+ @client.auth_code.authorize_url(redirect_uri: @redirect_uri, scope: @scope)
37
+ end
38
+
39
+ def get_token(auth_code)
40
+ @token = @client.auth_code.get_token(auth_code, redirect_uri: @redirect_uri, headers: auth_header)
41
+ end
42
+
43
+ def token
44
+ @token.expired? ? refresh_token : @token
45
+ end
46
+
47
+ def refresh_token
48
+ @token = @token.refresh!(headers: auth_header)
49
+ end
50
+
51
+ def auth_header
52
+ {"Authorization" => ("Basic " + Base64.encode64(@client_id + ":" + @client_secret))}
53
+ end
54
+
55
+ def request_headers
56
+ {
57
+ "User-Agent" => "fitbyte-#{Fitbyte::VERSION} gem(#{Fitbyte::REPO_URL})",
58
+ "Accept-Language" => @unit_system,
59
+ "Accept-Locale" => @locale
60
+ }
61
+ end
62
+
63
+ def get(path)
64
+ JSON.parse(token.get(path, headers: request_headers).response.body)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,7 @@
1
+ module Fitbyte
2
+ class Client
3
+ def devices
4
+ get("1/user/-/devices.json")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ module Fitbyte
2
+ class Client
3
+ def food_logs(date=Date.today)
4
+ get("1/user/-/foods/log/date/#{format_date(date)}.json")
5
+ end
6
+
7
+ def recent_foods
8
+ get("1/user/-/foods/recent.json")
9
+ end
10
+
11
+ def frequent_foods
12
+ get("1/user/-/foods/log/frequent.json")
13
+ end
14
+
15
+ def favorite_foods
16
+ get("1/user/-/foods/log/favorite.json")
17
+ end
18
+
19
+ def food_goals
20
+ get("1/user/-/foods/log/goal.json")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,11 @@
1
+ module Fitbyte
2
+ class Client
3
+ def friends
4
+ get("1/user/-/friends.json")
5
+ end
6
+
7
+ def friends_leaderboard
8
+ get("1/user/-/friends/leaderboard.json")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,17 @@
1
+ module Fitbyte
2
+ class Client
3
+ def format_date(date)
4
+ if [Date, Time, DateTime].include?(date.class)
5
+ date.strftime("%Y-%m-%d")
6
+ elsif date.is_a? String
7
+ if date =~ /\d{4}\-\d{2}\-\d{2}/
8
+ date
9
+ else
10
+ raise ArgumentError, "Invalid argument [\"#{date}\"] - string must follow yyyy-MM-dd format."
11
+ end
12
+ else
13
+ raise ArgumentError, "Invalid type [#{date.class}] - provide a Date/Time/DateTime or a String(yyyy-MM-dd format)."
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module Fitbyte
2
+ class Client
3
+ def sleep_logs(date=Date.today)
4
+ get("1/user/-/sleep/date/#{format_date(date)}.json")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Fitbyte
2
+ class Client
3
+ def profile
4
+ get("1/user/-/profile.json")
5
+ end
6
+
7
+ def badges
8
+ get("1/user/-/badges.json")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Fitbyte
2
+ VERSION = "0.1.0"
3
+ REPO_URL = "https://github.com/zokioki/fitbyte"
4
+ end
@@ -0,0 +1,7 @@
1
+ module Fitbyte
2
+ class Client
3
+ def water_logs(date=Date.today)
4
+ get("1/user/-/foods/log/water/date/#{format_date(date)}.json")
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fitbyte
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zoran
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - zoran1991@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - fitbyte.gemspec
86
+ - lib/fitbyte.rb
87
+ - lib/fitbyte/activities.rb
88
+ - lib/fitbyte/alarms.rb
89
+ - lib/fitbyte/body.rb
90
+ - lib/fitbyte/client.rb
91
+ - lib/fitbyte/devices.rb
92
+ - lib/fitbyte/food.rb
93
+ - lib/fitbyte/friends.rb
94
+ - lib/fitbyte/helpers.rb
95
+ - lib/fitbyte/sleep.rb
96
+ - lib/fitbyte/user.rb
97
+ - lib/fitbyte/version.rb
98
+ - lib/fitbyte/water.rb
99
+ homepage: https://github.com/zokioki/fitbyte
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.4.5
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: A gem that allows interaction with Fitbit's REST API, using OAuth2 for user
123
+ authorization.
124
+ test_files: []